急:串口接收数据乱码问题
目前做称重管理系统:
现在环境是:一个传感器连接称重显示器,然后连接电脑。
用一个串口测试程序测了一下。接收到的数据,一开始老是乱码。
为什么?
第二,我比特率之类的参数已经和显示器参数都设置好了。
第三。是不是因为乱码的问题,我读出来的数据都是问号
先谢谢
[解决办法]
1、首先验证一帧应答数据的合法性:起始符、结束符、异或校验
2、第3~8字节的值是ASCII表中的值,如31表示数字1,32表示数组2,也就是说称重值为5位
3、把5位称重值合成为int类型数值weight
4、获取第9字节小数点位数pointer(也是ASCII转换),然后weight=weight/power(10,pointer)
5、获取第2字节符号位symbol,weight=symbol=='+'?weight:0-weight;
[解决办法]
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO.Ports;namespace Demo{ public partial class myForm : Form { //private int[] ReceiveData = new int[4]; //private int ReceiveDataIndex = 0; string ReceiveData; public myForm() { InitializeComponent(); } private void myForm_Load(object sender, EventArgs e) { comboBoxPortName.SelectedIndex = 0; comboBoxBaudRate.SelectedIndex = 5; comboBoxParity.SelectedIndex = 0; comboBoxDataBits.SelectedIndex = 0; comboBoxStopBits.SelectedIndex = 0; btnClose.Enabled = false; btnOpen.Enabled = true; textBoxInformation.Text = "系统初始化成功!\r\n"; } private void btnOpen_Click(object sender, EventArgs e) { String myParity; String myStopBits; myParity = comboBoxParity.SelectedItem.ToString(); myStopBits = comboBoxStopBits.SelectedItem.ToString(); //设置端口号 mySerialPort.PortName = comboBoxPortName.SelectedItem.ToString(); //设置波特率 mySerialPort.BaudRate = Convert.ToInt32(comboBoxBaudRate.SelectedItem); //设置校验位 switch (myParity) { case "None": mySerialPort.Parity = Parity.None; break; case "Even": mySerialPort.Parity = Parity.Even; break; case "Odd": mySerialPort.Parity = Parity.Odd; break; default: mySerialPort.Parity = Parity.None; break; } //设置数据位 mySerialPort.DataBits = Convert.ToInt32(comboBoxDataBits.SelectedItem); //设置停止位 switch(myStopBits) { case "1": mySerialPort.StopBits = StopBits.One; break; case "2": mySerialPort.StopBits = StopBits.Two; break; default: mySerialPort.StopBits = StopBits.One; break; } //采用ASCII编码方式 mySerialPort.Encoding = Encoding.ASCII; //接收到一个字符就出发接收事件 mySerialPort.ReceivedBytesThreshold = 1; //试图打开指定串口 try { if (mySerialPort.IsOpen == false) { mySerialPort.Open(); //打开串口 btnClose.Enabled = true; //关闭按键失能 btnOpen.Enabled = false; //打开按键失能 textBoxInformation.AppendText("串口已打开\r\n"); textBoxInformation.ScrollToCaret(); } } //打开异常,输出提示信息 catch { MessageBox.Show("串口打开异常","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning); } } private void btnClose_Click(object sender, EventArgs e) { if (mySerialPort.IsOpen == true) { mySerialPort.Close(); //关闭串口 btnClose.Enabled = false; btnOpen.Enabled = true; textBoxInformation.AppendText("串口已关闭\r\n"); textBoxInformation.ScrollToCaret(); } } private void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { String ch = mySerialPort.ReadExisting(); switch (ch) { case "\x12" : //接收到串口头 //清空接收数组 ReceiveData = string.Empty; break; case "\x14" : //接收到串口尾 //在拥有此控件的基础窗口句柄的线程上执行委托Invoke(Delegate) //即在控件textBoxInformation的父窗口form中执行委托. textBoxInformation.Invoke ( new MethodInvoker ( delegate { //textBoxInformation.AppendText(ReceiveData); textBoxReceiveData.Text = ReceiveData; } ) ); break; default: ReceiveData += ch; break; } } }}