如何在阻止read()调用中检测USB电缆断开



我有一个智能电表,它每秒发送能耗数据。我编写的用于读取数据的守护程序(C++/C Arch Linux(在USB电缆断开连接时不会退出,并在阻塞read((调用中无限期暂停。

如何中断阻塞的read((调用(即使用EINTR失败,而不是等待下一个字符(?

我广泛搜索了谷歌,并在SO中查看了这里,但找不到这个问题的答案。

详细信息:

  • Github上的Smartmeter项目源
  • 带有FT232RL USB到UART桥接的IR加密狗
  • 数据报的固定长度为每秒发送328个字节
  • Read方法检测开始\和结束!数据报的标记
  • 捕获CTRL+C SIGINT和SIGTERM信号的sigaction
  • termios被设置为在VMIN=1和VTIME=0的情况下执行阻塞读取((

已尝试:

  • 使用VMIN和VTIME
  • 删除SA_RESTART

可能的解决方案:

  • 使用非阻塞读取方法,可以使用select((和poll((
  • 或者VMIN>0(数据报长度超过255个字符,我需要以较小的块读取数据报(
  • 不确定如何处理非阻塞读取方法的数据报开始/结束检测和数据报之间的1秒间隔

EDIT:下面的代码现在将read((调用缓冲到一个255字节的中间缓冲区中(VMIN=255,VTIME=5(,该缓冲区从这里改编而来。这避免了为每个字符调用read((的小开销。然而,在实践中,与一次阅读一个字符相比,这并没有什么区别。在断开电缆连接时,Read((仍然无法正常退出。守护进程需要使用kill -s SIGQUIT $PID来终止。SIGKILL无效。

main.cpp:

volatile sig_atomic_t shutdown = false;
void sig_handler(int)
{
shutdown = true;
}
int main(int argc, char* argv[])
{
struct sigaction action;
action.sa_handler = sig_handler;
sigemptyset(&action.sa_mask);
action.sa_flags = SA_RESTART;
sigaction(SIGINT, &action, NULL);
sigaction(SIGTERM, &action, NULL);
while (shutdown == false)
{
if (!meter->Receive())
{
std::cout << meter->GetErrorMessage() << std::endl;
return EXIT_FAILURE;
}
}

Smartmeter.cpp:

bool Smartmeter::Receive(void)
{
memset(ReceiveBuffer, '', Smartmeter::ReceiveBufferSize);  
if (!Serial->ReadBytes(ReceiveBuffer, Smartmeter::ReceiveBufferSize)) 
{
ErrorMessage = Serial->GetErrorMessage();
return false;
}
}

SmartMeterSerial.cpp:

#include <cstring>
#include <iostream>
#include <thread>
#include <unistd.h>
#include <termios.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include "SmartmeterSerial.h"
const unsigned char SmartmeterSerial::BufferSize = 255;
SmartmeterSerial::~SmartmeterSerial(void)
{
if (SerialPort > 0) {
close(SerialPort);
}
}
bool SmartmeterSerial::Begin(const std::string &device)
{
if (device.empty()) {
ErrorMessage = "Serial device argument empty";
return false;
}
if ((SerialPort = open(device.c_str(), (O_RDONLY | O_NOCTTY))) < 0)
{
ErrorMessage = std::string("Error opening serial device: ") 
+ strerror(errno) + " (" + std::to_string(errno) + ")";
return false;
}
if(!isatty(SerialPort))
{
ErrorMessage = std::string("Error: Device ") + device + " is not a tty.";
return false;
}
if (flock(SerialPort, LOCK_EX | LOCK_NB) < 0)
{
ErrorMessage = std::string("Error locking serial device: ")
+ strerror(errno) + " (" + std::to_string(errno) + ")";
return false;
}
if (ioctl(SerialPort, TIOCEXCL) < 0)
{
ErrorMessage = std::string("Error setting exclusive access: ") 
+ strerror(errno) + " (" + std::to_string(errno) + ")";
return false;
}
struct termios serial_port_settings;
memset(&serial_port_settings, 0, sizeof(serial_port_settings));
if (tcgetattr(SerialPort, &serial_port_settings))
{
ErrorMessage = std::string("Error getting serial port attributes: ")
+ strerror(errno) + " (" + std::to_string(errno) + ")";
return false;
}
cfmakeraw(&serial_port_settings);
// configure serial port
// speed: 9600 baud, data bits: 7, stop bits: 1, parity: even
cfsetispeed(&serial_port_settings, B9600);
cfsetospeed(&serial_port_settings, B9600);
serial_port_settings.c_cflag |= (CLOCAL | CREAD);
serial_port_settings.c_cflag &= ~CSIZE;
serial_port_settings.c_cflag |= (CS7 | PARENB);

// vmin: read() returns when x byte(s) are available
// vtime: wait for up to x * 0.1 second between characters
serial_port_settings.c_cc[VMIN] = SmartmeterSerial::BufferSize;
serial_port_settings.c_cc[VTIME] = 5;
if (tcsetattr(SerialPort, TCSANOW, &serial_port_settings))
{
ErrorMessage = std::string("Error setting serial port attributes: ") 
+ strerror(errno) + " (" + std::to_string(errno) + ")";
return false;
}
tcflush(SerialPort, TCIOFLUSH);
return true;
}
char SmartmeterSerial::GetByte(void)
{
static char buffer[SmartmeterSerial::BufferSize] = {0};
static char *p = buffer;
static int count = 0;   
if ((p - buffer) >= count)
{
if ((count = read(SerialPort, buffer, SmartmeterSerial::BufferSize)) < 0)
{
// read() never fails with EINTR signal on cable disconnect   
ErrorMessage = std::string("Read on serial device failed: ")
+ strerror(errno) + " (" + std::to_string(errno) + ")";
return false;
}
p = buffer;
}
return *p++;
}
bool SmartmeterSerial::ReadBytes(char *buffer, const int &length)
{
int bytes_received = 0;
char *p = buffer;
bool message_begin = false;

tcflush(SerialPort, TCIOFLUSH);

while (bytes_received < length)
{
if ((*p = GetByte()) == '/')
{
message_begin = true;
}
if (message_begin)
{
++p;
++bytes_received;
}
}
if (*(p-3) != '!')
{
ErrorMessage = "Serial datagram stream not in sync.";
return false;
}
return true;
}

非常感谢你的帮助。

虽然下面的代码不是关于如何中断阻塞读取((调用的原始问题的解决方案,但至少对我来说这是一个可行的解决方案。VMIN=0和VTIME=0后,这现在是一个非阻塞读((:

bool SmartmeterSerial::ReadBytes(char *buffer, const int &length)
{
int bytes_received = 0;
char *p = buffer;
tcflush(SerialPort, TCIOFLUSH);
bool message_begin = false;
const int timeout = 10000;
int count = 0;
char byte;
while (bytes_received < length) 
{
if ((byte = read(SerialPort, p, 1)) < 0)
{
ErrorMessage = std::string("Read on serial device failed: ")
+ strerror(errno) + " (" + std::to_string(errno) + ")";
return false;
}
if (*p == '/')
{
message_begin = true;
}
if (message_begin && byte)
{
++p;
bytes_received += byte;
}
if (count > timeout)
{
ErrorMessage = "Read on serial device failed: Timeout";
return false;
}
++count;
std::this_thread::sleep_for(std::chrono::microseconds(100));
}
if (*(p-3) != '!')
{
ErrorMessage = "Serial datagram stream not in sync.";
return false;
}
return true;
}

然而,我仍然很想知道是否真的可以中断阻塞的"读取((",因为这种解决方法是不断轮询串行端口。

我相信一次读取一个字符不是问题,因为从UART接收的字节是由操作系统缓冲的,但用read()不断轮询缓冲区是!也许我会在read()之前尝试ioctl(SerialPort, FIONREAD, &bytes_available),尽管我不知道这是否真的会有所不同。

有什么建议吗?

相关内容

  • 没有找到相关文章

最新更新