首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >termios Arduino读/写失败

termios Arduino读/写失败
EN

Stack Overflow用户
提问于 2013-05-19 05:44:58
回答 1查看 1K关注 0票数 0

你好,我遇到了一些问题,试图编程一个Arduino从c++程序接受命令。我在用termios连接到ArduinoArduino处于网关模式(基本上它本身并不执行代码,而是等待来自我的程序的输入与我连接到它的硬件交互)。

我的termios设置如下:

代码语言:javascript
复制
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的代码是这样的:

代码语言:javascript
复制
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不会亮起,任何读取命令都会无限期地挂起。

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

EN

回答 1

Stack Overflow用户

发布于 2016-05-26 19:37:40

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16629280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档