如何在C#中从Mbus正确接收数据? [英] How to receive data properly from Mbus in C#?

查看:313
本文介绍了如何在C#中从Mbus正确接收数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常可怕的问题让我几乎生病了。 2-3天,我一直在处理这个协议问题,我发现自己在这里得到你们的帮助。我希望我能解决。提前致谢。我在Vb中使用了旧的MsComm库代码。所以我决定用C#改变所有东西。我打开,关闭端口和发送数据等等。



Vb;我有以下部分代码,用于通过RS485从Mbus驱动程序接收数据。发送后,它会响应您以获取数据。它工作没问题。



I have really terrible problem that makes me almost sick. For 2-3 days, I've been dealing with this protocol issue and i find myself here to get some help from you guys. I hope I'll be solving. Thanks in advance. I had code in Vb that uses Old MsComm Library. So I decided to change all stuff with C#. I'm okey with opening , closing port and sending data etc.

In Vb; I have the following part of code which is for receiving data from Mbus driver via RS485. Once you sent this it responses you to obtain data. It works and no problem.

Dim SendData(19) As Byte
Dim sending As String
SendData(0) = &HFA
SendData(1) = Mid(DriverNo, 1, 2)
SendData(2) = Mid(DriverNo, 3, 2)
SendData(3) = Mid(DriverNo, 5, 2)
SendData(4) = Mid(DriverNo, 7, 2)
SendData(5) = 210

SendData(6) = CheckSum_Temass(5)
SendData(7) = &HFB

sending = ""
For i = 0 To 7
    sending= sending + Chr(SendData(i))
Next

SP.Output = sending





因此,上面的代码在Vb和Vb.Net中运行良好。但是,当我将其转换为C#时,如下所示;我无法得到mbus驱动程序的响应。在通过RS485发送数据时,我可以看到黄色引发火灾。通常在接收数据时,您可以看到红色指示灯也会触发。 C#中的代码;





So , the code above is working fine in Vb and Vb.Net. However when I convert it to C# like the following ; I cant get response from mbus driver. While sending data via RS485, I can see that yellow led fires. Normally while receiving data, you can see that red led also fires. The Code in C# ;

string sending= "";
byte[] SendData = new byte[8];
SentData[0] = 0xfa;
SendData[1] = Convert.ToByte((Strings.Mid(DriverNo, 1, 2)));
SendData[2] =  Convert.ToByte((Strings.Mid(DriverNo, 3, 2)));
SendData[3] = Convert.ToByte((Strings.Mid(DriverNo, 5, 2)));
SendData[4] = Convert.ToByte((Strings.Mid(DriverNo, 7, 2)));
SendData[5] = 210
SendData[6] = CheckSum_Temass(5); 
SendData[7] = 0xfb;

for (int i = 0; i <= 7; i++)
{
    sending= sending+ ((char)SendData[i]);
}

sp.Write(sending);





我看不出有任何问题但是Vb Code有效,而C#没有。



在c#中,以下是我的开放端口功能的一部分;





I cant see any problem with this but Vb Code works and C# does not.

In c# , the following is the part of my open port function ;

sp.PortName = portName;
    sp.BaudRate = baudRate;
    sp.DataBits = databits;
    sp.Parity = parity;
    sp.StopBits = StopBits.One;//stopBits;
    sp.PinChanged += SerialPinChangedEventHandler1;
    sp.ErrorReceived += SerialErrorReceivedEventHandler1;
    sp.DataReceived += new SerialDataReceivedEventHandler(DataReceived);

    sp.ReadTimeout = 1000;
    sp.WriteTimeout = 1000;





一切正常。我可以看到我通过RS485通过Mbus说的数据流。我可以从TX led看到它一直发送数据。但是,正如我再次说的那样,RX led不会触发。



Everything works fine. I can see as I said the flow of data through Mbus via RS485. I can see it from TX led which fires all the time I send data.However, as i said again, RX led does not fires.

推荐答案

可能有一个这里有点不对劲。 VB数组边界是一个很好的位置。



.Net中的字符是16位,这可能是问题所在。您正在处理二进制数据,因此请勿使用它们。只需检查SerialPort类的文档,默认编码为ASCII,7位。高于0x7f的任何内容都会转换为问号。所以我怀疑你的数据已经损坏了。



http://msdn.microsoft.com/en-us/library/y2sxhat8(v = vs.110).aspx [ ^ ]



而不是靠近字符串和字符。



尝试:

There could be a few things wrong here. The VB array bounds was a good spot.

Chars in .Net are 16bit, and that could be the problem. You're working with binary data so don't use them. Just checking the documentation for the SerialPort class, default encoding is ASCII, 7 bit. Anything above 0x7f gets converted to a question mark. So I suspect your data is getting corrupted.

http://msdn.microsoft.com/en-us/library/y2sxhat8(v=vs.110).aspx[^]

Instead don't go near strings and chars.

Try:
sp.Write(SendData, 0, 8);



看看是否有帮助。


And see if that helps.


这篇关于如何在C#中从Mbus正确接收数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆