tags:

views:

1438

answers:

6

Hi all, i'm developing an application that reads data from a serial port and sends it over a TCP connection, and vice versa. Unfortunately, while reading data from serial port, it never stops. It does not detect EOF mark, nor EOL or some other special character.

So, how could i detect an end of file (or "end of connection") over serial port in C and Linux?

Thanks in advance.

+6  A: 

Depends on how much control you have over the protocol used for the serial link. Unless the files implicitly include some end-of-file marker (and as I've understood your post they don't), you need to implement some kind of communication protocol in order to transfer files.

Some of the most simple procols used way back in the BBS days were XMODEM and it's derivatives. They may be simple enough for you to use.

If you have a full-blown computer at the other end of the serial line, it would probably be far simpler just to set up an PPP link over the serial line and do the communication over TCP/IP.

andri
A: 

Check your serial port configuration:

stty -F /dev/ttySx -a

Set cooked link instead of raw link:

stty -F /dev/ttySx cooked
saxi
+2  A: 

Serial link only sends bytes. There is no packet frame, no error checking so you can't send files reliably over raw serial link. You need to use some protocol like XMODEM, KERMIT etc.

It's not trivial to implement such protocol. It may be easier to run TCP/IP over the link if the other end is also a computer. Please check out SLIP or PPP.

ZZ Coder
+1  A: 

The serial port gives "end of file" on a hangup condition, which is signalled out-of-band by the modem control lines (dropping of DCD). If you're connected with a null-modem, that'll never happen.

Use a framing mechanism, like the other answers have suggested. You may not need to go the whole hog with something like ZMODEM though - just prefixing your file with the file size and a CRC32 checksum should do, if the link is reasonably error-free and 8-bit clean.

caf
A: 

cheap and dirty solution: on linux, unix or osx just run 'screen -L <serial_device> <baudrate>'. At the other end write the output of your file to the console, in raw mode. -L logs it to a file the contents of which will be the file you transfered from the other end. Check the contents with xxd to verify they match what you had at the source.

cyphunk
+1  A: 

i want to reference code aboute trasnfer file through comport. can anyone give me?thanks

hop