Just quickly coming back to this in case it helps someone else.
This didn’t actually fix the issue. It temporarily resolved it by settings some params on the serial port.
After rebooting the pi it was broken again.
I wouldn’t call myself a programmer by any means… so I might be wrong here.
But it turns out the issue is related to running the serial port in canonical vs non-canonical mode.
Freshly booting the pi sets these params on /dev/ttyS0:
user@rpizero2w:~ $ stty -F /dev/ttyS0 -a
speed 9600 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S;
susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho -extproc
Running the nibegw (nibegw -d /dev/ttyS0 -a 10.10.10.10 -vv) makes this:
user@rpizero2w:~ $ stty -F /dev/ttyS0 -a
speed 9600 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S;
susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 1;
-parenb -parodd -cmspar cs8 hupcl -cstopb cread clocal crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl -ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho -extproc
Running the waveshare read python script turns this to:
user@rpizero2w:~ stty -F /dev/ttyS0 -a
speed 9600 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S;
susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O; min = 0; time = 0;
-parenb -parodd -cmspar cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany -imaxbel -iutf8
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase -tostop -echoprt -echoctl -echoke -flusho -extproc
Running the nibegw again after this makes it work (since settings are changed):
user@rpizero2w:~ stty -F /dev/ttyS0 -a
speed 9600 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S;
susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 1;
-parenb -parodd -cmspar cs8 hupcl -cstopb cread clocal crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany -imaxbel -iutf8
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase -tostop -echoprt -echoctl -echoke -flusho -extproc
The hardware-control param (crtscts) is settable through the nibegw script, so I didn’t touch that.
I disabled all the other params in the original C code to match the last state… and guess what… rebooting the pi and starting my modified nibegw (via systemctl) just works.
Strange that no one else has this issue with the product? (I’m rocking rpi zero 2w rev. 1, Waveshare RS485 CAN hat) Should I create a pull request for this or is there another explanation? (@elupus)
For those wondering, replace the initSerialPort (line 95-134) with this:
int initSerialPort(int fd, int hwflowctrl)
{
struct termios options;
// Get the current options for the port...
tcgetattr(fd, &options);
// Set the baud rates
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
// Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);
// 8 data bits, no parity, 1 stop bit
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
if (hwflowctrl)
options.c_cflag |= CRTSCTS; // Enable hardware flow control
else
options.c_cflag &= ~CRTSCTS; // Disable hardware flow control
// Flow control
options.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL );
// Local flags
options.c_lflag &= ~(ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHONL | ECHOCTL | ECHOKE );
options.c_oflag &= ~(OPOST | ONLCR);
options.c_cc[VMIN] = 1; // Min character to be read
options.c_cc[VTIME] = 1; // Time to wait for data (tenth of seconds)
// Set the new options
if (tcsetattr(fd, TCSANOW, &options) < 0 )
{
return -1;
}
return 0;
}
Extra info: (removed some as I cannot post more then 2 links yet)
https://www.gnu.org/software/libc/manual/html_node/Local-Modes.html