public void clearDataCOM() { try { COMPort.DiscardInBuffer(); } catch (Exception ex) {} }
private void ModbusSendReceive(object param) { lock (this) { try { ModbusParam parameters; if (param is ModbusParam) { parameters = (ModbusParam)param; } else { throw new ModbusPDUException("Wrong parametrization of ModbusSendReceive method."); } byte[] responseFrame = new byte[(int)parameters.ResponseSize]; COMPort.DiscardInBuffer(); COMPort.Write(parameters.RequestADU, 0, parameters.RequestADU.Length); Thread.Sleep(ResponseWaitingDelay); COMPort.Read(responseFrame, 0, (int)parameters.ResponseSize); byte[] adu = ModbusSerialPDU.extractADU(responseFrame); if (responseFrame[1] > (byte)127) { throw new ModbusPDUException(String.Format("Modbus application error : function {0:X}, exception {1:X}", responseFrame[1], responseFrame[2])); } else if (responseFrame[1] == 0x03) { Int16[] values = GetValuesFromResponseADU03(adu); ModbusDataReceived(values); } } catch (Exception ex) { commException = ex; ModbusError(ex); } } }