C programming serial ports

In summary, the serial port is an interface that has been around for a while and uses a simple protocol. You need to configure the data rate, start/stop bits, and parity. You can use the serial port to communicate with a communications chip to drive a serial port chip.
  • #1
Grotesque Puppet
41
0
In my C book it says that the serial port is stdaux, but when I make a program to send data to stdaux the compiler (Dev-C++ in Windows and gcc in linux) says that stdaux doesn't exist :frown: Can anyone explain or point me toward a good tutorial about using the serial ports in C?

Thanks
 
Computer science news on Phys.org
  • #3
using serial ports

Hi

The serial port has been around and in use over 50 years. Some of the standards may seem odd, like signal names? why all those pins if we only use 3? Whats with data set ready, CTS, DCD? Realize those were for older systems and teletype equipment.

What we use today is a simple interface, it assumes the cable is fine and it tends to easily spout data across.

From the program, what you need to do is configure and talk to the communications chip that drives the serial port, it handles moving the data.

You have to set the data rate, start/stop bits usually 9600 baud, 8 bits no parity 1 stop, so its 9600, 8, N, 1 almost all the time.

To approach 100K speeds it can take some extra work; but generally I find it easiest to begin at 9600 to establish the link, then try for higher data rates.

Your program will have to have some include file like a .h to "drive" the serial port chip. you could call the stdaux function, which would then have to figure out how to program/talk to the serial port chip. COM1/2 are fixed addresses in a PC, locked into the hardware. If I recall you get 8 bytes to address, one of those is the actual data, another is a status byte, another a config byte? something like that.

You would, depending on your include file, either write bytes or write to a buffer area (or read) The functionality comes from that file, you could write your own but there should be great ones out in virtual cyberspace
 
Last edited:

1. What is a serial port in C programming?

A serial port is a communication interface that allows data to be transmitted one bit at a time. In C programming, it is used to connect devices such as modems, printers, and microcontrollers to a computer.

2. How do I open a serial port in C programming?

To open a serial port in C programming, you need to use the open() function with the appropriate port name and flags. The port name can vary depending on the operating system, but it is typically something like /dev/ttyS0 for Linux or COM1 for Windows.

3. How do I read data from a serial port in C programming?

To read data from a serial port in C programming, you can use the read() function. This function takes in the file descriptor for the port, a buffer to store the data, and the number of bytes to read. It will return the number of bytes actually read.

4. How do I write data to a serial port in C programming?

To write data to a serial port in C programming, you can use the write() function. This function takes in the file descriptor for the port, a buffer containing the data to be written, and the number of bytes to write. It will return the number of bytes actually written.

5. How do I close a serial port in C programming?

To close a serial port in C programming, you can use the close() function. This function takes in the file descriptor for the port and releases any resources associated with it. It is important to close the port when you are finished using it to avoid any potential issues or conflicts.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Replies
3
Views
16K
Replies
2
Views
5K
  • Computing and Technology
Replies
18
Views
1K
  • Programming and Computer Science
2
Replies
69
Views
4K
Replies
10
Views
2K
  • Programming and Computer Science
Replies
6
Views
986
  • Computing and Technology
Replies
4
Views
18K
  • Electrical Engineering
Replies
6
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
Back
Top