C#串口接收数据乱码问题

zhuzheweiwei521 2010-03-04 01:31:05
private void Form1_Load(object sender, EventArgs e)
{
mainPort = new SerialPort();

interfaceUpdateHandle = new HandleInterfaceUpdateDelegate(UpdateTextBox); //实例化委托对象
mainPort.DataReceived += new SerialDataReceivedEventHandler(mainPort_DataReceived);
mainPort.ReceivedBytesThreshold = 1;


}

private void button1_Click(object sender, EventArgs e)
{
mainPort.PortName = "COM1";
mainPort.BaudRate = 9600;
mainPort.Open();
Send(Encoding.ASCII.GetBytes("Connect"));
}

public void mainPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] readBuffer = new byte[mainPort.BytesToRead];
mainPort.Read(readBuffer, 0, readBuffer.Length);
this.Invoke(interfaceUpdateHandle, new string[] { Encoding.Unicode.GetString(readBuffer) });
}


private void UpdateTextBox(string text)
{
this.textBox1.Text = text;
}


代码如上 ,为什么我接收到的信息是乱码的 请各位大大帮看看 谢谢
...全文
1068 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhuzheweiwei521 2010-03-04
  • 打赏
  • 举报
回复
2楼一针见血 谢谢
兔子-顾问 2010-03-04
  • 打赏
  • 举报
回复
用串口传输汉字?说明传输协议有问题。
kyoybs 2010-03-04
  • 打赏
  • 举报
回复
Encoding.ASCII 不适合处理汉字
兔子-顾问 2010-03-04
  • 打赏
  • 举报
回复
Encoding.Unicode->Encoding.ASCII
kyoybs 2010-03-04
  • 打赏
  • 举报
回复
传输之前做BASE64转码,之后再转回来

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧