Сериен порт

From Ilianko

Цел на упражнението

Преговор типове данни, оператори. Достъп до сериен порт.


Сериен порт


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

int main(void)
{
    int fd; // File descriptor for the port 
    struct termios options; // Serial port settings
	
	
    fd = open( "/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);
	
    if (fd == -1)//Could not open the port.
	perror("open_port: Unable to open port");
    else 
       fcntl(fd, F_SETFL, 0);
	
    tcgetattr(fd, &options);
    cfsetispeed(&options, B2400);
    cfsetospeed(&options, B2400);
    
    options.c_cflag |= ( CREAD);
    options.c_cflag |= ( CLOCAL);
    options.c_cflag &= ( ~CSTOPB);
        
    tcsetattr(fd,TCSANOW, &options);

    write(fd, "a \n", 3); \\write to serial port
           
    return 0;
}