发送特殊字符串口 [英] Send special characters to Serial Port

查看:153
本文介绍了发送特殊字符串口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,允许交换消息,我尝试发送特殊字符的字符串

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



不过,我收到这一领域的空白N 2 O 2 EST?一个funcionar! (PT)

我试图把 comPort1.Encoding = Encoding.UTF8; 之前,但它不工作呢。我试过diferent编码。

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

推荐答案

如果你写你的字符串港口的编码字节,它们将被正确发送。
这段代码就可以了你!

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

这篇关于发送特殊字符串口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆