I’m using an external Modbus library – written in C – to connect to a microntroller Modbus slave from a Linux machine, using a serial connection. I’m also using wxWidgets to create an application on the Linux machine, which runs a Modbus master in the background.
One of the Modbus functions accepts the name of the serial port the Modbus master should connect to. On my Linux machine, that’s /dev/ttyS4
. In the function that I’m using, the parameter type is defined as const char*
. When I pass the parameter as a string literal, i.e. "/dev/ttyS4"
, the function works fine. However when I first define a variable as follows:
const char mbPort[] = {'/', 'd', 'e', 'v', '/', 't', 't', 'y', 'S', '4'};
and then pass that variable to the function, it doesn’t work – and I’m not sure why that is. When passing the variable in a similar fashion in Windows, it worked fine. In Windows, my mbPort
variable looked as follows:
const char mbPort[] = { '\', '\', '.', '\', '\', 'C', 'O', 'M', '5' };
So now i’m not sure whether it’s a mistake in my code, or whether this is related to the OS that i’m using. Here is the declaration of the Modbus function that I’m using:
modbus_t* modbus_new_rtu(const char *device, int baud, char parity, int data_bit, int stop_bit);
Any help would be greatly appreciated.
Advertisement
Answer
Add the value ''
to the end of your initializer lists. Maybe on some compiler builds, there just happens to be a zero after the data in memory, but you cannot rely on that fact.