public ushort[] ReadData(int DglbId, uint size) { BPModbusDecoder decoder = new BPModbusDecoder(null, DglbId, (int)DevMgmtS_dcte.mgmt_cmd.DGLB_RD_FIRST); try { return modbus.ReadHoldingRegisters((byte)slaveAddress, (ushort)decoder.address, (ushort)(size / 2)); } catch (TimeoutException e) { throw new CommunicationException("Timeout has occured", e); } catch (InvalidOperationException e) { throw new CommunicationException("Serial port is not opened", e); } catch (SlaveException e) { //This encodes exceptions to modbus unspeciffic format switch (e.SlaveExceptionCode) { case 0x01: throw new CommunicationException("Read not supported", new InvalidOperationException()); case 0x02: throw new CommunicationException("Illegal data address, are we really talking to end device?", new AmbigousDeviceException()); case 0x04: //We assume that if slave device has a failure, it is unavalible. throw new CommunicationException("Slave device failure", new DeviceUnavalibleException()); } } catch (Exception e) { throw new CommunicationException("Unknown communication exception", e); } return null; }
public void ReadRecord(int DglbId, DataRow data) { DataRowSerializer serializer = new DataRowSerializer(data.Table); uint readCount; //Code for reading record try { readCount = serializer.GetSerializedDataLength(); } catch (SerializerException e) { throw new CommunicationException("Problems with getting length of serialized data", e); } //Here we read record UInt16[] receivedData = new UInt16[readCount / 2]; BPModbusDecoder decoder = new BPModbusDecoder(receivedData, DglbId, (int)DevMgmtS_dcte.mgmt_cmd.DGLB_RD_FIRST); decoder._data = ReadData(DglbId, readCount); //Here we deserialize record try { serializer.DeserializeData(decoder.data, data); } catch (SerializerException e) { throw new CommunicationException("Problems with deserialization", e); } }
public object ReadRecord(int DglbId, Type type) { BinarySerializer serializer = new BinarySerializer(new Type[] { type }); uint readCount; //Code for reading record try { readCount = serializer.GetSerializedDataLength(); } catch (SerializerException e) { throw new CommunicationException("Problems with getting length of serialized data", e); } //Here we read record UInt16[] receivedData = new UInt16[readCount / 2]; BPModbusDecoder decoder = new BPModbusDecoder(receivedData, DglbId, (int)DevMgmtS_dcte.mgmt_cmd.DGLB_RD_FIRST); decoder._data = ReadData(DglbId, readCount); //Here we deserialize record try { return serializer.DeserializeData(decoder.data)[0]; } catch (SerializerException e) { throw new CommunicationException("Problems with deserialization", e); } }