你好,我遇到了一些问题,试图编程一个Arduino从c++程序接受命令。我在用termios连接到ArduinoArduino处于网关模式(基本上它本身并不执行代码,而是等待来自我的程序的输入与我连接到它的硬件交互)。
我的termios设置如下:
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 read\n";
bufferPointer = read(file, outputBuffer,255);
cout <<"Finished Read\n";
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发送一个test命令("AT\r\n"),它会响应("OK"),但在此之后,任何后续的读/写操作都会失败。
在测试命令中,Arduino的RX和TX引脚亮起(意味着它正在接收和发送数据),但我在那之后发送的命令失败了。当我尝试向Arduino写入数据时,Arduino不会亮起,任何读取命令都会无限期地挂起。
我认为问题类似于文件处理程序关闭,但我不确定如何测试,否则可能是另一个问题。
发布于 2016-05-26 19:37:40
尝试从newtio.c_cflag行中删除CRTSCTS。当您连接串行连接的所有电缆时,需要CRTSCTS。使用Arduino时,只能连接tx、rx和gnd线路。有关更多信息,请参见termios手册页http://linux.die.net/man/3/termios
https://stackoverflow.com/questions/16629280
复制相似问题