Send special characters to Serial Port

时光怂恿深爱的人放手 提交于 2019-12-22 10:46:12

问题


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:


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);


来源:https://stackoverflow.com/questions/18833160/send-special-characters-to-serial-port

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!