WIP modbus write

This commit is contained in:
Duke NUCem 2021-10-10 11:35:14 +02:00
parent 917886820d
commit 051509155e

View File

@ -65,20 +65,28 @@ int8_t modbus_copy_reply_to_buffer(uint8_t *buffer, uint8_t *msg_len, modbus_tra
if (transaction->function_code | MODBUS_ERROR_FLAG) { if (transaction->function_code | MODBUS_ERROR_FLAG) {
/* sending error reply */ /* sending error reply */
buffer[2] = transaction->exception.exception_code; buffer[2] = transaction->exception.exception_code;
} } else {
switch (transaction->function_code) { switch (transaction->function_code) {
case MODBUS_READ_HOLDING_REGISTERS: case MODBUS_READ_HOLDING_REGISTERS:
case MODBUS_READ_INPUT_REGISTERS: case MODBUS_READ_INPUT_REGISTERS:
byte_count = transaction->register_count * 2; byte_count = transaction->register_count * 2;
buffer[2] = byte_count; buffer[2] = byte_count;
*msg_len = byte_count + 5; *msg_len = byte_count + 5;
for (int i = 0; i < transaction->register_count; i++) { for (int i = 0; i < transaction->register_count; i++) {
// TODO endianness handling // TODO endianness handling
/* buffer16b is alias for both holding and input register buffers */ /* buffer16b is alias for both holding and input register buffers */
buffer[3 + 2*i] = transaction->buffer16b[i] >> 8; buffer[3 + 2*i] = transaction->buffer16b[i] >> 8;
buffer[4 + 2*i] = transaction->buffer16b[i] & 0xff; buffer[4 + 2*i] = transaction->buffer16b[i] & 0xff;
} }
break; break;
case MODBUS_WRITE_SINGLE_REGISTER:
buffer[2] = (uint8_t) (transaction->register_address >> 8);
buffer[3] = (uint8_t) transaction->register_address;
buffer[4] = (uint8_t) (transaction->holding_registers[0] >> 8);
buffer[5] = (uint8_t) transaction->holding_registers[0];
*msg_len = 8;
break;
}
} }
crc16 = modbus_CRC16(buffer, *msg_len - 2); /* last two bytes is the checksum itself */ crc16 = modbus_CRC16(buffer, *msg_len - 2); /* last two bytes is the checksum itself */
buffer[*msg_len - 2] = crc16 & 0xff; buffer[*msg_len - 2] = crc16 & 0xff;