views:

887

answers:

5

How do i read data from serial port using C ? and then again transfer the data to modem ? I am using RS 232 cable for serial communication ...

+2  A: 

First of all this is very dependant on OS you use.

For *nix type you usually open serial port device like normal file (/dev/ttyS0, for example), and start reading to or writing from it. As for windows, i can give you advise to search for a reliable library, because windows serial communication can be a real nightmare. Even more, there may be some differences between old windows versions and never ones.

Personally, because i'm wxWidgets user i use wxCTB. You should look for library that will suit IDE you are using.

some free of charge links ;) www.robbayer.com/files/serial-win.pdf www.lookrs232.com/com_port_programming/

Andrejs Cainikovs
Not so applicable for embedded (it's tagged 'embedded').
Craig McQueen
Not true. Embedded *usually* means Linux or WinCE.
Andrejs Cainikovs
+4  A: 

A lot of useful information about dealing with serial ports both on PCs and on the device end of the wire can be found at Jan Axelson's site for the book Serial Port Complete.

Actual interaction with the port will be highly platform-specific. On *nix of most flavors you open a device named something like /dev/ttya and use read(), write(), and ioctl() to read, write, and configure the port.

On Windows, you open a file named COM1 (or \\.\COM1 on some flavors) with the CreateFile() function, and then get it configured and happy with functions like SetCommState() and the DCB structure. The details are similar in broad outline to the things you need to do on *nix, but organized completely differently. You can find the whole discussion of configuring and using COM ports at MSDN.

On the embedded device, you will either be directly accessing the hardware registers of the UART or interacting with an RTOS. Either way, there is essentially no way to make the code on the device end broadly portable.

RBerteig
A: 

In the Windows land, you better find a library that makes all the magic happen.

Personally I'm using Borland C++ to put together small utility to talk to my hardware (bootloaders, configuration and testing) and I'm very happy with the TurboPower Async Professional Library that works both in Delphi and BCC and is open source (Mozilla Public License 1.1).

PFM
+2  A: 

In the embedded world your compiler will have some mechanism (usually compiler specific) that allows it to access the hardware registers that are used to implement the serial port function either within the device or in an external memory or IO mapped device. These register definitions are often provided by the tool or processor manufacturer and it is then just a case of writing the configuration control values to the serial port registers to set its operating mode and then reading the data out of tje receive buffer register.

Sounds simple but, on the MSP-430 mailing list USART programming questions are the most common single topic.

Ian
+1  A: 

To expand on Ian's answer, a typical embedded c compiler will have a compiler directive or a statement whose parameters control settings such as baud rate, and which associates the UART (or other I/O hardware) with a stream. You can specify a stream name (if you have more than one serial port) or use the defaults. Then you can access the serial port with the usual c character routines, and there will be an interrupt when it receives each character. So your interrupt routine reads each character and puts it in a buffer.

Jeanne Pindar