| 120 | } |
| 121 | |
| 122 | void fxOpenSerial(txSerialTool self) |
| 123 | { |
| 124 | int fd = -1; |
| 125 | struct termios options; |
| 126 | CFSocketContext context; |
| 127 | |
| 128 | fd = open(self->path, O_RDWR | O_NOCTTY | O_NONBLOCK); |
| 129 | if (fd == -1) { |
| 130 | usleep(500000); |
| 131 | fd = open(self->path, O_RDWR | O_NOCTTY | O_NONBLOCK); |
| 132 | if (fd == -1) { |
| 133 | fprintf(stderr, "Error opening serial port %s - %s(%d).\n", self->path, strerror(errno), errno); |
| 134 | return; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if (ioctl(fd, TIOCEXCL) == -1) { |
| 139 | fprintf(stderr, "Error setting TIOCEXCL on %s - %s(%d).\n", self->path, strerror(errno), errno); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | tcgetattr(fd, &options); |
| 144 | |
| 145 | options.c_cc[VMIN] = 0; |
| 146 | options.c_cc[VTIME] = 10; |
| 147 | |
| 148 | // baud rate |
| 149 | //cfsetspeed(&options, getBaud(baud)); |
| 150 | // bits per character |
| 151 | options.c_cflag &= ~CSIZE; |
| 152 | if (self->data == 5) |
| 153 | options.c_cflag |= CS5; |
| 154 | else if (self->data == 6) |
| 155 | options.c_cflag |= CS6; |
| 156 | else if (self->data == 7) |
| 157 | options.c_cflag |= CS7; |
| 158 | else if (self->data == 8) |
| 159 | options.c_cflag |= CS8; |
| 160 | |
| 161 | // parity |
| 162 | if (self->parity == 'N') |
| 163 | options.c_cflag &= ~(PARENB | PARODD); |
| 164 | else { |
| 165 | options.c_cflag |= PARENB; |
| 166 | if (self->parity == 'O') |
| 167 | options.c_cflag |= PARODD; |
| 168 | else if (self->parity == 'E') |
| 169 | options.c_cflag &= ~PARODD; |
| 170 | } |
| 171 | // stop bits |
| 172 | if (self->stop == 1) |
| 173 | options.c_cflag &= ~CSTOPB; |
| 174 | else if (self->stop == 2) |
| 175 | options.c_cflag |= ~CSTOPB; |
| 176 | |
| 177 | // Cause the new options to take effect immediately. |
| 178 | if (tcsetattr(fd, TCSANOW, &options) == -1) { |
| 179 | fprintf(stderr, "Error setting tty attributes %s - %s(%d).\n", self->path, strerror(errno), errno); |
no test coverage detected