I run a primitive code on Teensy 2.0, which is just to blink a LED and transmit counter value to usb serial port every time it gets an 'r' command from the C program which sends the command and reads the value from usb.
I run those on two identical(?) Xubuntus (uname -a: Linux XXX 4.4.0-112-generic #135-Ubuntu SMP Fri Jan 19 11:48:36 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux) geting suprisingly different results.
Teensy code works perfect with Arduino IDE Serial Monitor on both machines as well as the commands: echo -n 'r' >/dev/teense and tail -f /dev/teensy.
The problem appears when to use a C application or the commands: echo -n 'r' >/dev/teense; cat /dev/teensy. They both work correctly on first machine and incorrectly (return only every second (!) value) on the second one.
Machine XXX1 (as expected):
Machine XXX2 (unexpected behavior):
I tried many serial port settings in the C code and/or set with stty (btw stty -a -F /dev/teensy looks the same on both mashines) with no success.
Can you point any reason in the code or system settings for this different behavior.
The Teensy code is as follows:
The C code:
I run those on two identical(?) Xubuntus (uname -a: Linux XXX 4.4.0-112-generic #135-Ubuntu SMP Fri Jan 19 11:48:36 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux) geting suprisingly different results.
Teensy code works perfect with Arduino IDE Serial Monitor on both machines as well as the commands: echo -n 'r' >/dev/teense and tail -f /dev/teensy.
The problem appears when to use a C application or the commands: echo -n 'r' >/dev/teense; cat /dev/teensy. They both work correctly on first machine and incorrectly (return only every second (!) value) on the second one.
Machine XXX1 (as expected):
HTML:
XXX1:~$ echo -n 'r' >/dev/teensy; cat /dev/teensy
200
XXX1:~$ echo -n 'r' >/dev/teensy; cat /dev/teensy
201
XXX1:~$ echo -n 'r' >/dev/teensy; cat /dev/teensy
202
XXX1:~$ ./counter
Read bytes: 3 [ 0x32 0x30 0x33] [203]
XXX1:~$ ./counter
Read bytes: 3 [ 0x32 0x30 0x34] [204]
XXX1:~$ ./counter
Read bytes: 3 [ 0x32 0x30 0x35] [205]
XXX1:~$ ./counter
Machine XXX2 (unexpected behavior):
HTML:
XXX1:~$ echo -n 'r' >/dev/teensy; cat /dev/teensy
200
XXX1:~$ echo -n 'r' >/dev/teensy; cat /dev/teensy
XXX1:~$ echo -n 'r' >/dev/teensy; cat /dev/teensy
202
XXX1:~$ echo -n 'r' >/dev/teensy; cat /dev/teensy
XXX1:~$ echo -n 'r' >/dev/teensy; cat /dev/teensy
XXX1:~$ ./counter
Read bytes: 3 [ 0x32 0x30 0x34] [204]
XXX1:~$ ./counter
Read bytes: 0 [] []
XXX1:~$ ./counter
Read bytes: 3 [ 0x32 0x30 0x36] [206]
XXX1:~$ ./counter
Read bytes: 0 [] []
I tried many serial port settings in the C code and/or set with stty (btw stty -a -F /dev/teensy looks the same on both mashines) with no success.
Can you point any reason in the code or system settings for this different behavior.
The Teensy code is as follows:
Code:
#define LEDPIN 11
int counter;
void setup()
{
pinMode(LEDPIN, OUTPUT);
counter = 200;
Serial.begin(115200);
while (!Serial) ;
}
void loop()
{
if ( Serial.available() > 0 )
{
switch ( Serial.read() )
{
case 'r':
Serial.print(counter++);
break;
default:
Serial.print("error");
break;
}
digitalWrite(LEDPIN, HIGH);
delay(400);
digitalWrite(LEDPIN, LOW);
delay(100);
}
}
Code:
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
int set_interface_attribs(int fd)
{
struct termios tty;
if (tcgetattr(fd, &tty) != 0) {
printf("Error from tcgetattr: %s\n", strerror(errno));
return -1;
}
tty.c_cflag |= (CLOCAL | CREAD);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
tty.c_cflag &= ~(PARENB | CSTOPB | CRTSCTS);
tty.c_lflag &= ~(ICANON | ECHO | ECHONL | ISIG | IEXTEN);
tty.c_lflag &= ~(NOFLSH | FLUSHO);
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXOFF | IXANY);
tty.c_oflag &= ~OPOST;
/* fetch bytes as they become available */
tty.c_cc[VMIN] = 0;
tty.c_cc[VTIME] = 20; // read czeka do 5 sekund na pojawienie się danych
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
printf("Error from tcsetattr: %s\n", strerror(errno));
return -1;
}
return 0;
}
#define MAXBUF 16
int main(int argc, char *argv[])
{
char *portname = "/dev/teensy";
int fd;
char buf[MAXBUF];
int rdlen;
if ( (fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC)) < 0 )
{
printf("Error opening %s: %s\n", portname, strerror(errno));
return(EXIT_FAILURE);
}
set_interface_attribs(fd);
if ( write(fd, "r", 1) != 1 )
{
printf("Error from write: %d\n", errno);
return(EXIT_FAILURE);
}
if ( (rdlen = read(fd, buf, sizeof(buf) - 1)) >= 0 )
{
buf[rdlen] = '\0';
printf("Read bytes: %d [", rdlen);
for (int i = 0; i < rdlen; i++) printf(" 0x%x", buf[i]);
printf("] [%s]\n", buf);
}
else
{
printf("Error from read: %d: %s\n", rdlen, strerror(errno));
}
close(fd);
return(EXIT_SUCCESS);
}