Hello,
I am trying to write to device connected via a serial port. It uses 8 data bits, 1 stop bit, even parity and a communication rate of 2400bps. I am developing in Linux.
I need to send message 0x80007F23FF.
But "it doesn't work" (I know that's vague, but I don't know how to check what's wrong - for example, what message was received (and if it was received). I just don't get the desired output (which would be to turn a light on on the device).
Below is the relevant code I am using.
Thanks in advance
Code:
int fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
fcntl(fd, F_SETFL, 0);
// Set options for the serial port
struct termios options;
tcgetattr(fd, &options);
// Set the baud rate to 2400
cfsetispeed(&options, B2400);
cfsetospeed(&options, B2400);
// Set even parity checking
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_iflag |= (INPCK | ISTRIP);
// Enable the receiver and set local mode
options.c_cflag |= (CLOCAL | CREAD);
// Choose raw input
options.c_lflag &= ~(ICANON | ECHO | ECHOE | IEXTEN | ISIG);
// Choose BRKINT and ICRNL options
options.c_iflag &= ~(BRKINT | ICRNL | IMAXBEL);
// Choose raw output
options.c_oflag &= ~(OPOST | ONLCR);
// Set the new options for the port
tcsetattr(fd, TCSANOW, &options);
// Try to send a message
unsigned char msg[5] = {0x80, 0x00, 0x7F, 0x23, 0xFF};
write(fd, &msg, 8);