开发者

Reading a Linux device with fstream

I'm attempting to get feedback from some hardware that is used over USBTMC and SCPI.

I can read and write to the device using /dev/usbtmc0 in a C++ [io]fstream, alternating by reading and writing to send and receive messages. Most commands are terminated by a single newline, so it's easy to tell when the end of a response is received. The simplified code I'm using for that is:

fstream usb;
usb.open("/dev/usbtmc0", fstream::in);
if (usb.good())
{
    string output;
    getline(usb, output);
    usb.close();
    // Do things with output
}
// Additional cleanup code...

There is, however, one thing that is escaping me, and it's defined in the SCPI/IEEE specification as "*LRN?". When sent, the connected device will send back arbitrary data (actual wording from the specification) that can be used to later reprogram the device should it get into a weird state.

The issue with the response message of this LRN command is that it contains one or more newlines. It does properly terminate the entire message with a newline, but the fact that there are newlines embedded makes it really tricky to work with. Some hardware will prefix the payload with a length, but some don't.

When reading data from the hardware, there is a 5 second timeout built into the Linux usbtmc kernel driver that will block any read calls if you try to read past what's available.

Using fstream::eof doesn't seem to return anything useful. It act开发者_Python百科s much like a socket. Is there a way that I can read all data on the device without knowing about its length, termination, and while avoiding a kernel timeout?


The problem with using fstream for this is that fstream has internal buffering, there's no 1:1 correlation between device fileOps->read calls and fstream operations.

For interacting with device drivers, you really need to use the low-level open, read, write functions from unistd.h and fcntl.h.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜