Thread: Serial Port Questions

  1. #1
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    RING 0

    Serial Port Questions

    Hey, I had a few questions about programming with serial ports if you don't mind.

    I'm pretty experienced at writing sockets and full on servers with internet protocols but lately I have been playing around with serial port communication amongst my PC and various microcontrollers. I had a few questions about guaranteeing data transmission and such.

    1) When dealing with TCP I know there is a client connected before I start transmitting. With a serial port how do I know I can transmit to anybody? Does the client usually send some special packet to the serial host to let them know hey i'm listening? Then what about dealing with disconnects/reconnects?

    2) What are "heartbeats" that I keep hearing about when people talk about serial communications?

    3) Maybe this is unlikely but I was just curious...if a host is sending mid packet and I plugin the serial cable, what is to stop the data from being totally corrupted from the client starting to read "mid byte" ?

    Thanks for any help/info/tips on dealing with serial ports that you can offer.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    1) Generally, you'd send a message out (once every few seconds or so) to say "Hello, I'm here, anyone else out there?". If there is anyone on the other end, that end says "Yes, I'm here" - perhaps you also want to add "my name is xxxx" to the respective messages.

    If you have a "client/server" system (rather than one of "all equals/peer-to-peer"), then you may have the master (or client) say "Any out there?", where the other end never sends anything unless specifically asked. This can make the system more robust and simpler to design, since you don't have to deal with "what happens if both are trying to connect at the same time" type scenarios, or messages going both ways at the same time.

    2) If the line is idle for a long time, you may need to send a "are you still there" message from time to time (e.g. once every 5 seconds). If you get a "I'm still here" back, then all is ok. If you don't get anything back, then you go back to 1.

    3) If all packets start and end with a certain character [which means you can't have that character in the middle of a packet, you either need to pick carefully, or figure out a way to "escape" those characters - a bit like control characters and \ in C strings], then the receiving side will discard anything received UNTIL the "start of packet" message comes in.

    You may also want to have a length in the packet header, and a checksum of the packet, either as part of the header of the packet or at the end.

    Finally, you probably want a "Thanks, got that" and "Say again" messages to ACK and NAK messages.

    I'm sure there are links out there, but when I was doing this, there were about three people with Internet Browsers in the 200+ staff company I worked for.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    RING 0
    Much thanks, I'll definently be playing with these concepts this weekend!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. serial port to poll on request
    By infineonintern in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 06:52 AM
  2. Can't Read From Serial Port
    By HalNineThousand in forum Linux Programming
    Replies: 14
    Last Post: 03-20-2008, 05:56 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Reading and writing to a serial port
    By SwarfEye in forum C Programming
    Replies: 2
    Last Post: 08-18-2006, 12:28 AM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM