Modbus over 485 problem

Ok, Lets hope someone replies to this, so far the track record here is not too good.

I’m trying to implement Modbus RTU over 485.

I have proven my hardware works, I can read and write over 485. When the Modbus library goes to read the line it reads 100 characters and returns the bytecount as 100, there is a serial_timeout of 3 ms(quiet time @ 19200), it seems to ignore this. I switched over to 232 and everything works perfectly.

Can anyone think of what might cause modbus to fail to see the quiet time over 485?

Ron

Why in the Modbus library does the sample code pend on the read buffer to be empty after writing the data?

#if(SERIAL_MODE == 1 || SERIAL_MODE == 3)
	ser485Tx();									// enable the RS485 transmitter
   serXrdFlush(MODBUS_PORT);				// clear the read FIFO
	serXwrite ( MODBUS_PORT, Packet, ByteCount );		// send the data
	while ( serXrdUsed(MODBUS_PORT) != ByteCount ); // wait for all bytes to be transmitted
   ser485Rx();									// disable the RS485 transmitter
   serXrdFlush(MODBUS_PORT);								// clear the read FIFO
#else
	serXwrite ( MODBUS_PORT, Packet, ByteCount );		// send the data
#endif

Shouldn’t it be looking at the write buffer, when it’s empty then it’s ok to toggle the Tx enable?

Ron

Waiting for the transmit buffer to be empty is not good enough. Two bytes, one in the hardware “XMIT BUF” and the other in the transmitter are still going out. Turning off TX enable would cut them off.

By having the receive enable on the RS485 chip on, the bytes being transmitted are also being received. When all the received bytes are seen, as is being checked by the input byte count, then it is sure that all the bytes have been transmitted.

However, the Rabbit code is not very good; it is tying up the processor until the full message trasnmission is done. This is unacceptable in many situations.

A better, non-blocking way of checking the end of the transmission is to watch for the transmit buffer to be empty and then also check if the “XMIT BUFF FULL” (SxSR bit 3) and “XMIT SENDING BYTE” (SxSR bit 2) are both 0. Then the transmit enable can be turned off.

Thanks for your reply, this took care of another problem I was seeing, sometimes the last byte of the CRC was chopped off.

Still trying to find out why the quiet time on 485 is not being seen yet when I switch to 232 it works.

Ron

Figured out the problem, the PCB designer tied /RE to ground and that made every transmission also read into the input buffer. Apparently this is the way that Rabbit does it and it causes headaches with modbus.

Cutting the trace and tying /RE to DE solved the problem (as well as editing the Modbus lib on how to detect all bytes transmitted)

Ron