public void RTU_ADU_DocSampleReq() { var DocRequest = new byte[] { 0x01, 0x10, 0x00, 0x01, 0x00, 0x01, 0x02, 0xFF, 0xFF, 0xA6, 0x31, }; var DocResponse = new byte[] { 0x01, 0x10, 0x00, 0x01, 0x00, 0x01, 0x50, 0x09, }; var ClientPDU = new WriteMultipleRegistersPDU(1, new int[] { 0xFFFF }); var Device = new RTU_Device(1); var ClientADU = new RTU_ADU(Device, ClientPDU); CollectionAssert.AreEqual(DocRequest, ClientADU.BuildRequest()); Assert.AreEqual(Status.OK, ClientADU.CheckResponse(DocResponse)); }
public int[] ReadInputRegisters(IModbusDevice Device, int StartAddr, int Quantity) { var PDU = new ReadInputRegistersPDU(StartAddr, Quantity); var ADU = new RTU_ADU((Device as RTU_Device), PDU); Query(ADU); return(PDU.Value); }
public void RTU_ADU_LifeCycle() { var ClientPDU = new ReadHoldingRegistersPDU(5, 10); var Device = new RTU_Device(1); var ClientADU = new RTU_ADU(Device, ClientPDU); var ServerADU = new RTU_ADU(Device); ServerADU.CheckRequest(ClientADU.BuildRequest()); }
public bool[] ReadCoils(IModbusDevice Device, int StartAddr, int Quantity) { if (!(Device is RTU_Device)) throw new ArgumentException("Wrong Type", "Device"); var PDU = new ReadCoilsPDU(StartAddr, Quantity); var ADU = new RTU_ADU((Device as RTU_Device), PDU); Query(ADU); return PDU.Value; }
public void WriteMultipleRegisters(IModbusDevice Device, int StartAddr, int[] Value) { if (!(Device is RTU_Device)) { throw new ArgumentException("Wrong Type", "Device"); } var PDU = new WriteMultipleRegistersPDU(StartAddr, Value); var ADU = new RTU_ADU((Device as RTU_Device), PDU); Query(ADU); }
public void WriteSingleCoil(IModbusDevice Device, int StartAddr, bool Value) { if (!(Device is RTU_Device)) { throw new ArgumentException("Wrong Type", "Device"); } var PDU = new WriteSingleCoilPDU(StartAddr, Value); var ADU = new RTU_ADU((Device as RTU_Device), PDU); Query(ADU); }
void Listen() { var Device = new RTU_Device(Address); Buffer = new List<byte>(); try { while (true) { var ADU = new RTU_ADU(Device); Port.DiscardInBuffer(); Buffer.Clear(); #region Recieve first byte Port.ReadTimeout = SerialPort.InfiniteTimeout; Buffer.Add((byte)Port.ReadByte()); //ByteRecievedEvent(Buffer.Last()); #endregion #region Recieve All bytes Port.ReadTimeout = ByteTimeout; try { while(true) Buffer.Add((byte)Port.ReadByte()); //ByteRecievedEvent(Buffer.Last()); } catch (TimeoutException) { } #endregion #region Handle Req if ((ADU.CheckRequest(Buffer.ToArray())) == Status.OK) { ADU.PDU.Handle(this); var Response = ADU.BuildResponse(); Port.Write(Response, 0, Response.Length); } #endregion } } catch (IOException) { } }
void Listen() { var Device = new RTU_Device(Address); Buffer = new List <byte>(); try { while (true) { var ADU = new RTU_ADU(Device); Port.DiscardInBuffer(); Buffer.Clear(); #region Recieve first byte Port.ReadTimeout = SerialPort.InfiniteTimeout; Buffer.Add((byte)Port.ReadByte()); //ByteRecievedEvent(Buffer.Last()); #endregion #region Recieve All bytes Port.ReadTimeout = ByteTimeout; try { while (true) { Buffer.Add((byte)Port.ReadByte()); } //ByteRecievedEvent(Buffer.Last()); } catch (TimeoutException) { } #endregion #region Handle Req if ((ADU.CheckRequest(Buffer.ToArray())) == Status.OK) { ADU.PDU.Handle(this); var Response = ADU.BuildResponse(); Port.Write(Response, 0, Response.Length); } #endregion } } catch (IOException) { } }
public bool[] ReadDiscreteInputs(IModbusDevice Device, int StartAddr, int Quantity) { if (!(Device is RTU_Device)) { throw new ArgumentException("Wrong Type", "Device"); } var PDU = new ReadDiscreteInputsPDU(StartAddr, Quantity); var ADU = new RTU_ADU((Device as RTU_Device), PDU); Query(ADU); return(PDU.Value); }
private Status Query(RTU_ADU ADU) { lock (Port) { var Request = ADU.BuildRequest(); var Count = 0; try { do { Port.DiscardInBuffer(); Buffer.Clear(); Port.Write(Request, 0, ADU.ReqSize); ReqSendEvent(ADU.BuildRequest()); if (!ADU.Device.Broadcast) { Port.ReadTimeout = Timeout; #region Wait for first byte try { Buffer.Add((byte)Port.ReadByte()); ByteRecievedEvent(Buffer.Last()); } catch (TimeoutException) { Count++; continue; } #endregion Port.ReadTimeout = ByteTimeout; #region Recieve All bytes try { for (int i = 0; i < ADU.RespSize - 1; i++) { Buffer.Add((byte)Port.ReadByte()); ByteRecievedEvent(Buffer.Last()); } } catch (TimeoutException) { } #endregion switch (ADU.CheckResponse(Buffer.ToArray())) { case Status.OK: Thread.Sleep(4); return Status.OK; case Status.Exception: throw new ModbusException(ADU.Device.Address, FunctionCode.WriteMultipleRegisters, (ExceptionType)ADU.PDU.ExceptionCode); case Status.Error: Thread.Sleep(4); Count++; break; } } else return Status.OK; } while (Count < MaxCount); } catch (IOException ex) { throw new RTU_Exception("Ошибка порта", ex); } throw new ModbusException(ADU.Device.Address, FunctionCode.WriteMultipleRegisters, (ExceptionType)ADU.PDU.ExceptionCode); } }
public void WriteMultipleRegisters(IModbusDevice Device, int StartAddr, int[] Value) { if (!(Device is RTU_Device)) throw new ArgumentException("Wrong Type", "Device"); var PDU = new WriteMultipleRegistersPDU(StartAddr, Value); var ADU = new RTU_ADU((Device as RTU_Device), PDU); Query(ADU) ; }
public void WriteSingleCoil(IModbusDevice Device, int StartAddr, bool Value) { if (!(Device is RTU_Device)) throw new ArgumentException("Wrong Type", "Device"); var PDU = new WriteSingleCoilPDU(StartAddr, Value); var ADU = new RTU_ADU((Device as RTU_Device), PDU); Query(ADU); }
public int[] ReadInputRegisters(IModbusDevice Device, int StartAddr, int Quantity) { var PDU = new ReadInputRegistersPDU(StartAddr, Quantity); var ADU = new RTU_ADU((Device as RTU_Device), PDU); Query(ADU); return PDU.Value; }
private Status Query(RTU_ADU ADU) { lock (Port) { var Request = ADU.BuildRequest(); var Count = 0; try { do { Port.DiscardInBuffer(); Buffer.Clear(); Port.Write(Request, 0, ADU.ReqSize); ReqSendEvent(ADU.BuildRequest()); if (!ADU.Device.Broadcast) { Port.ReadTimeout = Timeout; #region Wait for first byte try { Buffer.Add((byte)Port.ReadByte()); ByteRecievedEvent(Buffer.Last()); } catch (TimeoutException) { Count++; continue; } #endregion Port.ReadTimeout = ByteTimeout; #region Recieve All bytes try { for (int i = 0; i < ADU.RespSize - 1; i++) { Buffer.Add((byte)Port.ReadByte()); ByteRecievedEvent(Buffer.Last()); } } catch (TimeoutException) { } #endregion switch (ADU.CheckResponse(Buffer.ToArray())) { case Status.OK: Thread.Sleep(4); return(Status.OK); case Status.Exception: throw new ModbusException(ADU.Device.Address, FunctionCode.WriteMultipleRegisters, (ExceptionType)ADU.PDU.ExceptionCode); case Status.Error: Thread.Sleep(4); Count++; break; } } else { return(Status.OK); } } while (Count < MaxCount); } catch (IOException ex) { throw new RTU_Exception("Ошибка порта", ex); } throw new ModbusException(ADU.Device.Address, FunctionCode.WriteMultipleRegisters, (ExceptionType)ADU.PDU.ExceptionCode); } }