Advertisement
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); //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;
- SerialPortSettings.c_cc[VTIME] = 100;
- if((tcsetattr(fd,TCSANOW,&SerialPortSettings)) != 0) { //Set attributes
- printf("Failed to set attributes!");
- close(fd);
- return;
- }
- tcflush(fd, TCIFLUSH);
- char read_buffer[64];
- int bytes_read = 0;
- bytes_read = read(fd,&read_buffer,64);
- printf("\n\n Bytes Rxed %d", bytes_read);
- printf("\n\n ");
- int i = 0;
- for(i=0;i<bytes_read;i++)
- printf("%c",read_buffer[i]);
- close(fd);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement