Data gets corrupted during transmission over the serial port

后端 未结 1 1427
我寻月下人不归
我寻月下人不归 2021-01-25 03:34

I am developing a program to communicate with an old system. I use System.IO.Ports.SerialPort for this. The problem is when I send a longer message, the message bevome corrupt.

相关标签:
1条回答
  • 2021-01-25 04:32

    My guess is that messageStr is a string, and you're seeing encoding issues. You've explicitly specified the UTF-8 encoding, so that's what you're getting - but I suspect it's not what you really want.

    You've shown binary data, so I assume you actually want to send exactly that binary data - in which case you should use the Write(byte[], int, int) overload.

    If you really want to write text data, you probably just need to pick the right encoding - but you'll need to give us more information for us to help you make the right choice.

    0 讨论(0)
提交回复
热议问题