Example #1
0
        /// <summary>
        /// 处理串口接受到的数据,如果有完整且有效的返回数据,则存入_atrList<T>变量
        /// </summary>
        private static void ProcessRecvData()
        {
            if (_recvLen - _procLen < 6)
            {
                return;
            }

            int tmpPtr;

            do
            {
                tmpPtr = _procLen;
                bool hasSTX = false;
                int  i      = 0;
                while (i < _recvLen - tmpPtr)
                {
                    if (_recvBuffer[tmpPtr + i] == 0x02) //查找起始位(0x02)
                    {
                        hasSTX   = true;
                        _procLen = tmpPtr + i;
                    }
                    else if (_recvBuffer[tmpPtr + i] == 0x03 && hasSTX) //查找结束位(0x03),且存在起始位
                    {
                        i++;
                        byte[] atrData = CMDUtil.DecodeRecvData(_recvBuffer, _procLen, tmpPtr + i - _procLen);
                        _procLen = tmpPtr + i;

                        if (atrData.Length > 4 || atrData[1] + 4 == atrData.Length) // 返回数据长度正常
                        {
                            byte crc = 0;
                            for (int j = 1; j < atrData[1] + 2; j++)
                            {
                                crc = (byte)(crc + atrData[j]);
                            }
                            if (atrData[atrData[1] + 2] == crc) // 校验和比对
                            {
                                _atrList.Add(atrData);
                            }
                        }
                        break;
                    }
                    else if (_recvBuffer[tmpPtr + i] == 0x10) // 跳过数据位
                    {
                        i++;
                    }

                    i++;
                }
                tmpPtr += i;
            } while (tmpPtr < _recvLen);
        }
Example #2
0
        /// <summary>
        /// 发送命令
        ///    0:	命令完成成功
        ///   -1:	命令完成失败
        ///  -16:	命令校验失败
        ///  -17:	命令错误(没有该命令)
        ///  -18:	命令参数错误
        ///   -5:COM口未连接
        ///   -6:COM口读错误
        ///   -7:控制板无响应或返回数据不合法
        /// </summary>
        /// <param name="cmd">命令字节(不包括STX、Len、CRC和ETX)</param>
        /// <returns>
        /// </returns>
        private static int SendCommand(byte[] cmd)
        {
            int cmdLen = cmd.Length + 4;

            byte[] srcCmd = new byte[cmdLen];
            srcCmd[0] = 0x02;   // STX
            srcCmd[1] = (byte)cmd.Length;
            Buffer.BlockCopy(cmd, 0, srcCmd, 2, cmd.Length);
            srcCmd[cmdLen - 1] = 0x03;  // ETX

            _isError     = false;
            _atrList     = new List <byte[]>(10);
            _recvLen     = 0;
            _procLen     = 0;
            _retAtrIndex = -1;

            byte cnt         = 0;
            byte lastAtrCode = 0;

            do
            {
                srcCmd[3]          = cnt;
                srcCmd[cmdLen - 2] = 0;
                for (int i = 1; i < cmdLen - 2; i++)
                {
                    srcCmd[cmdLen - 2] += srcCmd[i];    // 计算校验码
                }
                byte[] sendBuffer = CMDUtil.EncodeCommand(srcCmd);

                try
                {
                    _serialPort.Write(sendBuffer, 0, sendBuffer.Length);//发送指令
                    int timeOut = _timeOut;
                    do
                    {
                        Thread.Sleep(50);
                        timeOut -= 50;
                        ProcessRecvData();
                        for (int i = 0; i < _atrList.Count; i++)
                        {
                            byte[] atrData = _atrList[i];
                            if (atrData[2] != srcCmd[2])
                            {
                                continue;
                            }

                            if (atrData[4] == 0x00)
                            {
                                _retAtrIndex = i;
                                return(0);
                            }
                            else
                            {
                                lastAtrCode = atrData[4];
                            }
                        }
                    }while (!_isError && timeOut > 0);
                }
                catch (Exception ex)
                {
                    _log.Error("出票机构控制板指令发送错误!指令:" + BitConverter.ToString(sendBuffer).Replace('-', ' '), ex);
                }

                cnt++;
            } while (cnt <= _retryTimes);

            if (lastAtrCode != 0x00)
            {
                return(-lastAtrCode);
            }
            else if (_isError)
            {
                return(-6); //COM口读错误
            }
            else
            {
                return(-7);   //IO板无响应或返回异常
            }
        }