#include <stdio.h>
#include <fcntl.h>
#include <sys/errno.h>
#include <sys/termios.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/filio.h>
#include <sys/ioctl.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/serial/IOSerialKeys.h>
#include <IOKit/IOBSD.h>
int main (int argc, char *argv[]) {
struct termios termiosA;
struct termios termiosB;
struct timeval timeout;
fd_set rds;
fd_set wds;
fd_set exs;
char *devA;
char *devB;
char strA[1024];
char strB[1024];
int modemA;
int modemB;
int baudA;
int baudB;
int fdA;
int fdB;
int ret;
int iA = 0;
int iB = 0;
if (argc != 3 && argc != 5) {
printf("Usage: %s deviceA baudA ?deviceB baudB?\n", argv[0]);
exit(1);
}
devA = argv[1];
baudA = atoi(argv[2]);
printf("Interface A:\n\tDevice = %s\n\tBaud = %i\n", devA, baudA);
if (argc == 5) {
devB = argv[3];
baudB = atoi(argv[4]);
printf("Interface B:\n\tDevice = %s\n\tBaud = %i\n", devB, baudB);
}
printf("\nOpening Interface A...\n");
fdA = open(devA, O_RDWR | O_NONBLOCK);
memset(&termiosA, 0, sizeof(struct termios));
cfmakeraw(&termiosA);
cfsetspeed(&termiosA, baudA); // Set Baud
termiosA.c_cflag |= CS8; // 8 Data Bits
ret = tcsetattr(fdA, TCSANOW, &termiosA);
if (ret) {
printf("ioctl (A) - returned %i errno %i\n", ret, errno);
close(fdA);
exit(1);
}
ioctl(fdA, TIOCMGET, &modemA);
modemA |= TIOCM_DTR;
ioctl(fdA, TIOCMSET, &modemA);
if (argc == 5) {
printf("\nOpening Interface B...\n");
fdB = open(devB, O_RDWR | O_NONBLOCK);
memset(&termiosB, 0, sizeof(struct termios));
cfmakeraw(&termiosB);
cfsetspeed(&termiosB, baudB); // Set Baud
termiosB.c_cflag |= CS8; // 8 Data Bits
ret = tcsetattr(fdB, TCSANOW, &termiosB);
if (ret) {
printf("ioctl (B) - returned %i errno %i\n", ret, errno);
close(fdA);
close(fdB);
exit(1);
}
ioctl(fdB, TIOCMGET, &modemB);
modemB |= TIOCM_DTR;
ioctl(fdB, TIOCMSET, &modemB);
}
printf("Setting up the select loop...\n");
memset(&timeout, 0, sizeof(struct timeval));
timeout.tv_sec = 15;
printf("Starting the select loop for %i & %i.\n", fdA, fdB);
while (1) {
char tmpA[1024];
char tmpB[1024];
memset(tmpA, '\0', 1024);
memset(tmpB, '\0', 1024);
FD_ZERO(&rds);
FD_ZERO(&wds);
FD_ZERO(&exs);
FD_SET(fdA, &rds);
FD_SET(fdA, &wds);
FD_SET(fdA, &exs);
if (argc == 5) {
FD_SET(fdB, &rds);
FD_SET(fdB, &wds);
FD_SET(fdB, &exs);
}
ret = select(5, &rds, &wds, &exs, &timeout);
if (FD_ISSET(fdA, &rds)) {
if (read(fdA, tmpA, 1) < 0) {
printf("errno = %i\n", errno);
break;
}
if (tmpA[0] == '\r' || tmpA[0] == '\n') {
if (iA > 0) {
strA[iA] = '\0';
printf("A(%i) -> %s\n", iA, strA);
memset(strA, '\0', 1024);
iA = 0;
}
continue;
} else {
strA[iA++] = tmpA[0];
tmpA[0] = '\0';
}
}
if (argc == 5 && FD_ISSET(fdB, &rds)) {
if (read(fdB, tmpB, 1) < 0) {
printf("errno = %i\n", errno);
break;
}
if (tmpB[0] == '\r' || tmpB[0] == '\n') {
if (iB > 0) {
strB[iB] = '\0';
printf("B(%i) -> %s\n", iB, strB);
memset(strB, '\0', 1024);
iB = 0;
}
continue;
} else {
strB[iB++] = tmpB[0];
tmpB[0] = '\0';
}
}
if (FD_ISSET(fdA, &wds)) {
printf("Port A ready for writing.\n");
write(fdA, "Test from port A.\r\n", 19);
}
if (argc == 5 && FD_ISSET(fdB, &wds)) {
printf("Port B ready for writing.\n");
write(fdB, "Test from port B.\r\n", 19);
}
if (FD_ISSET(fdA, &exs)) {
printf("Port A had an exception.\n");
}
if (argc == 5 && FD_ISSET(fdB, &exs)) {
printf("Port B had an exception.\n");
}
}
}