/// <summary> /// Инициализировать PDU запроса, рассчитать длину ответа /// </summary> public override void InitReqPDU() { if (Multiple) { // формирование PDU для команды WriteMultipleCoils или WriteMultipleRegisters int dataLength = TableType == TableType.Coils ? ((ElemCnt % 8 == 0) ? ElemCnt / 8 : ElemCnt / 8 + 1) : ElemCnt *ModbusUtils.GetDataLength(ElemType); ReqPDU = new byte[6 + dataLength]; ReqPDU[0] = FuncCode; ReqPDU[1] = (byte)(Address / 256); ReqPDU[2] = (byte)(Address % 256); ReqPDU[3] = (byte)(ElemCnt / 256); ReqPDU[4] = (byte)(ElemCnt % 256); ReqPDU[5] = (byte)dataLength; ModbusUtils.ApplyByteOrder(Data, 0, ReqPDU, 6, dataLength, ByteOrder, false); // установка длины ответа RespPduLen = 5; } else { // формирование PDU для команды WriteSingleCoil или WriteSingleRegister int dataLength = TableType == TableType.Coils ? 2 : ModbusUtils.GetDataLength(ElemType); ReqPDU = new byte[3 + dataLength]; ReqPDU[0] = FuncCode; ReqPDU[1] = (byte)(Address / 256); ReqPDU[2] = (byte)(Address % 256); if (TableType == TableType.Coils) { ReqPDU[3] = Value > 0 ? (byte)0xFF : (byte)0x00; ReqPDU[4] = 0x00; } else { byte[] data = dataLength == 2 ? new byte[] // standard Modbus { (byte)(Value / 256), (byte)(Value % 256) } : Data; ModbusUtils.ApplyByteOrder(data, 0, ReqPDU, 3, dataLength, ByteOrder, false); } // установка длины ответа RespPduLen = ReqPDU.Length; // echo } }
/// <summary> /// Получить значение элемента в соответствии с его типом, преобразованное в double /// </summary> public double GetElemVal(int elemInd) { Elem elem = Elems[elemInd]; byte[] elemVal = ElemVals[elemInd]; byte[] buf; // перестановка байт в случае необходимости if (elem.ByteOrder == null) { buf = elemVal; } else { buf = new byte[elemVal.Length]; ModbusUtils.ApplyByteOrder(elemVal, buf, elem.ByteOrder); } // расчёт значения switch (elem.ElemType) { case ElemType.UShort: return(BitConverter.ToUInt16(buf, 0)); case ElemType.Short: return(BitConverter.ToInt16(buf, 0)); case ElemType.UInt: return(BitConverter.ToUInt32(buf, 0)); case ElemType.Int: return(BitConverter.ToInt32(buf, 0)); case ElemType.ULong: return(BitConverter.ToUInt64(buf, 0)); case ElemType.Long: return(BitConverter.ToInt64(buf, 0)); case ElemType.Float: return(BitConverter.ToSingle(buf, 0)); case ElemType.Double: return(BitConverter.ToDouble(buf, 0)); case ElemType.Bool: return(buf[0] > 0 ? 1.0 : 0.0); default: return(0.0); } }
/// <summary> /// Get the value of the element according to its type, converted to double /// </summary> public double GetElemVal(int elemInd) { var elem = Elems[elemInd]; byte[] elemVal = ElemVals[elemInd]; byte[] buf; // swapping bytes if necessary if (elem.ByteOrder == null) { buf = elemVal; } else { buf = new byte[elemVal.Length]; ModbusUtils.ApplyByteOrder(elemVal, buf, elem.ByteOrder); } // calculation of value switch (elem.ElemType) { case ElemType.UShort: return(BitConverter.ToUInt16(buf, 0)); case ElemType.Short: return(BitConverter.ToInt16(buf, 0)); case ElemType.UInt: return(BitConverter.ToUInt32(buf, 0)); case ElemType.Int: return(BitConverter.ToInt32(buf, 0)); case ElemType.ULong: return(BitConverter.ToUInt64(buf, 0)); case ElemType.Long: return(BitConverter.ToInt64(buf, 0)); case ElemType.Float: return(BitConverter.ToSingle(buf, 0)); case ElemType.Double: return(BitConverter.ToDouble(buf, 0)); case ElemType.Bool: return(buf[0] > 0 ? 1.0 : 0.0); default: return(0.0); } }
/// <summary> /// Инициализировать PDU запроса, рассчитать длину ответа /// </summary> public override void InitReqPDU() { if (Multiple) { // формирование PDU для команды WriteMultipleCoils или WriteMultipleRegisters int byteCnt = TableType == TableType.Coils ? ((ElemCnt % 8 == 0) ? ElemCnt / 8 : ElemCnt / 8 + 1) : ElemCnt * 2; ReqPDU = new byte[6 + byteCnt]; ReqPDU[0] = FuncCode; ReqPDU[1] = (byte)(Address / 256); ReqPDU[2] = (byte)(Address % 256); ReqPDU[3] = (byte)(ElemCnt / 256); ReqPDU[4] = (byte)(ElemCnt % 256); ReqPDU[5] = (byte)byteCnt; ModbusUtils.ApplyByteOrder(Data, 0, ReqPDU, 6, byteCnt, ByteOrder, false); } else { // формирование PDU для команды WriteSingleCoil или WriteSingleRegister ReqPDU = new byte[5]; ReqPDU[0] = FuncCode; ReqPDU[1] = (byte)(Address / 256); ReqPDU[2] = (byte)(Address % 256); if (TableType == TableType.Coils) { ReqPDU[3] = Value > 0 ? (byte)0xFF : (byte)0x00; ReqPDU[4] = 0x00; } else { byte[] data = new byte[] { (byte)(Value / 256), (byte)(Value % 256) }; ModbusUtils.ApplyByteOrder(data, 0, ReqPDU, 3, 2, ByteOrder, false); } } // установка длины ответа RespPduLen = 5; }
/// <inheritdoc /> /// <summary> /// Initialize the request PDU, calculate the answer length /// </summary> public override void InitReqPDU() { if (Multiple) { // PDU generation for WriteMultipleCoils or WriteMultipleRegisters command int byteCnt = TableType == TableType.Coils ? ((ElemCnt % 8 == 0) ? ElemCnt / 8 : ElemCnt / 8 + 1) : ElemCnt * 2; ReqPDU = new byte[6 + byteCnt]; ReqPDU[0] = FuncCode; ReqPDU[1] = (byte)(Address / 256); ReqPDU[2] = (byte)(Address % 256); ReqPDU[3] = (byte)(ElemCnt / 256); ReqPDU[4] = (byte)(ElemCnt % 256); ReqPDU[5] = (byte)byteCnt; ModbusUtils.ApplyByteOrder(Data, ReqPDU, 6, byteCnt, ByteOrder); } else { // PDU generation for WriteSingleCoil or WriteSingleRegister ReqPDU = new byte[5]; ReqPDU[0] = FuncCode; ReqPDU[1] = (byte)(Address / 256); ReqPDU[2] = (byte)(Address % 256); if (TableType == TableType.Coils) { ReqPDU[3] = Value > 0 ? (byte)0xFF : (byte)0x00; ReqPDU[4] = 0x00; } else { var data = new byte[] { (byte)(Value / 256), (byte)(Value % 256) }; ModbusUtils.ApplyByteOrder(data, ReqPDU, 3, 2, ByteOrder); } } // setting the length of the response RespPduLen = 5; }