a、串行通讯方式:连续发送称重值 b、串行通讯的数据格式 
10位:1位起始位(0),8位数据位(D0-D7),1为停止位(1)。 c、串行通讯发送的重量数据为ASCII码,每次发送12个字节。 
其定义如下: 
第1字节:起始位(02H) 
第2字节:状态字A 
第3字节:状态字B 
第4字节:状态字C 
第5字节:6位重量值的高位 
....... 
第10字节:6位重量值的低位 
第11字节:回车(0DH) 
第12字节:换行(0AH) d、状态字A 
D7--0 
D6--0 
D5--1 
D4--0 
D3--0 
小数点:X .X .XX .XXX .XXXX .XXXXX 
D2-- 0 0 1 1 1 1 
D1-- 0 1 0 0 1 1 
D0-- 0 1 0 1 0 1 e、状态字B 
D7--0 
D6--0 
D5--1 
D4--1 
D3--不稳定为0,稳定为1。 
D2-- 量程以内为0,超载为1 
D1--重量值正为0,负为1。 
D0--0 f、状态字C(20H) 

解决方案 »

  1.   

       /// <summary>
            /// 解析串口读出的数据
            /// </summary>
            /// <returns>用于显示的磅秤值</returns>
            private decimal ReadComData()
            {
                decimal a = 0;
                int count = 0;
                int readCount;
                byte[] readData = new byte[this.dataLength];
                int readOff;
                int readLength;            try
                {
                    do
                    {
                        count = comPort.ReadByte();
                    } while (count != this.flagBits);
                    readCount = this.dataLength;
                    readOff = 0;
                    while (readCount != 0)
                    {                    readLength = comPort.Read(readData, //缓冲区
                                                readOff,//偏移
                                                readCount //预期的长度
                                                );
                        readCount -= readLength; //调整缓冲区                    readOff += readLength;  //调整偏移                }                for (int i = 0; i < readDataLength; i++)
                    {
                        a = a * 10;
                        if (readDataWay == ReadDataWay.First)
                        {
                            a = a + (readData[this.whereToRead + i] & 0x000f);
                        }
                        else if (readDataWay == ReadDataWay.Last)
                        {
                            a = a + (readData[this.whereToRead + readDataLength - i - 1] & 0x000f);
                        }
                    }
                    return a;
                }
                catch (TimeoutException ex)
                {
                    return -1;
                }
            }
      

  2.   

    this.dataLength = 10;
    this.flagBits = 02h;
    this.whereToRead  =3;readDataLength = 6;根据开发经验,状态字基本上可以不分析
      

  3.   

    flagBits 是什么数据类型呀readDataWay 是什么
      

  4.   

    flagBits  设置为int
    readDataWay 是正反向的一个牧举,你可以不考虑
      

  5.   

    this.flagBits = 02h;我这出错
    02h是什么呀? if (readDataWay == ReadDataWay.First) 
                        { 
                            a = a + (readData[this.whereToRead + i] & 0x000f); 
                        } 
                        else if (readDataWay == ReadDataWay.Last) 
                        { 
                            a = a + (readData[this.whereToRead + readDataLength - i - 1] & 0x000f); 
                        } 
    这一段里的readDataWay没有定义,如果不用,怎么写,直接
    a = a + (readData[this.whereToRead + i] & 0x000f); 吗