My simple Serial Port code doesnt work. Not sure if its my code, or my USB adapter. Can someone try it / tell me what ive done wrong?

#include <stdio.h>   /* Standard input/output definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <termios.h> /* POSIX terminal control definitions */

// Note: Device files in UNIX are usually not accessable by normal users.
#define MODEMDEVICE "/dev/ttyS0"
char* port = MODEMDEVICE;

     * 'open_port()' - Opens serial port 1 (dev/ttyS0 under Linux)
     * Returns the file descriptor "fd" upon success, or -1 in case of error.

open_port(char* port)
    int fd; // File descriptor for the port.

    // O_NOCTTY as we dont want to be the "controlling terminal" for that port.
    // O_NDELAY we dont care whether the other end of the port is up and running (i.e. if a Data Carrier Detect (DCD) signal is present)
    fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY); // Open Read/Write

    if (fd == -1)
        // Could not open the port.
        perror("Error in function open_port(): Unable to open serial port at /dev/ttyS0 - ");
        fcntl(fd, F_SETFL, 0);
        printf("Port Opened. File Descriptor = %d \n",fd);

    return (fd);

// Set 9600 8N1.
int configure_port(int fd)      // configure the port
    struct termios port_settings;      // structure to store the port settings in

    cfsetispeed(&port_settings, B9600);    // set baud rates
    cfsetospeed(&port_settings, B9600);

    port_settings.c_cflag &= ~PARENB;    // set no parity, stop bits, data bits
    port_settings.c_cflag &= ~CSTOPB;
    port_settings.c_cflag &= ~CSIZE;
    port_settings.c_cflag |= CS8;

    tcsetattr(fd, TCSANOW, &port_settings);    // apply the settings to the port
    printf("Port Configured\n");

write_data(int fd)
    // TODO!
    int wr = write(fd,"AT\r",3); // send an AT command to get its attention, followed by a CR
    printf("Bytes written: %d \n",wr);

read_data(int fd)
    // This is trickier. In raw data mode, each read() will return the number of characters available in the input buffer, or block.
    // We want it to return 0 in this case, not block.

    fcntl(fd, F_SETFL, FNDELAY);
    char* buff;
    int rd = read(fd,buff,10);
    printf("Bytes read: %d \n",rd);

close_port(int fd)
    close(fd); // Set the low DTR signal to hang up any modem.
    printf("File Descriptior %d Closed\n",fd);

int main(int argc, char *argv[])
    int fd = open_port(port);
    if (fd != -1)
        printf("Cannot open port.\n");
4 Years
Discussion Span
Last Post by jbennet

My simple Serial Port code doesnt work.

Can you be more specific?

I see a problem at lines 71/72 (buff is not initialized nor allocated); does your program reach that far?


Well I think I'm opening it wrong... As if I don't have any serial ports on a machine, it still returns success (fd of 3, which is greater than -1)

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.