首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

急串口接收数据乱码有关问题

2012-03-17 
急:串口接收数据乱码问题目前做称重管理系统:现在环境是:一个传感器连接称重显示器,然后连接电脑。用一个串

急:串口接收数据乱码问题
目前做称重管理系统:
现在环境是:一个传感器连接称重显示器,然后连接电脑。
用一个串口测试程序测了一下。接收到的数据,一开始老是乱码。

为什么?
第二,我比特率之类的参数已经和显示器参数都设置好了。
第三。是不是因为乱码的问题,我读出来的数据都是问号

先谢谢



[解决办法]
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;
[解决办法]

探讨

现在问题是怎么递归把02开头的数,取出来

[解决办法]
探讨

引用:

现在问题是怎么递归把02开头的数,取出来

请看通过缓存实现接收数据的解析

[解决办法]
楼主想多了,串口发给你的是16进制的内容。
你需要去了解,发送过来的数据的格式。

如他们告诉你,返回的第一个是值是一个整数。那么你就

 BitConverter.ToInt32得到第一个值
返回的第二个的值是一个单精度浮度,你就接着
BitConverter.ToSingle

一直往下读。

如果您知道所有的数都是整数,你就可以用一个循环来读 BitConverter.ToInt32

不过,你要注意,有时候不是一次读就可以读到所有数据的。
特别是连续的收到数据时,这时候一般串口报文数据就是有一定的分隔符了。你得询问硬件开发商。


[解决办法]
这程序我做过,给个串口读取的例子你看看吧
C# code
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;            }                  }    }} 

热点排行