Home > OS >  Embedded Linux C, serial port to read, via a serial port debug tool according to hexadecimal to send
Embedded Linux C, serial port to read, via a serial port debug tool according to hexadecimal to send

Time:10-14

Embedded Linux C, read serial code is as follows, through a serial port debug tool according to hexadecimal to send data, the problem is every time read 0 x00 stops, such as sending 31 00 32, receive only 31?
Do
{
Memset (buff, 0, BUFFER_SIZE);
If (nread=read (fd, buff, BUFFER_SIZE) & gt; 0)
{
Printf (" The received words are: % s \ n ", buff);
}
sleep(1);
Write (fd, buff, strlen (buff));
} while (STRNCMP (buff, "quit", 4));

CodePudding user response:

Printf (" The received words are: % s \ n ", buff); This line only type 0 x31, with a loop to play X %

CodePudding user response:

If send 31 00 32, all can print it out

CodePudding user response:

If a serial port initialization which Settings are not set?

CodePudding user response:

Strlen function '\ 0' (equal to the number 0) to determine whether to to the end of the string, the length of the strlen calculated is 1, in fact you only read a byte

CodePudding user response:

Sorry, the write one byte

CodePudding user response:

I also encountered this problem,
An array of strings inside 00 is annulled,

CodePudding user response:

Strlen function to '\ 0' (equal to the number 0) determine whether to to the end of the string; Try to use when sending and receiving data

CodePudding user response:

 
Write (fd, buff, strlen (buff));//method error, the length of the string functions should not be used to calculate, should be used to read data length,


Modified to
 
Write (fd, buff, nread);


The rest of the logic judgment should be modified according to the situation,,

CodePudding user response:

This should be 0 x00 as string terminator

CodePudding user response:

Do not use the length of the strlen is used directly
  • Related