eoin_oc
Posts: 2
Joined: Tue Jun 25, 2019 12:21 pm

Issue communicating with /dev/ttyUSB1

Tue Jun 25, 2019 12:34 pm

Hi all,

I have cellular modem Ublox SARA-R410 plugged into my Pi Model B. Using the command screen /dev/ttyUSB1 I can send and receive AT commands no problem.

Now I am trying to access /dev/ttyUSB1 from C and it is not working.

I tried in Python and it worked great.

Code: Select all

import time
import serial

ser=serial.Serial(
    port='/dev/ttyUSB1',
    baudrate=115200,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=1
    )

while 1:
    ser.write(str.encode("AT+URAT?\r\n"))
    x=ser.readline()
    print(x)
    time.sleep(1)
In C I have tried 2 different approaches and neither of them read back correctly, but the port does seem to open ok.

This was my first attempt:

Code: Select all

    if(wiringPiSetup()==-1)
    {
        printf("wiring pi failed");
        return 1;
    }
    serialPort=serialOpen("/dev/ttyUSB1",115200);
    if(serialPort==-1)
    	return -1;
    serialFlush(serialPort);
    serialPrintf(serialPort,"AT+URAT?\r\n");
    
while(serialDataAvail(serialPort))
    printf("%c",serialGetchar(serialPort));
This code does not receive any characters from the USB port.

This was C attempt #2:

Code: Select all

    int  init_modem()   /* I - Serial port file */
{
  char buffer[255];  /* Input buffer */
  char *bufptr;      /* Current char in buffer */
  int  nbytes;       /* Number of bytes read */
  int  tries,serialPort;        /* Number of tries so far */
  struct termios options;

    memset(&options,0,sizeof(options));
    
    serialPort=open("/dev/ttyUSB1", O_RDWR | O_NOCTTY | O_NDELAY);
    fcntl(serialPort, F_SETFL, 0);
    if(serialPort==-1)
        return -1;
        /*
     * Get the current options for the port...
     */

    cfsetospeed(&options, B115200);
    cfsetispeed(&options, B115200);

    /*
     * Enable the receiver and set local mode...
     */
    options.c_cc[VMIN]=0;
    options.c_cc[VTIME]=5;
    options.c_cflag = (B115200 | CS8 |CLOCAL | CREAD);
    options.c_iflag =IGNPAR;
    options.c_oflag=0;
    options.c_lflag=0;
    tcflush(serialPort, TCIFLUSH);
    /*
     * Set the new options for the port...
     */

    if(tcsetattr(serialPort, TCSANOW, &options)==-1)
    {
        printf("error setting uart params\r\n");
        return-1;
    }
    

  for (tries = 0; tries < 3; tries ++)
  {
   /* send an AT command followed by a CR */
    if (write(serialPort, "AT\r\n", 4) < 4)
      continue;

   /* read characters into our string buffer until we get a CR or NL */
    bufptr = buffer;
    while ((nbytes = read(serialPort, bufptr, 1)) > 0)
    {
        printf("%c\r\n",bufptr);
      bufptr += nbytes;
      //expect a \r\n together
      if ((bufptr[-1] == '\r' && bufptr[0] == '\n'))//&& (bufptr-buffer)>=4)
        break;
    }

   /* nul terminate the string and see if we got an OK response */
    *bufptr = '\0';

    if (strncmp(buffer, "OK", 2) == 0)
      return (serialPort);
  }

  return (-1);
}

Does anybody know where I am going wrong? I am running the code as sudo.

User avatar
topguy
Posts: 5616
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Issue communicating with /dev/ttyUSB1

Tue Jun 25, 2019 2:33 pm

I think the first attempt will just exit the while loop at once because you don't wait for the modem to reply.
Wrap the while in a 20 iterations "for"-loop that sleep for 100ms every loop.

eoin_oc
Posts: 2
Joined: Tue Jun 25, 2019 12:21 pm

Re: Issue communicating with /dev/ttyUSB1

Tue Jun 25, 2019 3:29 pm

Thanks, you were right 1 does work I just need a delay when checking the first available characters

Return to “C/C++”