Thread: Pin level access to serial port?

  1. #1
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413

    Pin level access to serial port?

    Hi, wondering if it's possible to get the current pin state (high/low) of the TX and RX lines of a serial port.

    What I'm aiming to do is make a program that allows me to attach the serial port to an existing communication line and detect baud rate (and maybe parity), then process intercepted Modbus packets to find out other settings.

    Tried googling this, didn't get very far. So far I've only seen that you can get the pin state of the other lines (RTS, CTS, etc.).

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It depends on your OS/Compiler and whatever interfaces the kernel level drivers expose.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    To know the state of the Tx and Rx lines, you check the other lines. To my knowledge this is the only way to do this progamatically. And the reason why you cannot debug serial lines in software.

    Search for Hardware Flow Control for serial lines to get an idea. But in short, when your DTE wants to send data it sets its RTS (Request to send), this will signal the other end of the cable (the DCE). When the DCE is ready to receive the data, it will set its CTS (Clear to Send). This signals the DTE to start sending data. If one of the sides needs to stop the transmission (or it has ended), they will lower their respective line.

    So, to know if Tx or Rx is active you simply look at the RTS on the DTE side and the CTS on the DCE side. If both are active, data is being transmitted. This is at least my understanding. You can use pySerial to do what you want.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Thanks guys. Guess I'm SOL, using half-duplex RS485 so only the TX, RX, and GND lines will be connected; won't be able to use Mario's method.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Epy View Post
    Thanks guys. Guess I'm SOL, using half-duplex RS485 so only the TX, RX, and GND lines will be connected; won't be able to use Mario's method.
    Data is being transmitted on a half-duplex RS485 if the A and B pins have inverted charges. But you know this already. I have no idea how you can check this programatically without a breakout board. (This example may not be ideal since you mentioned Modbus, but you get the idea)
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    RS-485 is generally "half-duplex" by nature (assuming a single pair of communication lines, as indicated), since only one device can transmit at a given time while all other channels simply listen.

    RS-485 communication protocols are remarkably similar to RS-232, except with the addition of an address (and a transmit drive bit when going from "listen" to "talk").

    Have you tried just reading the RX of the serial port while the main system is in communication? Perhaps with a USB RS-232 to RS-485 converter (to prevent bus contention)? This should at least allow you to read the bit patterns received from the source.

  7. #7
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    That would be fine, except baud rate isn't known. The intent of the program is to attach to a pair of lines already communicating and determine slave IDs, baud rate, parity/bit settings, etc.

  8. #8
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    May just end up letting my Arduino Due with TTL to RS485 converter do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 02-10-2009, 11:02 AM
  2. Low-Level hard drive access
    By SgtMuffles in forum C Programming
    Replies: 21
    Last Post: 06-10-2007, 03:03 PM
  3. Low Level Hard Disk Access
    By samGwilliam in forum Tech Board
    Replies: 5
    Last Post: 04-01-2005, 06:14 PM
  4. Low Level Drive Access?
    By coldfusion244 in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2005, 08:19 AM
  5. VC++/NT serial port access
    By danhar707 in forum Windows Programming
    Replies: 1
    Last Post: 11-21-2002, 12:28 PM

Tags for this Thread