Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <unistd.h>
- #include <errno.h>
- void main(void)
- {
- int fd;
- fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY); //FileDescriptor fd = ttyUSB0 (CP2102)
- if (fd<0) {
- printf("Failed to open ttyUSB0\n");
- close(fd);
- return; //Wenn ttyUSB0 nicht geöffnet werden konnte, return
- }
- struct termios SerialPortSettings;
- tcgetattr(fd, &SerialPortSettings);
- //Baudrate setzen 9600 bps
- cfsetispeed(&SerialPortSettings, B9600);
- cfsetospeed(&SerialPortSettings, B9600);
- SerialPortSettings.c_cflag &= ~PARENB; //No Parity
- SerialPortSettings.c_cflag &= ~CSTOPB; //1 Stopbit
- SerialPortSettings.c_cflag &= ~CSIZE; //Clear Mask
- SerialPortSettings.c_cflag |= CS8; //Databits = 8
- SerialPortSettings.c_cflag &= ~CRTSCTS; //Turn off hardware flow control
- SerialPortSettings.c_cflag |= CREAD | CLOCAL; //Turn on rx
- SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY); //Turn off software flow control
- SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG); //Set Non-Cannoncial
- SerialPortSettings.c_oflag &= ~OPOST;
- if((tcsetattr(fd,TCSANOW,&SerialPortSettings)) != 0) { //Set attributes
- printf("Failed to set attributes!");
- close(fd);
- return;
- }
- char write_buffer[2];
- write_buffer[0] = 'C';
- write_buffer[1] = '\n';
- int bytes_written = 0;
- bytes_written = write(fd,write_buffer,sizeof(write_buffer));
- printf("\n %s written to ttyUSB0",write_buffer[0],write_buffer[1]);
- printf("\n %d Bytes written to ttyUSB0", bytes_written);
- close(fd);
- }
Add Comment
Please, Sign In to add comment