问题
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