termios Arduino读写失败
termios Arduino read/write failing
你好,我在尝试编程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
相关文章:
- 用C++快速读写文件
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 在一个读写器队列中,我可以用volatile替换原子吗
- C++套接字对不读/写父/子
- 如何调试读写器锁的死锁?
- 在两个线程上读/写 64 位,无互斥/锁定/原子
- Linux 挂载使用重新挂载以允许读写
- C++中读/写二进制文件
- 如何读/写或遍历 std::array 中的特定元素范围?
- 内存排序或读取-修改-写入操作,仅(读/写)内存顺序
- USB-HID 读/写(重叠)等待单个对象不返回C++
- 为什么使用_access时只读测试对读写文件有效
- Boost::Asio串行读/写打开:参数不正确
- 读/写 OpenMP 中的共享向量会减慢程序速度
- 谷歌测试读写同一文件失败
- Android USB 附件模式无法使用 libusb 与主机 PC 一起读/写
- 特定时间间隔 C++ 上的读写日志
- SQLite - C++中的多线程读写
- termios Arduino读写失败