Modbus query problem

D

Thread Starter

David Cemin

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
 
david
check using another pc to varify the data being sent. It's possible you maybe to fast for the end device. Is there handshaking enabled xon xoff

glengillisnaz @ hotmail.com
 
F

Fred Loveless

Just out of curiosity where is the checksum for the RTU message being generated. The Modbus RTU Serial packet requires the checksum. The Modbus TCP packet does not need it but you do need a valid MBAP header in the packet. I am surpised that you are not at least getting an error response from the device.
 
Actually not. I am not using any kind of handshaking. Do you think that maybe I am sending that data too fast for the controller interpret? Maybe then I need to send all the message once, in a unsigned long int format(8 bytes) ..

Ideas?
 
You can use a MODBUS Simulator software to simulate a MODBUS Master or Slave. It can show every bytes on the wire and the timing of the message. I bet it will help you find the problem.

The one I used is from Calta.(http://www.calta.com/)
It is a pretty handy tool to diagnose MODBUS problem. You can try other similar products. A lot of them are free.

Peter Hao
 
Actually it is not necessary to append a checksum on the end of the query data, as long as the checksum is disabled on the controller.
 
E

Eric Ratliff

I have never heard of putting pauses between characters of a Modbus RTU command or response. I would send all six chartacters and the checksum as one message. When sending a Modbus RTU response, a delay is often required before starting the reply, but no between characters in the reply.

Eric
www.icpdas-usa.com
 
Thread starter Similar threads Forum Replies Date
A Modbus 4
P Modbus 3
N Modbus 3
S Modbus 2
K Modbus 2
Top