/// <summary> /// �ڲ����ڴ����շ��Ķ����߳� /// �����ڲ�ʵ��ͬ���շ� /// </summary> private void DealwithCommunicationRoute() { while (!_StopTheadFlag) { if (_DataBlockQueue.QueueLength == 0) { Thread.Sleep(20); continue; } FxCommandArgs arg = _DataBlockQueue.Pick(); if (arg == null) { continue; } FxCommandResponse result = _SerialDeamon.Send(arg.ChannelNo, arg.Data, arg.Data.Length); if (result.ResultCode == ResultCodeConst.rcFailt) { // �������ʧ�ܣ����ۼ���ʧ��ʱ�䣬����E�ʱֵ����Ҫ��UI�������з��� if (DateTime.Now.Subtract(_FirstFailtTime).TotalSeconds > 30) { _FirstFailtTime = DateTime.Now; } else if (_FirstFailtTime == DateTime.MaxValue) { _FirstFailtTime = DateTime.Now; } } else if (result != null) { _FirstFailtTime = DateTime.MaxValue; switch (arg.Cmd) { case 1: // �����... break; default: break; } } else { } if (_WaitHandle != null) { arg.Result = result; _WaitHandle.Set(); } //if (arg.OnSendHandle != null) { // arg.OnSendHandle.OnPackageRecived(result); //} } System.Diagnostics.Debug.Assert(false, "�˳��߳��ˣ���"); _Thead = null; }
/// <summary> /// 同步发送命令,并等待返回 /// 函数内部实现发送队列、以及超时机制 /// </summary> /// <param name="response">命令字</param> /// <param name="channelNo">通道号,默认0</param> /// <param name="cmdData">命令内容,以 0xAA,0xCC,0x83 开头的byte[] </param> /// <param name="timeout">最大超时设定</param> public FxCommandResponse SendCall(short cmd, byte channelNo, byte[] cmdData, TimeSpan timeout) { if (cmdData == null) // 如果命令没有构造,则默认构造无参数的命令 { FxCommandResponse prm = new FxCommandResponse(ResultCodeConst.rcNotSettting, cmdData); //cmdData = _CommanderHelper.MakeSmart(response, targetPort, prm); } FxCommandArgs arg = new FxCommandArgs(cmd, cmdData, channelNo); return(SendCall(arg, timeout)); }
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); }