Here is my code, but I am unable to get the device to respond correctly. Anyone notice any problems with this code? I’m trying to write a command consisting of 3 numbers to a serial port on a circuit board to control a relay. The board is using a Digi ethernet interface.
fd = open( portAddress, O_RDWR | O_NOCTTY | O_NDELAY );
//check if serial port opened properly
if (fd == -1 ) {
syslog(LOG_INFO, "open_port: Unable to open %s", portAddress );
printf("open_port: Unable to open %s", portAddress);
return 0;
} else {
//set the port read function to return 0 if no data is available to read
fcntl(fd, F_SETFL, FNDELAY);
//get the current serial port settings, so we can restore them later
tcgetattr(fd, &oldtio);
tcgetattr(fd, &newtio);
//set the speed of the serial port
cfsetispeed(&newtio, B115200); //input speed
cfsetospeed(&newtio, B115200); //also output speed
newtio.c_cflag &= ~PARENB; //no parity
newtio.c_cflag &= ~CSTOPB; //1 stop bit
newtio.c_cflag &= ~CSIZE; //clear data size bits
newtio.c_cflag |= CS8; //set data size to 8 bits
newtio.c_cflag |= (CLOCAL);
newtio.c_cflag &= ~CRTSCTS; //no flow control
newtio.c_cflag &= ~(IXON | IXOFF);
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE |ISIG); //raw input, disable canonical input, no echo, no signals
newtio.c_lflag &= ~(INPCK | ISTRIP); //no parity check or strip parity bit
newtio.c_oflag = 0;
tcsetattr(fd, TCSAFLUSH, &newtio); //make changes now with in/out flush
char command[ 3 ] = { char(254), char(116), char(1) };
int wr = write(fd, &command, 3);