Send special characters to Serial Port

2019-05-14 20:44发布

I have an application that allow exchange messages and I'm trying to send a string with special characters

string my_str = "isto não está a funcionar! (pt)";
comPort1.Write(my_str);

But I receive isto n?o est? a funcionar! (pt) .

I tried to put comPort1.Encoding = Encoding.UTF8; before but it's not working yet. I tried diferent encodings.

1条回答
不美不萌又怎样
2楼-- · 2019-05-14 21:47

If you write the encoded bytes of your string to the port, they will be sent correctly. This piece of code will do the trick for you:

string my_str = "isto não está a funcionar! (pt)";
byte[] my_bytes = System.Text.Encoding.UTF8.GetBytes(str);
comPort1.Write(my_bytes, 0, my_bytes.Length);
查看更多
登录 后发表回答