串口助手不支持中文 超级终端可以
public SerialPort m_sp; // 串口初始化
private void InitializeMy()
{
string strT;
strT = textBox1.Text; try
{
m_sp = new SerialPort();
m_sp.BaudRate = 57600;
m_sp.StopBits = StopBits.One;
m_sp.Parity = Parity.None;
m_sp.DataBits = 8;
m_sp.Handshake = Handshake.None;
m_sp.DataReceived += OnSerialDataReceived;
m_sp.Encoding = System.Text.Encoding.GetEncoding("GB2312");
}
catch (System.Exception e)
{
MessageBox.Show(e.Message);
} }
就可以发中文了
public SerialPort m_sp; // 串口初始化
private void InitializeMy()
{
string strT;
strT = textBox1.Text; try
{
m_sp = new SerialPort();
m_sp.BaudRate = 57600;
m_sp.StopBits = StopBits.One;
m_sp.Parity = Parity.None;
m_sp.DataBits = 8;
m_sp.Handshake = Handshake.None;
m_sp.DataReceived += OnSerialDataReceived;
m_sp.Encoding = System.Text.Encoding.GetEncoding("GB2312");
}
catch (System.Exception e)
{
MessageBox.Show(e.Message);
} }
就可以发中文了
System.Text.Encoding.GetEncoding("GB2312");
public static string ToUnicode(string str)
{
byte[] data = Encoding.BigEndianUnicode.GetBytes(str);
int i = 0;
StringBuilder sb = new StringBuilder();
foreach (byte b in data)
{
if (i++ % 2 == 0) sb.Append("\\u");
sb.AppendFormat("{0:X2}", b);
}
return sb.ToString();
}
更多实用帮助函数下载:http://download.csdn.net/source/2277988
个人感觉使用二进制序列化和反序列化比较简单,直接使用dotnet的功能,不需要自己另外写编码解码函数了。
然后Write就可以了
最近刚做了个CE下的串口调试助手,中文支持较好,是另一种方法的,
关键技术是,统一为字节接收,接收到字节流后,使用缓冲技术,保留
最后一个,不能显示的半个字节,留做和下次一起在判定。
每次收到的字节流均需判定最后一字节是否是半个汉字的前部,是前部
则存入缓冲区。