NetBSD-Users archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Arduino connect to NetBSD box woes
Hello List,
I am using NetBSD 7.1.2 released (March 15, 2018) i386 on a box and
noticed some serious issue with the Arduino and other dongles.
I have to reconnect the dongle to get the device "/dev/dtyU0"
accessible.
This is quite annoying since it would mean that at every reboot I have
to go to the machine and physically unplug and replug the Arduino.
I have another issue with a code which is 100% working on Linux but
fails to work in 1 way on NetBSD:
int serialport_open(const char *serialport)
{
struct termios toptions;
int fd;
fd = open(serialport, O_RDWR | O_NOCTTY);
if (fd == -1)
{
fprintf(stderr, "Serial port %s cannot be opened ... [ ERROR ]\n",
serialport);
return -1;
}
printf("Serial port 1 ... [ OPENED ]\n");
/* get current serial port settings */
if (tcgetattr(fd, &toptions) < 0) {
perror("serialport_init: Couldn't get term attributes");
return -1;
}
/* set 9600 baud both ways */
cfsetispeed(&toptions, B9600);
cfsetospeed(&toptions, B9600);
/* 8 bits, no parity, no stop bits */
toptions.c_cflag &= ~PARENB;
toptions.c_cflag &= ~CSTOPB;
toptions.c_cflag &= ~CSIZE;
toptions.c_cflag |= CS8;
/* no flow control */
toptions.c_cflag &= ~CRTSCTS;
/* Canonical mode */
toptions.c_lflag |= ICANON; // this supposed to be off
toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl
lines
toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
/* will break everything, these are here NOT to enable them */
//toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
//toptions.c_oflag &= ~OPOST; // make raw
// see: http://unixwiz.net/techtips/termios-vmin-vtime.html
toptions.c_cc[VMIN] = 0;
toptions.c_cc[VTIME] = 0;
//toptions.c_cc[VTIME] = 20;
/* commit the serial port settings */
tcsetattr(fd, TCSANOW, &toptions);
if( tcsetattr(fd, TCSAFLUSH, &toptions) < 0) {
perror("init_serialport: Couldn't set term attributes");
return -1;
}
return fd;
}
int serialport_read_until(int fd, char* buf, char until, int buf_max,
int timeout)
{
char b[1]; // read expects an array, so we give it a 1-byte array
int i=0;
do {
int n = read(fd, b, 1); // read a char at a time
if( n==-1) return -1; // couldn't read
if( n==0 ) {
usleep( 1 * 1000 ); // wait 1 msec try again
timeout--;
if( timeout==0 ) return -2;
continue;
}
#ifdef SERIALPORTDEBUG
printf("serialport_read_until: i=%d, n=%d b='%c'\n",i,n,b[0]);
// debug
#endif
buf[i] = b[0];
i++;
} while( b[0] != until && i < buf_max && timeout>0 );
buf[i] = 0; // null terminate the string
return 0;
}
int serialport_write(int fd, char *comm)
{
char command[100];
comm[strcspn(comm, "\r\n")] = 0;
sprintf(command,"%s\n",comm);
printf("Sending Command:>%s<\n",comm);
int len = strlen(command);
int n = write(fd, command, len);
if( n!=len )
return -1;
return 0;
}
This works for RX (Receive data from the arduino) but no the serial
write (once again the exact same hw works fine with the same code
compiled on Linux). I guess NetBSD does something differently when
opening and handling the serial port. In the write function I remove the
incoming \r\n trash for security then complete the incoming command
string with \n before writing it to serial.
Any ideas what can go wrong here?
Thanks.
Home |
Main Index |
Thread Index |
Old Index