This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MODBUS RTU 3.5 character time

Other Parts Discussed in Thread: MSP430G2553

Hi, 

I am working on MSP430G2553. I need to implement MODBUS RTU over RS232. The 2553 will act as a slave. I have understood the entire protocol but have some difficulty in understanding the Start and Stop silent  interval. I have already written a code for ModBus Ascii which is working perfectly. In Modbus Ascii, it was easier to detect the start and stop of a frame since I could just compare inputs to : and 0x0D & 0x0A. However in Modbus RTU, the silent interval is a bit confusing.

  • Does the silent interval of 3.5 char times(4.01ms for 9600 BR and 11 bits) mean that I have to wait for this time before a character is received? How does the slave get triggered initially?
  • And how do I go about this timing? I mean what should be the general flow of the timing? When the controller is initialized, suppose I start a timer for 4.01ms, what if I receive an RX interrupt before this 4.01ms? Should this char be ignored and the time counter reset and we wait for the next character? How to ensure that this character is an address byte? And what if the next character is not an address byte?
  • What if there is nothing transmitted from the master for more than 4.01ms? Should the counter be kept incrementing till a character is received? Won't the counter overflow if nothing is received from the master?

I may have asked a lot of questions which might be a bit silly. May be I am going in the wrong direction. But a reply from anyone having implemented MODBUS RTU over RS232/RS485 using MSP430G2553/MSP4305438 would be very useful. Please provide a general flow of the program just for the start and stop timing. The rest of the packet and CRC check wont be needed to be explained.

Thanks,

Tanmay Gadekar

  • Tanmay Gadekar said:
    I may have asked a lot of questions which might be a bit silly.

    Not silly, but at the wrong place. Those questions are MODBUS specific and not related to MSP430. You'll find many experienced MSP430 users here, but surely not as many MODBUS experts.

    You'll surely find help here if you know the implementation details of MODBUS but require help to implement them on MSP430 (like how to do a certain delay, or to check whether a byte has been sent etc.)

    But you will be really lucky if you find someone here who can help you with protocol details.

  • Hi,

    The secret here is to set up a buffer for the received characters with 256 bytes.

    At start simply wait for reception of a character and put it in the buffer at position zero, increment the buffer index and start your timer. 

    Now each time a character is received use the Rx ISR to add the character to the buffer, and RESTART the timer. Whilst the command is being received, the timer will get restarted before it times out.

    Once the complete command is received the timer will time out, so use the Timer ISR to set a flag. The main loop detects this flag and then looks at what is now in the buffer. If the first char is the correct address you can check the command and CRC  and then send the response. Once done reset the buffer index to zero and wait for next Rx character.

    Roy

  • Thanks. I will try out this technique and will keep you posted about the results.

    Regards,

    Tanmay

  • Hi, I finally managed to make modbus rtu work. I have tested the code for function codes 01,03,06 and 05. There is a minor problem I am facing now. When using function code 06, when I want to write data 0000 at address 0000, the slave doesn't receive 8 bytes all the time. Sometimes it receives just 7 bytes and a timeout error is processed by the master(using Modbus poll sw). However if I write data 0001 at address 0000 or if I write data 0000 at address 0001, the slave receives 8 bytes of query all the time and the code works properly. Writing 0 at address 0 is tge only condition whuch doesn't seem to be working. Please suggest if anything is wrong,because all other conditiins are perfectly working. Thanks, Tanmay
  • This sounds as if you have a timing problem.

    Sending a 0x00 (or 0x01), you have a lot of '0' bits in a row. Start bit and all following data bits are 0 until the stop bit. Some UARTs re-synchronize their receive timing at the edge of a bit change. If there are none, they may run off and miss a bit. This can happen if the baudrate timing of both peers together is off by >5% (1/2 bit).

    You could check the send timing of both peers with a scope and compare the duration of a bit to verify this as the source of your problem. If so, correct the timing: calibrate your clock(s) or use a crystal.

  • Hey Tanmay please share how did you implement 3.5 Char delay in the Modbus RTU protocol. (Just the algo in brief )

  • A delay of 3.5 char is 35 bits. The baudrate is known. Implementing a delay can be done with a timer that runs on the same clock as the UART and using 35* the UART baudrate divider as delay constant.
    If a delay is needed when sending, the delay can be started with the TX interrupt for the last byte sent.In this case, the delay needs to be 45 bit lengths, as the last byte (10 bit) is stiil to be sent.

**Attention** This is a public forum