Use legato API to open ttyUSB0 and got error


I want to use legato API to read ttyUSB0, but i can’t open ttyUSB0.

#include “legato.h”
#include “le_tty.h”

int SerialPortFd = -1;
int ret = 0 , num_ret = 0;
char buf[1024] = {0};

SerialPortFd = le_tty_Open("/dev/ttyUSB0" , O_RDWR | O_NOCTTY | O_NDELAY );


Error log

I can find my device like below:

I don’t know why!

are you using unsandboxed legato application?

Hi @jyijyi ,

I can’t understand what is “unsandboxed” ?

I follow APP doc to write myAPP.

if it is sandboxed, it cannot see there is /dev/ttyUSB0.
you need to go to .adef and add:
sandboxed: false

If you still want to use sandboxed feature, you can see this application note:

In .cdef, you need to add something like:


        [rw]   /dev/ttyHSL1             /dev/  // WP UART2  mangOH debug Console
        [rw]   /dev/ttyHS0             /dev/   // WP UART2 or IoT Connector UART
//        [rw]   /dev/ttyHSL0             /dev/   // FX30S UART   

Hi @jyijyi ,

Thanks for your help.

I fixed the problem.

may I have other question about print log?

I want to use “printf” to print log on console, but it seems not work.

you can see here:

Hi @jyijyi ,

Thanks for your help. I will read it.

Now I try to read ttyUSB0 like

num_ret = read(SerialPortFd, buf, sizeof(buf));
usleep ((7 + 25) * 1000);
buf[num_ret+1] = ‘\0’;
if (num_ret > 0) {
LE_ERROR(“read result: %s\n”, buf, num_ret);
sprintf(cmd, “echo "%s" > /tmp/test.log”,buf);

I can see the length > 0, but i can’t get the string

but I can use “cat” to get result.


why don’t you print it out in LE_INFO()?
you can print the %i instead of %s as there might be NULL character

Hi @jyijyi

I got a error from update but I can’t find the detail log, please help me

Not sure if you compiled with the correct legato version…

you can try with a simple helloworld application first which should be able to download to the module.
If it works, port the code to it line by line and see which line makes the update failed.

Hi @jyijyi ,

I used it yesterday, but it can’t work now

does “logread” command show any reason on this?
I never see this before, maybe you need to redownload FW?

Hi @jyijyi ,

I fixed it, I have other question about read result,

I use “stty” to set the com port and use “cat” to read result like below:

But I use API it can not get the result.

SerialPortFd = le_tty_Open(“/dev/ttyUSB0” , O_RDWR);
ret = le_tty_SetBaudRate(SerialPortFd, LE_TTY_SPEED_1152000);
ret = le_tty_SetFraming(SerialPortFd, ‘N’, 8, 1);
ret = le_tty_SetRaw(SerialPortFd, 0, 0);

num_ret = read(SerialPortFd, buf, sizeof(buf));

Is my setting the same as “stty” ?

Please help me thanks.

The easiest way is you use unsandboxed legato app, and run system(“cat /dev/ttyUSB0 > /tmp/test.txt”)
After you can read the file in your app

But I don’t want to use “cat” because I want to save the result in the buffer.

You can read the content of the file and save the result in your buffer…

BTW, you can try this UART_TEST1 app and see if it works on your environment:

Hi @jyijyi ,

I think read and write the same file that is a problem in multi thread.

I will ty UART_Test1.rar

Hi @jyijyi ,

I tested UART_Test1.rar and I got some conclusions.

  1. I only use OpenPort(), then it doesn’t work.
  2. I only use le_tty_xxx API to setup and open, the it doesn’t work too.
  3. I use OpenPort() + le_tty_SetRaw(), then it worked, so I want to know what diffrernt between le_tty_SetRaw and OpenPort() to set RAW step ?


I think it related to c_iflag, c_lflag and c_oflag of the port setting.

you can see the source code of le_tty_SetRaw() here:

Hi @jyijyi ,

Thanks for your help and I keep try to find out which settings will affect.

And I have other question.

  1. I can’t use printf to print to my ssh window by following hello1.rar, I use command “app runProc XXX XXX --exe=XXX” but I still can see the log in “logread”.
  2. I print log to LE_INFO, my question is that function have limit ? because I saw the log and check string length is 162 but it doesn’t show all the string.