- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想用 C/C++ 代码从 Linux 上的串口读取数据。因为我仍然可以使用 GtkTerm 甚至使用 cat/dev/ttyUSB0
从这个串行端口读取,所以这不是硬件/驱动程序问题。
使用 gtkterm 之类的程序后,串口似乎没有正确启动,因为读取确实有效。
这是我用来初始化串口的代码(第二版):
UbiDriver::UbiDriver(const std::string &ttyPort)
{
// Doc : http://www.easysw.com/~mike/serial/serial.html#2_4
m_serialHandle = open(ttyPort.c_str(), O_RDWR | O_NOCTTY | O_NDELAY); // Open perif
if(m_serialHandle < 0)
{
MY_THROW("Impossible d'ouvrir le port '" << ttyPort << "' !\nerrno = " << errno);
}
// Conf
//if(fcntl(m_serialHandle, F_SETFL, 0) == -1) // lecture en mode bloquant
if(fcntl(m_serialHandle, F_SETFL, O_NONBLOCK) == -1) // lecture en mode non bloquant
{
MY_THROW("fcntl failed !\nerrno = " << errno);
}
struct termios options;
tcgetattr(m_serialHandle, &options); // Init struct avec la conf actuelle
cfsetispeed(&options, B9600); // In speed
cfsetospeed(&options, B9600); // Output speed
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_cflag |= (CLOCAL | CREAD); // Enable the receiver and set local mode...
options.c_cflag &= ~PARENB; // Desactive bit de parité
options.c_cflag &= ~CSTOPB; // Désactive 2 stop bits -> Active 1 stop bits
options.c_cflag &= ~CSIZE; // Désactive le bit "CSIZE"
options.c_cflag |= CS8; // Communication sur 8 bits
options.c_oflag &= ~OPOST; // Raw output is selected by resetting the OPOST option in the c_oflag member:
// Application de la conf
if(tcsetattr(m_serialHandle, TCSAFLUSH, &options) == -1) // Vidage buffer & application immédiate
{
MY_THROW("tcsetattr failed !\nerrno = " << errno);
}
}
并从端口读取数据
std::string UbiDriver::GetAnswer()
{
const int buffSize = 1024;
char buffer[buffSize] = {'\0'};
int count = 0;
std::string wholeAnswer = "";
int noDataTime = 0;
while(noDataTime < 2) // Tant qu'il y a des données à lire
{
count = read(m_serialHandle, buffer, buffSize - 1);
if(count == -1)
{
MY_THROW("Impossible de lire sur le port serie. Verifiez la connexion avec l'imprimante !")
}
if(count > 0)
{
noDataTime = 0;
buffer[count] = '\0';
for(int i = 0; i < count; i++)
{
buffer[i] &= ~128; // Supression du premier 1 du binaire
}
wholeAnswer += std::string(buffer);
std::cout << count << std::endl;
}
else
{
noDataTime++;
usleep(100000);
}
}
cerr << "----------- Answer -----------" << endl;
cerr << "Size = " << wholeAnswer.size() << endl;
cerr << wholeAnswer << endl;
return wholeAnswer;
return std::string("");
}
注意:此代码是根据您的意见完成的第二个版本。
最佳答案
我打开了 gtkterm 源代码,我终于找到了一个解决方案:事实上,您需要覆盖 terminos 结构(是的,您通常不必这样做)。
如果有人找到更好的解决方案,请随时发布。同时,这里是工作代码,带有英文注释:
打开串口:
// Doc : http://www.easysw.com/~mike/serial/serial.html#2_4
m_serialHandle = open(ttyPort.c_str(), O_RDWR | O_NOCTTY | O_NDELAY); // Open serial port
if(m_serialHandle < 0)
{
MY_THROW("Impossible d'ouvrir le port '" << ttyPort << "' !\nerrno = " << errno);
}
// Read mode
//if(fcntl(m_serialHandle, F_SETFL, 0) == -1) // Blocking read
if(fcntl(m_serialHandle, F_SETFL, O_NONBLOCK) == -1) // Non-blocking read
{
MY_THROW("fcntl failed !\nerrno = " << errno);
}
// Get current terminos configuration
struct termios options;
tcgetattr(m_serialHandle, &options);
// Force termios values (should not be needed, but is)
options.c_cflag = B9600;
options.c_oflag = 0;
options.c_lflag = 0;
options.c_iflag = IGNPAR | IGNBRK;
options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 1;
// Set data rate
cfsetispeed(&options, B9600); // In speed
cfsetospeed(&options, B9600); // Output speed
// Set communication flags
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_cflag |= (CLOCAL | CREAD); // Enable the receiver and set local mode...
options.c_cflag &= ~PARENB; // Desactive bit de parité
options.c_cflag &= ~CSTOPB; // Désactive 2 stop bits -> Active 1 stop bits
options.c_cflag &= ~CSIZE; // Désactive le bit "CSIZE"
options.c_cflag |= CS8; // Communication sur 8 bits
options.c_oflag &= ~OPOST; // Raw output is selected by resetting the OPOST option in the c_oflag member:
// Disable flow control
options.c_iflag &= ~(IXON | IXOFF);
// Apply
if(tcsetattr(m_serialHandle, TCSANOW, &options) == -1) // Vidage buffer & application immédiate
{
MY_THROW("tcsetattr failed !\nerrno = " << errno);
}
// Empty buffers
tcflush(m_serialHandle, TCIOFLUSH);
读取(非阻塞)
const int buffSize = 1024;
char buffer[buffSize] = {'\0'};
int count = 0;
std::string wholeAnswer = "";
int noDataTime = 0;
while(noDataTime < 3) // while there is data to be read
{
count = read(m_serialHandle, buffer, buffSize - 1);
// May fail in NON-BLOCKING mode if there is nothing to be read
/*if(count == -1)
{
MY_THROW("Impossible de lire sur le port serie. Verifiez la connexion avec l'imprimante !")
}*/
if(count > 0)
{
noDataTime = 0;
buffer[count] = '\0';
/*for(int i = 0; i < count; i++)
{
buffer[i] &= ~128; // Delete first binary bit. Could be useful for 7 bit communication, if the first bit is set to 1
}*/
wholeAnswer += std::string(buffer);
}
else
{
noDataTime++;
usleep(100000);
}
}
if(!wholeAnswer.empty())
{
cerr << "----------- Answer -----------" << endl;
cerr << "Size = " << wholeAnswer.size() << endl;
cerr << wholeAnswer << endl;
}
return wholeAnswer;
关于c++ - 在 C++ 中从 Ubuntu Linux (11.10) 上的串口读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9165324/
我想用 python 与我的串口通信。我为 linux 安装了 pyserial 和 uspp: import serial ser = serial.Serial('/dev/pts/1', 192
如何实现 IP 串行,反之亦然。 我听说可以用 SOCAT 做到这一点(9600 N,8,1)--> 串口 --> 网络 --> 串口 -->(9600 N81) 请求人们帮助我解决这个问题 最佳答案
ATtiny88初体验(三):串口 ATtiny88单片机不包含串口模块,因此只能使用软件方式模拟串口时序。 串口通信时序通常由起始位、数据位、校验位和停止位四个部分组成,常见的
我有一个 c# 应用程序,它通过串行端口将 pc 连接到设备。 当我向设备发送数据时,如果设备发回数据,则会触发 datareceived 事件。 我想问这个。 有没有办法模拟设备的数据发送? 我的意
所以我将数据从 Arduino 传输到 C# Winform,后者将数据输出到文本框并将其保存到文件中。传输数据格式如下18|25|999|100~;第一部分是以秒为单位的时间,它让我知道什么时候跳过
规范模式状态的 Termios 手册页 ( http://man7.org/linux/man-pages/man3/termios.3.html ): Input is made available
串口代码有问题。 我只是这样做: opencomm(); send(); closecomm(); ClearCommError()(在 recv() 内) 返回comstat.cbInQue 发送的
我想通过音频插孔使用串行端口获取数据。我对此一无所知。但是我找到了一个应用audioserial可以发送数据到。所以,我认为应该获取像 audioserial 这样的数据。 .是否有相同的项目或对此很
串口有问题 我写了一个程序,可以读取端口 COM1 到 COM9,但可以打开 COMXX(如 com10、com11 等) 我搜索并了解到 tCOM1–COM9 是 NT 命名空间中保留名称的一部分。
我正在尝试在 Linux 中使用串口组织 nob-blocking 读写功能。这是我的代码:http://pastebin.com/RSPw7HAi一切正常,但已缓冲。这意味着,如果我通过控制台 +
我想将出现在 Arduino 中的数据传输到我的 C# 应用程序,但不知道我的代码有什么问题。Arduino 代码来了: int switchPin = 7; int ledPin = 13; boo
我正在编写一个网络驱动程序,它应该使用串行通信将数据包发送到 Arduino。这是一项家庭作业,仅用于教育目的。请在建议一切都可以在用户空间中完成之前考虑到这一点。 这answer说明了 filp_o
我想在笔记本电脑和模块之间进行通信。为此,我创建了一个 python 文件,它将一些数据包发送到 UART,它必须读取它们。我有一个创建数据包的 python 脚本(笔记本电脑): SOF= '24'
正在寻找正确的方法来在主板启动消息期间检测一个关键字。检测到关键字后,一秒后发送 Enter 键。内核是Linux。 # Serial port inisialisation is finished
我尝试通过串口读取数据,但读取操作总是返回0。 // Opening COM port and m_fd returned a valid number m_fd = open (m_com_por
微 Controller :dsPIC33EP512MU810 编译器:MikroC 我正在尝试通过 UART 从远程设备请求多个字节。要获得所需的信息,您发送一个请求字节以接收一个数据字节。当请求超
我计划很快开始围绕串行设备的输入进行编码,很高兴找到 Ruby-serialport . API 看起来很容易使用,但我对如何采用基于事件的方法来接收数据有点困惑。 每当 \n 出现时,我想对数据做一
我想在 Linux 中实现一个驱动程序,它有一个以太网堆栈,但在硬件上输出的数据将是一个串行端口。基本上,我想将我的串行端口注册为以太网驱动程序。有谁知道这是否可能?我希望能够将 IPv6 和/或 U
我正在开发一个项目,其中有许多硬件传感器通过 RS232 串行端口连接到部署机器。 但是……我正在一台没有物理 RS232 串行端口的机器上进行开发,但我想制作假的串行端口,我可以连接到这些端口并
我正在制作一个非常简单的 c++ 程序,它通过串行端口向 arduino 发送一个角度,然后 arduino 将该角度应用于伺服电机。我知道 Unix 把串口设备看成一个文件,实际上这是 c++ 代码
我是一名优秀的程序员,十分优秀!