Larus glider flight sensor system 3.9.2024
Software-In-The-Loop test and validation system
Loading...
Searching...
No Matches
USB_serial.cpp
Go to the documentation of this file.
1/***********************************************************************/
25#ifdef _WIN32
26#include "USB_serial.h"
27#include <stdint.h>
28bool open_USB_serial(char *portname) {
29 return false;
30}
31
32bool write_usb_serial(uint8_t *data, unsigned size) {
33 return false;
34}
35
36#else
37
38#include <errno.h>
39#include <fcntl.h>
40#include <string.h>
41#include <termios.h>
42#include <unistd.h>
43#include <stdint.h>
44#include "USB_serial.h"
45
46int fd;
47
48int set_interface_attribs (int fd, int speed, int parity)
49{
50 struct termios tty;
51 if (tcgetattr (fd, &tty) != 0)
52 return -1;
53
54 cfsetospeed (&tty, speed);
55 cfsetispeed (&tty, speed);
56
57 tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit symbols
58 tty.c_iflag &= ~IGNBRK;
59 tty.c_lflag = 0;
60 tty.c_oflag = 0;
61 tty.c_cc[VMIN] = 0;
62 tty.c_cc[VTIME] = 5;
63
64 tty.c_iflag &= ~(IXON | IXOFF | IXANY); // no XON XOFF
65
66 tty.c_cflag |= (CLOCAL | CREAD); // ignore modem controls + enable reading
67 tty.c_cflag &= ~(PARENB | PARODD); // no parity
68 tty.c_cflag |= parity;
69 tty.c_cflag &= ~CSTOPB;
70 tty.c_cflag &= ~CRTSCTS;
71
72 if (tcsetattr (fd, TCSANOW, &tty) != 0)
73 return -1;
74 else
75 return 0;
76}
77
78void set_blocking (int fd, int should_block)
79{
80 struct termios tty_settings;
81 memset (&tty_settings, 0, sizeof tty_settings);
82 if (tcgetattr (fd, &tty_settings) != 0)
83 return;
84
85 tty_settings.c_cc[VMIN] = should_block ? 1 : 0;
86 tty_settings.c_cc[VTIME] = 5; // 5 + 100ms timeout
87
88 tcsetattr (fd, TCSANOW, &tty_settings);
89}
90
91bool open_USB_serial ( char *portname)
92{
93 fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
94 if (fd < 0)
95 return false;
96
97 set_interface_attribs (fd, B115200, 0);
98 set_blocking (fd, 0);
99
100 return true;
101}
102
103bool write_usb_serial( uint8_t * data, unsigned size)
104{
105 if( fd == 0)
106 return 0; // silently give up
107
108 return 0 == write (fd, data, size);
109}
110
111#endif
bool write_usb_serial(uint8_t *data, unsigned size)
bool open_USB_serial(char *portname)
int fd
void set_blocking(int fd, int should_block)
int set_interface_attribs(int fd, int speed, int parity)
Interface to USB -> RS232 (interface)