#include #include #include #include #include #include #include #include #include #include #include #include /* * Send NULL bytes to serial port. * * Copyright (C) 2018, 2019 R.J. van der Putten, Leiden, Holland, * rob at sput dot nl. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ void hlp(void) { fprintf(stderr, \ "This program sends null chars to a serial port at 115200 bps.\n"); fprintf(stderr, "Usage: sndnul -d Device [-t Seconds]\n"); fprintf(stderr, \ "Seconds should be 0 ... 10 and defaults to 1.\n"); } int main(int argc, char **argv) { int fd, i, j, len, opt, secs, wlen; struct termios newtio, oldtio; char ttysn[256]; len = 11520; char nulchrs[len]; fd = 0; i = 0; j = 0; opt = 0; secs = 1; wlen = 0; memset(&newtio, 0, sizeof(newtio)); memset(&oldtio, 0, sizeof(oldtio)); memset(nulchrs, 0, len); memset(ttysn, 0, 256); while ((opt = getopt(argc, argv, "d:ht:")) != -1) { switch (opt) { case 'd': strncpy(ttysn, optarg, 255); break; case 'h': hlp(); exit(0); case 't': secs = atoi(optarg); break; default: hlp(); exit(1); } } if (secs < 0 || secs > 10) { hlp(); exit(1); } if (ttysn[0] == 0 || strncmp(ttysn, "/dev/", 5) != 0) { hlp(); exit(1); } /* Open tty */ if ((fd = open(ttysn, O_RDWR | O_NOCTTY)) < 0) { perror(ttysn); exit(1); } /* Force blocking IO */ fcntl(fd, F_SETFL, 0); /* save current serial port settings */ if (tcgetattr(fd, &oldtio) < 0) { perror("tcgetattr() failed"); close(fd); exit(1); } /* * Control flags; * 115200 bps, 8 bits, drop DTR when done, * Ignore modem control lines */ newtio.c_cflag = B115200 | CS8 | HUPCL | CLOCAL; /* Input flags */ newtio.c_iflag = IGNPAR; /* Output flags */ newtio.c_oflag = 0; /* Local flags */ newtio.c_lflag = 0; /* initialise all control characters */ newtio.c_cc[VINTR] = 0; /* Ctrl-C */ newtio.c_cc[VQUIT] = 0; /* Ctrl-\ */ newtio.c_cc[VERASE] = 0; /* del */ newtio.c_cc[VKILL] = 0; /* Ctrl-U */ newtio.c_cc[VEOF] = 0; /* Ctrl-D */ newtio.c_cc[VTIME] = 0; /* inter-character timer unused */ newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */ newtio.c_cc[VSWTC] = 0; /* '\0' */ newtio.c_cc[VSTART] = 0; /* Ctrl-Q */ newtio.c_cc[VSTOP] = 0; /* Ctrl-S */ newtio.c_cc[VSUSP] = 0; /* Ctrl-Z */ newtio.c_cc[VEOL] = 0; /* '\0' */ newtio.c_cc[VREPRINT] = 0; /* Ctrl-R */ newtio.c_cc[VDISCARD] = 0; /* Ctrl-O */ newtio.c_cc[VWERASE] = 0; /* Ctrl-W */ newtio.c_cc[VLNEXT] = 0; /* Ctrl-V */ newtio.c_cc[VEOL2] = 0; /* '\0' */ /* now clean the tty and activate the settings for the port */ tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSANOW, &newtio); /* Write null chars to serial port */ while (secs > 0) { i = 0; j = len; /* Bytes in one second */ while (j > 0 && fd > 0) { wlen = write(fd, nulchrs + i, j); if (j > 0 && wlen < 1 && errno != EINTR) { perror("Cant' write to serial port"); secs = 0; break; } else { i = i + wlen; /* Already written */ j = len - i; /* Still to write */ } } secs--; } /* restore the old port settings */ tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSADRAIN, &oldtio); close(fd); return(0); }