termios Arduino读写失败

termios Arduino read/write failing

本文关键字:失败 读写 Arduino termios      更新时间:2023-10-16

你好,我在尝试编程Arduino以从c++程序中获取命令时遇到了一些麻烦。我正在使用termios连接到Arduino。Arduino处于网关模式(基本上它本身不执行代码,但正在等待我的程序输入与我连接到它的硬件进行交互)。

我的术语设置如下:

SerialHandler::SerialHandler(char* fPath, int bRate)
{
filePath = fPath;
baudRate = 0;
//Open the file, file is of type int
file = open(filePath, O_RDWR | O_NOCTTY);
if(file<0) //If there is an error opening the file
{
    perror(filePath);
    exit(-1);
}
//Save the old port settings
tcgetattr(file, &oldtio);
bzero(&newtio, sizeof(newtio));
//now to load the baudrate
getBaudRate(bRate, baudRate);
newtio.c_cflag = baudRate | CRTSCTS | CS8 | CLOCAL | CREAD;
//IGNPAR ignore bits with parity errors
//ICRNL  map CR to NL ..May have to change to raw input processing
newtio.c_iflag = IGNPAR;
//Raw output
newtio.c_oflag = 0;
//ICANON - enable canonical input
//disables echo functionality, doesnt send signals to calling program
newtio.c_lflag = ICANON;
//Clean and activate port
tcflush(file, TCIFLUSH);
tcsetattr(file, TCSANOW, &newtio);
}

我编写和读取Arduino的代码是这样的:

void SerialHandler::getSerialOutput(char* readBuffer, int& bufferPoint)
{
cout <<"Beginning readn";

bufferPointer = read(file, outputBuffer,255);
cout <<"Finished Readn";
for(int i=0;i<bufferPointer;i++)
{
    cout << outputBuffer[i]<<endl;
    readBuffer[i] = outputBuffer[i];
}
bufferPoint = bufferPointer;
}

void SerialHandler::writeSerial(char* writeBuffer, int length)
{
cout << "Writing: " << writeBuffer<<endl;
write(file,writeBuffer,length);
cout << "Finished Write n";
}

最初我发送Arduino一个测试命令("ATrn"),它响应("OK"),但在此之后任何后续的读/写失败。

在测试命令上,Arduino的RX和TX引脚亮起(意味着它正在接收和传输数据),但之后我发送的命令失败。当我尝试写入Arduino时,Arduino不会亮起,并且任何读取命令都无限期挂起。

我认为问题是类似文件处理程序关闭,但我不确定如何测试这个或它可能是另一个问题。

尝试从newtio.c_cflag行删除CRTSCTS。当您连接串行连接的所有电缆时,需要使用CRTSCTS。当使用Arduino时,您只连接tx、rx和地线。更多信息请参见termios手册http://linux.die.net/man/3/termios