Hi,
I am trying to implement UART communication to send a Modbus frame and read the response. I have successfully tested my RS485 Modbus communication using a logic analyzer when I use a GPIO pin to control the RE & DE pins of RS485. However, I'm currently experiencing an issue where I cannot receive the Modbus response through the Rx pin on the Launchpad. When attempting to read the size of the read byte, only a single byte is displayed, and the read buffer appears to be empty. Is there a potential issue with my read function?
code snippet:
size_t bytesRead;
uint8_t buffer[25];
void UART_test() {
// Initialize UART2 parameters
UART2_Params params;
UART2_Params_init(¶ms);
params.baudRate = 9600;
params.readMode = UART2_Mode_BLOCKING;
params.writeMode = UART2_Mode_BLOCKING;
params.readReturnMode = UART2_ReadReturnMode_PARTIAL;
// Open the UART
UART2_Handle uart;
uart = UART2_open(CONFIG_UART_RS485, ¶ms);
// Check if UART opening was successful
if (uart == NULL) {
// Print an error message and return or handle the error appropriately
Display_printf(display, 0, 0, "Error opening UART\n");
return;
}
while(1){
GPIO_write(CONFIG_GPIO_0, 1);
// Write the Modbus frame to the UART
uint8_t modbusFrame[] = {0x03, 0x03, 0x00, 0x00, 0x00, 0x01, 0x85, 0xE8};
int32_t writeStatus = UART2_write(uart, modbusFrame, sizeof(modbusFrame), NULL);
delayMilliseconds(8);
GPIO_write(CONFIG_GPIO_0, 0);
if (writeStatus < 0) {
// Handle error writing to UART
Display_printf(display, 0, 0, "Error writing to UART\n");
}
sleep(2);
// Read the response from the UART
uint32_t readStatus = UART2_read(uart, buffer, sizeof(buffer), &bytesRead);
Display_printf(display, 0, 0,"readbytet size %d\n", bytesRead);
if (readStatus != UART2_STATUS_SUCCESS) {
// Handle error reading from UART
Display_printf(display, 0, 0, "Error reading from UART\n");
} else {
// Process the received data in 'buffer'
Display_printf(display, 0, 0, "success reading from UART\n");
// Print each byte in hexadecimal format
Display_printf(display, 0, 0," data: %x\n", buffer[1]);
Display_printf(display, 0, 0," data: %x\n", buffer[2]);
Display_printf(display, 0, 0," data: %x\n", buffer[3]);
Display_printf(display, 0, 0," data: %x\n", buffer[4]);
Display_printf(display, 0, 0," data: %x\n", buffer[5]);
Display_printf(display, 0, 0," data: %x\n", buffer[6]);
Display_printf(display, 0, 0," data: %x\n", buffer[7]);
}
sleep(5);
}
// Close the UART
UART2_close(uart);
}
console log:
readbytet size 1
success reading from UART
data: 0
data: 0
data: 0
data: 0
data: 0
data: 0
data: 0
logical analyzer:
pin configuration: