NetBSD-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Arduino connect to NetBSD box woes



Hah I solve my own problem after reading through the POSIX serial communication. Here is the solution for those who are in same boots:

 /* Output options -> Auto add CR LF */
  toptions.c_oflag &= ~OPOST;
  toptions.c_oflag &= ~ONLCR;

When I sent command and did not get data back that suggested for me that the ardu does not get the command so there is a problem with the PC output. With these flags the CR LF will be appended to sent out commands, don't ask me why is this necessary since my code is sending \n but it is in NetBSD not in Linux.

So it is half solved. Now the question remains why is /dev/dtyU0 not created at boot time just when I replug the stick.


On 2018-05-02 09:30, freebsd%tango.lu@localhost wrote:
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