public FxCommandResponse Send(short channelNo, byte[] dataBuff, int dataSize, ICellDataType responseDataType)
        {
            byte[]            resultBuff = null;
            FxCommandResponse result     = new FxCommandResponse(ResultCodeConst.rcNotSettting, null, responseDataType);

            if (!_SerialPort.IsOpen)
            {
                return(result);
            }

            if (_SerialPort.BytesToWrite > 0)
            {
                //MyApp._LogWriter.WriteLine("准备写串口前,发现写缓冲中尚存{0}字节没有发出!", _SerialPort.BytesToWrite);
            }

            int reReadTimes = 0;                                                        // 重读次数

            _SerialPort.Write(dataBuff);

            System.Threading.Thread.Sleep(1);

            _RingBuffer.Clear();

            // 读取数据,直到得到完整报文或超时
            reReadTimes = 0;
            while (reReadTimes < MAX_RETRY_READ_COUNT)                                  // 连续超时指定次数,则不再等待
            {
                byte[] data = new byte[256];
                int    len  = _SerialPort.Read(data, 0, data.Length);

                if (len > 0)
                {
                    reReadTimes = 0;
                    _RingBuffer.Append(data, 0, len);

                    resultBuff = _RingBuffer.PickPackage();

                    if (resultBuff != null)
                    {
                        break;
                    }
                }
                else
                {
                    reReadTimes++;
                    System.Threading.Thread.Sleep(TimeSpan.FromMilliseconds(1));
                }
            }

            // 处理收到的数据
            if (resultBuff == null)
            {
                result.ResultCode = ResultCodeConst.rcFailt;
                Debug.Print("发送命令超时候仍没有收到FX PLC的合法响应.");
            }
            else if (resultBuff.Length == 1)
            {
                result.ResultCode = (ResultCodeConst)resultBuff[0];
            }
            else
            {
                result.ResultCode = ResultCodeConst.rcSuccess;

                List <int> valuelist;
                FxCommandHelper.ParseSmart(resultBuff, 0, responseDataType, out valuelist);

                result.SetResponseValue(valuelist);
                result.SetRawData(resultBuff);
            }

            return(result);
        }
Example #2
0
        public FxCommandResponse Send(short channelNo, byte[] dataBuff, int dataSize, ICellDataType responseDataType)
        {
            byte[]            resultBuff = null;
            FxCommandResponse result     = new FxCommandResponse(ResultCodeConst.rcNotSettting, null, responseDataType);

            if (!_SerialPort.IsOpen)
            {
                return(result);
            }

            if (_SerialPort.BytesToWrite > 0)
            {
                //MyApp._LogWriter.WriteLine("׼��д����ǰ������д�������д�{0}�ֽ�û�з�����", _SerialPort.BytesToWrite);
            }

            int reReadTimes = 0;                                                        // �ض�����

            _SerialPort.Write(dataBuff);

            System.Threading.Thread.Sleep(1);

            _RingBuffer.Clear();

            // ��ȡ���ݣ�ֱ���õ�́E����Ļ�ʱ
            reReadTimes = 0;
            while (reReadTimes < MAX_RETRY_READ_COUNT)
            {                                // ������ʱָ�����������ٵȴ�
                byte[] data = new byte[256];
                int    len  = _SerialPort.Read(data, 0, data.Length);

                if (len > 0)
                {
                    reReadTimes = 0;
                    _RingBuffer.Append(data, 0, len);

                    resultBuff = _RingBuffer.PickPackage();

                    if (resultBuff != null)
                    {
                        break;
                    }
                }
                else
                {
                    reReadTimes++;
                    System.Threading.Thread.Sleep(TimeSpan.FromMilliseconds(1));
                }
            }

            // �����յ�������
            if (resultBuff == null)
            {
                result.ResultCode = ResultCodeConst.rcFailt;
                Debug.Print("����ÁE�Ϭʱ����û���յ�FX PLC�ĺϷ�ρE�.");
            }
            else if (resultBuff.Length == 1)
            {
                result.ResultCode = (ResultCodeConst)resultBuff[0];
            }
            else
            {
                result.ResultCode = ResultCodeConst.rcSuccess;

                List <int> valuelist;
                FxCommandHelper.ParseSmart(resultBuff, 0, responseDataType, out valuelist);

                result.SetResponseValue(valuelist);
                result.SetRawData(resultBuff);
            }

            return(result);
        }