简体   繁体   English

使用TCP从COM端口读取数据

[英]Read data from a COM port using TCP

I'm using a Moxa Serial Hub centralize some devices that communicate using RS-232 / NMEA protocol. 我正在使用Moxa串行集线器来集中一些使用RS-232 / NMEA协议进行通信的设备。

The serial hub is working as a TCP client, witch every time it receives data transmits it to a know IP/port in my machine. 串行集线器充当TCP客户端,每次接收到数据时都会将其传输到我机器中的已知IP /端口。

图式

The problem is that I'm not receiving the data properly (I thought all I had to do was create a TCP server that accepts connections, and read the data as bytes, but that just gives me some weird characters) 问题是我没有正确接收数据(我以为我要做的就是创建一个接受连接的TCP服务器,并以字节为单位读取数据,但这给了我一些奇怪的字符)

I receive the bytes...but when converting to text... its just some weird characters, I'm missing some detail in this conversion. 我收到了字节...但是当转换为文本时...只是一些奇怪的字符,我在此转换中缺少一些细节。 so far I've tried: 到目前为止,我已经尝试过:

byte[] b = new byte[1000];
int k = s.Receive(b);
Console.WriteLine(Encoding.UTF8.GetString(b, 0, 1000));
Console.WriteLine(Encoding.ASCII.GetString(b));
for (int i = 0; i < k; i++)
   Console.Write(Convert.ToChar(b[i])); 

all these 3 approaches write the same text... 所有这三种方法都写相同的文字...

Does any one know how to properly read the data in this cases? 在这种情况下,是否有人知道如何正确读取数据?

This comment provided the solution to the problem: 此评论提供了该问题的解决方案:

So you actually get a connection request from the moxa hub and receive (wrong) data? 因此,您实际上是从moxa集线器收到连接请求并接收(错误的)数据吗? Then it might be related to wrong serial port settings on the moxa device (probably the wrong baud rate) – Jan 2 hours ago 然后,这可能与moxa设备上的串行端口设置错误(可能是错误的波特率)有关– 1月2小时之前

Indeed, the baud rate was wrong. 确实,波特率是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM