aboutsummaryrefslogtreecommitdiffstats
path: root/sercp.c
diff options
context:
space:
mode:
Diffstat (limited to 'sercp.c')
-rw-r--r--sercp.c47
1 files changed, 36 insertions, 11 deletions
diff --git a/sercp.c b/sercp.c
index f85871b..f19fea4 100644
--- a/sercp.c
+++ b/sercp.c
@@ -18,6 +18,10 @@
/*
* IFDEFS inspired here:
* https://iq.opengenus.org/detect-operating-system-in-c/
+ *
+ * vmin, vtime insipired here:
+ * // http://unixwiz.net/techtips/termios-vmin-vtime.html
+ *
*/
#ifdef _WIN32
@@ -162,7 +166,7 @@ void TestArgs (int argc, char *argv[])
};
c = getopt_long (argc, argv, "d:b:w:rvh", long_options, &option_index);
if (c == -1) {
- // konec parametru
+ // end of arguments
break;
}
switch (c) {
@@ -172,7 +176,6 @@ void TestArgs (int argc, char *argv[])
#else
sprintf(SERIALDEVICE, "%s", optarg);
#endif
-// printf ("Serial port: %s\n", SERIALDEVICE);
break;
case 'b':
baud_rate = atoi(optarg);
@@ -656,8 +659,10 @@ void sercpSend(void) {
}
}
printf("\nFile sent...\n");
- // FIXME na __APPLE__ tady pomaha pockat
- // sleep_ms(5000);
+#ifdef __APPLE__
+ // FIXME on __APPLE__ this waiting helps
+ sleep_ms(wait_ms);
+#endif
fclose(tap_fd);
}
@@ -705,11 +710,30 @@ int OpenUart() {
#else
struct termios oldtio, newtio;
/* open the device to be non-blocking (read will return immediatly) */
- serial_fd = open(SERIALDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK | O_NDELAY);
+ serial_fd = open(SERIALDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK); // | O_NDELAY
if (serial_fd < 0) {
perror(SERIALDEVICE);
return(-1);
}
+#ifdef __APPLE__
+ // Note that open() follows POSIX semantics: multiple open() calls to the same file will succeed
+ // unless the TIOCEXCL ioctl is issued. This will prevent additional opens except by root-owned
+ // processes.
+ // See tty(4) <x-man-page//4/tty> and ioctl(2) <x-man-page//2/ioctl> for details.
+ if (ioctl(serial_fd, TIOCEXCL) == -1) {
+ printf("Error setting TIOCEXCL on %s - %s(%d).\n", SERIALDEVICE, strerror(errno), errno);
+ return (-1);
+ }
+
+ // Now that the device is open, clear the O_NONBLOCK flag so subsequent I/O will block.
+ // See fcntl(2) <x-man-page//2/fcntl> for details.
+ if (fcntl(serial_fd, F_SETFL, 0) == -1) {
+ printf("Error clearing O_NONBLOCK %s - %s(%d).\n", SERIALDEVICE, strerror(errno), errno);
+ return (-1);
+ }
+
+
+#endif
tcgetattr(serial_fd, &oldtio); /* save current port settings */
bzero(&newtio, sizeof (newtio));
cfmakeraw(&newtio);
@@ -743,19 +767,21 @@ int OpenUart() {
newtio.c_cflag = B38400;
break;
}
- cfsetspeed(&newtio, baud_rate);
+ cfsetospeed(&newtio, baud_rate);
+ cfsetispeed(&newtio, baud_rate); // set ispeed same as ospeed (see POSIX)
#if defined (__APPLE__) || defined (BSD)
-// newtio.c_cflag |= CS8 | CLOCAL | CREAD | CCTS_OFLOW | CRTS_IFLOW;
- newtio.c_cflag |= CS8 | CLOCAL | CREAD;
+ newtio.c_cflag |= CS8 | CLOCAL | CREAD | CCTS_OFLOW | CRTS_IFLOW | HUPCL;
#else
newtio.c_cflag |= CS8 | CLOCAL | CREAD | CRTSCTS;
#endif
newtio.c_iflag &= ~(IXON | IXOFF | IXANY);
newtio.c_oflag &= ~(OPOST);
+ newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // raw input
printf ("Serial device: %s, communication speed is: %d Bd\n", SERIALDEVICE, baud_rate);
+
// http://unixwiz.net/techtips/termios-vmin-vtime.html
- newtio.c_cc[VMIN] = 0;
- newtio.c_cc[VTIME] = 0;
+ newtio.c_cc[VMIN] = 1; // minimum number to read
+ newtio.c_cc[VTIME] = 1; // time to wait
tcsetattr(serial_fd, TCSANOW, &newtio);
sleep_ms(20);
tcflush(serial_fd, TCIOFLUSH);
@@ -765,7 +791,6 @@ int OpenUart() {
-
/************************************************************************/
/************************************************************************/
int main(int argc, char** argv, char** env)