Hi,
I am using mbed os for the testing of Modbus RTU protocol using Nucleo F42ZI with TTL to RS485 converter.
The command is successfully sent to the modbus device (Motor Driver) but I don’t receive the response.
#include “mbed.h”
#include “BufferedSerial.h”
Serial pc(USBTX, USBRX);
Serial RS485(D1,D0,512); // Tx, Rx
DigitalOut ho(D2);
typedef uint8_t byte;
int value = 0;
//byte data[9] = {0x01,0x04,0x00,0x48,0x00,0x02,0xf1,0xdd};//your data
byte enable[9] = {0x01,0x06,0x20,0x0E,0x00,0x08,0xE2,0x0F};//your data
byte run[9] = {0x01,0x06,0x20,0x89,0x00,0x0A,0xD3,0xE7};//your data
byte stop[9] = {0x01,0x06,0x20,0x0E,0x00,0x07,0xA2,0x0B};//your data
byte data[9] = {0x01,0x03,0x20,0xAB,0x00,0x02,0xBE,0x2B};//your data
void snd_cmd(void){ //byte vdata[9]
pc.printf("Starting\n");
byte data[9] = {0x01,0x03,0x20,0xAB,0x00,0x02,0xBE,0x2B};
ho = 1; // 3.3V output from digital out pin for enabling tranmission
wait_ms(5);
for (int i=0; i<(8);i++)
{
RS485.putc(data[i]);
}
wait_ms(1);
char buffer[10];
ho = 0;
while(RS485.readable()){
if (RS485.readable()>0){
for (int count = 0; count < 10; count++) {
buffer[count]=RS485.getc();
}
for (int count = 1; count < 10; count++) {
pc.printf("%02hhX", buffer[count]);
}
}
break;
}
//pc.printf("\n");
//pc.printf("Done\n");
}
int main()
{
RS485.baud(115200);
RS485.format(
/* bits */ 8,
/* parity */ BufferedSerial::None,
/* stop bit */ 1
);
pc.baud(115200);
pc.format(
/* bits */ 8,
/* parity */ BufferedSerial::None,
/* stop bit */ 1
);
while(1) {
snd_cmd();
//pc.printf("error\n");
wait_ms(1);
//snd_cmd1();
//wait_ms(1000);
}
}
But if I uncomment the printf lines after the receive for loop I start receiving the response.
//pc.printf(“\n”);
//pc.printf(“Done\n”);
What could be the possible issue while receiving the response. Moreover, if I add a delay after the function snd_cmd() it stops working again. Can anyone guide me in this regard?