-
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.
-
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.
--
Mats
-
Much thanks, I'll definently be playing with these concepts this weekend!