Home > OS >  [with] Linux serial data read wrote. TXT file, the second line of extra ^ @ what reason?
[with] Linux serial data read wrote. TXT file, the second line of extra ^ @ what reason?

Time:12-31

Recently in learning Linux serial operation, the plan is to turn from the serial port to read data written to a. TXT file, found written definition of char title []="time data \ n"; Character, the second line is always more ^ @ content, the corresponding is \ 0 characters, but there are no written code inside the characters, don't know how this character, consult everybody have met, to give directions, thank you! The following is the result of the code and tests,



//serial port configuration function
Int serial_test (void)
{

Char testdata []={x26 0 xfe, 0 x19, 0, 0 x34};
Char dataread [10].
Bzero (dataread, sizeof (dataread));
Fd0=open ("/dev/ttyS2 O_RDWR);

If (fd0 & lt; 0)
{
Printf (" open serial fail \ n ");
return 1;
}
Tcgetattr (fd0, & amp; Oldtio);/* Save fd1 setting */
Tcgetattr (fd0, & amp; Newtio);
Newtio. C_cflag=B115200 | CS8 gives | CREAD | CLOCAL;
Newtio. C_iflag=IGNPAR;
Newtio. C_oflag=0;
Newtio. C_lflag=0;
Newtio. C_cc [VMIN]=1;
Newtio. C_cc [VTIME]=0;
Tcflush (fd0 TCIFLUSH);
Tcsetattr (fd0 TCSANOW, & amp; Newtio);/* the Configuration fd1 *///to make set of terminal properties take effect immediately
Printf (" serial ok \ n ");
Sleep (1);

}
///serial port thread function
Void * serial_read_data (void * arg)
{
Fd_set fdR.
//char buff [30]={0};
Int fd1;
Struct timeval timeout={0, 0};
Int rselect;
Time_t timer;
Char timebuff [40].
Char buff [256].
Int len, len1 len2;
Char kongge []="";
Char title []="time data \ n";
Char huanghang []="\ n";
Char rdatabuff [100]={0};


Fd0=open ("/dev/ttyS2 O_RDWR);
Tcflush (fd0 TCIFLUSH);
Sleep (4);

Fd1=open ("/umsd stest. TXT ", O_RDWR | O_CREAT | O_TRUNC) will be passed.
Lseek (fd1 and 0, SEEK_END);
Write (fd1, title, sizeof (title));
The timeout. Tv_sec=10;
Bzero (buff, sizeof (buff));
Bzero (timebuff, sizeof (timebuff));

If (fd0 & lt; 0)
{
Printf (" open Fuji serial fild \ n ");
}
While (1)
{
FD_ZERO (& amp; FdR);
FD_SET (fd0, & amp; FdR);
Rselect=select (fd0 + 1, & amp; FdR, NULL, NULL, & amp; The timeout);
The switch (rselect)
{
Case 1:
Printf (" serial error \ n ");
break;
Case 0:
//printf (" serial no read data \ n ");
break;
Default:
If (FD_ISSET (fd0, & amp; FdR))
{
Len1=read (fd0 rdatabuff, sizeof (rdatabuff));
The timer=time (NULL);
Ctime_r (& amp; The timer, timebuff);
Len=strlen (timebuff);
Strncpy (buff, timebuff, len - 1);
Strcat (buff, kongge);
Strcat (buff, rdatabuff);
Strcat (buff, huanghang);
Len2=strlen (buff);
Write (fd1, buff, len2);
Printf (" read data "ok" % s \ n ", buff);
Bzero (buff, sizeof (buff));
Bzero (rdatabuff, sizeof (rdatabuff));
}
}
}

///test result/MNT/home # CD/umsd
/umsd # vi stest. TXT
Time data
^ @ 123456789
7 09:09:50 Wed Feb 2018Wed Feb 7 09:10:00 2018 serial ok
Wed Feb 7 09:10:02 2018 123456789

CodePudding user response:

Both may be invisible \ r \ n characters, such as from the hexadecimal tool od command to see,

CodePudding user response:

Hello, has this problem you solve?
  • Related