D
I am writing a modbus application software in C. So, first I did an API to RS232 communication, and second a layer with modbus protocol. I did also a visual program in C++ that calls the protocol. So, I'm having some trouble with the query message.
The query message is a 6-byte string(without CRC) that I send to the RS232 API functions. for example:
//It builds the packet
void modbusBuildQueryPacket(st_modbusQuery modbusQuery, unsigned char *queryPacket)
{
queryPacket[0] = modbusQuery.address;
queryPacket[1] = modbusQuery.function;
queryPacket[2] = modbusQuery.startAddressHI; //high order
queryPacket[3] = modbusQuery.startAddressLO; //low order
queryPacket[4] = modbusQuery.countHI;
queryPacket[5] = modbusQuery.countLO;
}
//function to send the query
int modbusFunctionSend(st_modbusQuery modbusQuery, unsigned char hasCrc, unsigned char *queryPacket)
{
modbusBuildQueryPacket(modbusQuery, queryPacket);
if (modbusSendQuery(queryPacket, MODBUS_QUERY_LENGTH, hasCrc) < 0)
return -1;
return 0;
}
//and now the function that writes on the rs232 //driver
int rs232_msgSend(unsigned char *buffer, int size)
{
DWORD dwBytesWritten = 0;
int i, status;
int bytesWritten = 0;
printf("Size: %d\n\r", size);
fflush(stdin);
for(i = 0; i < size; i++) {
status = WriteFile(Hcom, &buffer, sizeof(char), &dwBytesWritten, NULL);
bytesWritten += dwBytesWritten;
}
printf("Status: %d; bytesWritten: %d\n\r", status, bytesWritten);
if (!status)
return -1;
return bytesWritten;
}
So, I actually write at least 5 times on the driver to send the message. I can do at this way, or I need to write a single message of length 5?
My CLP receives the message, but it isn`t answering. (Yes, I double checked the address and functions that it supports)
If anyone have a better implementation idea, or suggestions, or an idea of where my mistake is, please send me an email.
davidcemin @ gmail.com
The query message is a 6-byte string(without CRC) that I send to the RS232 API functions. for example:
//It builds the packet
void modbusBuildQueryPacket(st_modbusQuery modbusQuery, unsigned char *queryPacket)
{
queryPacket[0] = modbusQuery.address;
queryPacket[1] = modbusQuery.function;
queryPacket[2] = modbusQuery.startAddressHI; //high order
queryPacket[3] = modbusQuery.startAddressLO; //low order
queryPacket[4] = modbusQuery.countHI;
queryPacket[5] = modbusQuery.countLO;
}
//function to send the query
int modbusFunctionSend(st_modbusQuery modbusQuery, unsigned char hasCrc, unsigned char *queryPacket)
{
modbusBuildQueryPacket(modbusQuery, queryPacket);
if (modbusSendQuery(queryPacket, MODBUS_QUERY_LENGTH, hasCrc) < 0)
return -1;
return 0;
}
//and now the function that writes on the rs232 //driver
int rs232_msgSend(unsigned char *buffer, int size)
{
DWORD dwBytesWritten = 0;
int i, status;
int bytesWritten = 0;
printf("Size: %d\n\r", size);
fflush(stdin);
for(i = 0; i < size; i++) {
status = WriteFile(Hcom, &buffer, sizeof(char), &dwBytesWritten, NULL);
bytesWritten += dwBytesWritten;
}
printf("Status: %d; bytesWritten: %d\n\r", status, bytesWritten);
if (!status)
return -1;
return bytesWritten;
}
So, I actually write at least 5 times on the driver to send the message. I can do at this way, or I need to write a single message of length 5?
My CLP receives the message, but it isn`t answering. (Yes, I double checked the address and functions that it supports)
If anyone have a better implementation idea, or suggestions, or an idea of where my mistake is, please send me an email.
davidcemin @ gmail.com