gpt4 book ai didi

c++ - 如何更改此代码以在一部分(而不是 2 部分)中从串口读取所有数据?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:25:36 24 4
gpt4 key购买 nike

此代码正在从 RFID 模块 (EM-18) 读取数据。我这里有一个大问题。当我的应用程序正在运行并且我从 em 模块传递 RFID 卡时,它分两部分读取数据。先读取 8 字节的卡 ID,然后发送 4 字节。像这样:

[root@FriendlyARM /fgit]# ./RFIDMonitor -qws
enter the port name:
ttySAC3
open_port: succesfully open port
open_port: succesfully open port
RFID MONITORING => '010B7528'
RFID MONITORING => '297E'
RFID MONITORING => '010B7528'
RFID MONITORING => '297E'

我改变了 VMINVTime 但结果没有改变。

这是我的代码:

 if(!fork())
{
while(1)
{
memset(buf2,'\0',MAXDATASIZE);
//------------------------------------------------
if ((numbytes = read(fd,buf2, MAXDATASIZE-1)) != -1)
{
buf2[numbytes] = '\0';
printf("RFID MONITORING => '%s'\n",buf2);
}

}
}

这是我的配置:

   int openport(void)
{
cout<<"enter the port name:\n";
string portname="";
cin>>portname;

portname="/dev/"+portname;
fd=open(portname.c_str(),O_RDWR|O_NOCTTY|O_NDELAY);
if (fd==-1)
{
//printf("open_port: unable to open port \n");
return -1;
}
else
{
//printf("open_port: succesfully open port \n");
fcntl(fd,F_SETFL,0);
return 1;
}
}
//-------------------------------------
void closeport(void)
{
close(fd);
}
//-------------------------------------
void configport(void)
{
struct termios tty;
struct termios tty_old;
memset (&tty, 0, sizeof tty);

/* Error Handling */
if ( tcgetattr ( fd, &tty ) != 0 ) {
std::cout << "Error " << errno << " from tcgetattr: " << strerror(errno) << std::endl;
}

/* Save old tty parameters */
tty_old = tty;

/* Set Baud Rate */
cfsetospeed (&tty, (speed_t)B9600);
cfsetispeed (&tty, (speed_t)B9600);

/* Setting other Port Stuff */
tty.c_cflag &= ~PARENB; // Make 8n1
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;

tty.c_cflag &= ~CRTSCTS; // no flow control
tty.c_cc[VMIN] =1;// change to 0 and 5
tty.c_cc[VTIME] = 10;// change to 0 and 5
tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines

/* Make raw */
cfmakeraw(&tty);


/* Flush Port, then applies attributes */
tcflush( fd, TCIFLUSH );
if ( tcsetattr ( fd, TCSANOW, &tty ) != 0) {
std::cout << "Error " << errno << " from tcsetattr" << std::endl;
}
}

我想这样显示(所有数据在一个部分)

RFID MONITORING => '010B7528297E

最佳答案

read 只读取可以读取的内容。如果您必须通读整篇文章,请继续阅读,直到您读完整篇文章。

while(1)
{
memset(buf2,'\0',MAXDATASIZE);
int bytesread = 0;
//------------------------------------------------
//Keep going until the whole message has been read
while (bytesread != MAXDATASIZE-1)
{
//read less on successive reads, but write further into buffer
if ((numbytes = read(fd,buf2[bytesread], MAXDATASIZE-1-bytesread)) == -1)
{
printf("Error in reading data");
// handle error. Probably reopen port and resynch with RFID
}
else
{
// keep track of where we are in accumulating
bytesread += numbytes;
}
}
buf2[bytesread] = '\0';
printf("RFID MONITORING => '%s'\n",buf2);
}

关于c++ - 如何更改此代码以在一部分(而不是 2 部分)中从串口读取所有数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35680497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com