Serial Port Communication in C on Linux: Read and Write Data

├Źndice
  1. Introduction
  2. Serial Port Communication Basics
  3. Reading Data from a Serial Port
  4. Writing Data to a Serial Port
  5. Conclusion

Introduction

Serial port communication is a common method for transferring data between electronic devices. In this article, we will discuss how to read and write data from a serial port in C on Linux.

Serial Port Communication Basics

A serial port is a communication interface that sends and receives data one bit at a time. Each device has a unique address, or port number, which is used to establish communication between devices.

To communicate with a serial port in C on Linux, we need to use the termios library. This library provides functions to configure the serial port, and to read and write data from it.

Reading Data from a Serial Port

To read data from a serial port, we need to open the port using the `open()` system call. We also need to configure the port using the `tcgetattr()` and `tcsetattr()` functions from the termios library.

Once the port is open and configured, we can use the `read()` system call to read data from the port. The `read()` function takes three parameters: the file descriptor of the port, a buffer to store the data, and the number of bytes to read.

Here's an example code snippet to read data from a serial port:


#include <stdio.h>
#include <fcntl.h>
#include <termios.h>

int main()
{
    int fd;
    char buffer[255];
    struct termios serial;

    fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
    tcgetattr(fd, &serial);
    serial.c_cflag = B9600 | CS8 | CREAD;
    tcsetattr(fd, TCSANOW, &serial);

    while(1)
    {
        int n = read(fd, buffer, sizeof(buffer));
        buffer[n] = '';
        printf("%s", buffer);
    }

    close(fd);
    return 0;
}

This code reads data from the serial port /dev/ttyS0 in a loop, and prints it to the console.

Writing Data to a Serial Port

To write data to a serial port, we need to open the port, configure it, and then use the `write()` system call to send data to the port. The `write()` function takes three parameters: the file descriptor of the port, a buffer containing the data to send, and the number of bytes to send.

Here's an example code snippet to write data to a serial port:


#include <stdio.h>
#include <fcntl.h>
#include <termios.h>

int main()
{
    int fd;
    char buffer[] = "Hello, world!";
    struct termios serial;

    fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
    tcgetattr(fd, &serial);
    serial.c_cflag = B9600 | CS8 | CREAD;
    tcsetattr(fd, TCSANOW, &serial);

    write(fd, buffer, sizeof(buffer));

    close(fd);
    return 0;
}

This code sends the string "Hello, world!" to the serial port /dev/ttyS0.

Conclusion

In this article, we've discussed how to read and write data from a serial port in C on Linux. We used the termios library to configure the port and communicate with it using the `read()` and `write()` system calls.

Serial port communication is an important topic in embedded systems and IoT applications. With the knowledge gained from this article, you can start building your own serial port communication applications on Linux.

Click to rate this post!
[Total: 0 Average: 0]

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up

Below we inform you of the use we make of the data we collect while browsing our pages. You can change your preferences at any time by accessing the link to the Privacy Area that you will find at the bottom of our main page. More Information