0

Context

I am working on a project whereby I wish to expose a MODBUS compliant interface to a .NET object. In order to do so, I need to be able to extract "RTU Message Frames" from a serial port's data stream. The specification dictates that frames are delimited by a "silent interval of at least 3.5 character times", whereby I understand a character to be a 4–bit hexadecimal character.

Framing Specification

Question

Is there a possible approach, using System.IO.Ports, to reliably detect and delimit such frames?

The reading I have done suggests "no", a lower level implementation using the Win32 API would be required. However, I'd be very grateful for recommendations/ideas. Thank you.

George Kerwood
  • 1,248
  • 8
  • 19
  • The spacing is done inside UART and you do not need to do anything except if there is an option in UART to change spacing. – jdweng Sep 15 '22 at 17:09
  • You may need to change ICHAR_GAP . See https://support.industry.siemens.com/forum/ww/en/posts/modbus-rtu-charachter-spacing/267812/?page=0&pageSize=10&force_isolation=true – jdweng Sep 15 '22 at 17:17
  • 1
    A lot of implementations appear to just store all received data in a buffer and check if the buffer contains a valid message each time a new byte is received (using the CRC, perhaps trying different start points in case you have part of a previous message). It is possible to look for the silent interval but note that, at rates over 19200bps, it's 1.750ms and dealing with such short time-frames can be difficult. – Brits Sep 16 '22 at 01:19
  • Thanks @Brits. The more I've thought about it the more I also agree that the delimitation is far to "low level" to work with. We're talking edges and clock cycles at that point. As you say, the CRC could be key to algorithmically identifying frames, but searching for a span of unknown length and unknown start/end could quite quickly become expensive and I'd worry about collisions. I think I'll be forced to assume that the first received byte is the address and respect the subsequent length/quantity fields to extract the CRC and terminate. – George Kerwood Sep 16 '22 at 09:42
  • "searching for a span of unknown length" you don't really need to do this. A packet to a slave will always start with it's ID followed by a function; Master should know what slaves it is expecting a response from and possible values for the function code. This means you will only get as far as checking a CRC occasionality (and CRC calculation is not a big issue unless you are very resource constrained). – Brits Sep 19 '22 at 01:52
  • If you're check the CRC at all you're excepting that the data may be corrupted or incomplete. Any robust implementation has to surely consider that the first byte receive may not in fact be address or the function code. However, as I said, I'll for now just assume the first byte IS the address and the second IS the function code and size accordingly. – George Kerwood Sep 19 '22 at 08:04
  • The "silent interval" is simply an *idle* line at the marking level. That is not equivalent to a "*null terminated message*". "*I understand a character to be a 4–bit hexadecimal character*" -- Your understanding is incorrect; a (message) character is an 8-bit byte, and represented as *two* hex digits. IMO not validating every received message is a cheap, sloppy implementation. – sawdust Sep 20 '22 at 06:54
  • @sawdust Thank you for marking the duplicate, it is indeed the same thing and your answer is exactly a lined with what I'd imagined to be the case. Regarding characters however, I was referring the the modbus specification in which it is written, "each 8–bit byte in a message contains two 4–bit hexadecimal characters" - MODBUS over Serial Line Specification and Implementation Guide V1.02 Sec. 2.5.1. – George Kerwood Sep 20 '22 at 17:55
  • 1
    @GeorgeKerwood - That's really confusing wording in the Modbus spec; it's overuse of the word "character". Since it's number representation, hex "digit" would be better IMO. You might be interested in also reading https://stackoverflow.com/questions/62352322/does-modbus-rtu-require-a-gap-between-characters-when-transmitting/62354210#62354210 Note that I had included a link to the Modbus spec. – sawdust Sep 20 '22 at 19:07
  • @sawdust Fully agreed. Thank you for your time and the additional reference. – George Kerwood Sep 20 '22 at 19:18

0 Answers0