public FxCommandResponse Send(short channelNo, string data, ICellDataType responseDataType) { if (string.IsNullOrEmpty(data)) { return(null); } byte[] buff = ASCIIEncoding.ASCII.GetBytes(data); return(Send(channelNo, buff, buff.Length, responseDataType)); }
/// <summary> /// 解析PLC响应数据(到集合中) /// </summary> /// <param name="pureData"></param> /// <param name="startIndex"></param> /// <param name="cellDataType">支持数据类型 UInt16DataType.Default,UInt32DataType.Default</param> /// <param name="value">解析后的返回值</param> public static void ParseSmart(byte[] data, int startIndex, ICellDataType cellDataType, out List <int> value) { if (cellDataType.DataItemSize == 1) { ParseSmart <UInt8DataType>(data, startIndex, out value); } else if (cellDataType.DataItemSize == 4) { ParseSmart <UInt32DataType>(data, startIndex, out value); } else { ParseSmart <UInt16DataType>(data, startIndex, out value); } }
public FxCommandResponse(ResultCodeConst resultCode, byte[] rawData, ICellDataType responseDataType) { ResultCode = resultCode; RawData = rawData; _ResponseDataType = responseDataType; }
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); }
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); }