日期:2014-05-18  浏览次数:21083 次

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

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

先谢谢



------解决方案--------------------
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;
            //接收到一个字符就出发接收事件