首页 > 解决方案 > 从设备文件中读取提供了额外的输入

问题描述

我在 USB-COM 模式下通过 USB 将 QR-Code-Scanner 连接到我的 Linux-PC。当我这样做时,会创建一个文件:/dev/serial/by-id/usb-Datalogic_ADC__Inc._Handheld_Barcode_Scanner_S_N_E18C59366.-if00

我像这样从这个文件中读取:

#include <iostream>     
#include <fstream>      
#include <string>
#include <unistd.h>
using namespace std;

int main () {
        while (1) {
                ifstream bufferLink("/dev/serial/by-id/usb-Datalogic_ADC__Inc._Handheld_Barcode_Scanner_S_N_E18C59366.-if00");
                if (bufferLink.is_open()) {
                        string line;
                        while (!bufferLink.eof()) {
                                cout << "Waiting for input: " << endl;
                                getline(bufferLink, line);
                                cout << "+++++++++ Line: " << line << endl;
                        }
                }
        }
        usleep(1000*1000);
        return 0;
}

然后我用数据创建了一个二维码:Andre Mantei ist der Beste。

但是当我用我的扫描仪读取二维码时,它给了我很多额外的输出:

Waiting for input: 
+++++++++ Line: Andre Mantei ist der Beste.
Waiting for input: 
+++++++++ Line:  
Waiting for input: 
+++++++++ Line: 
Waiting for input: 
+++++++++ Line: !"#$%&'()*+,-./0123456789:;<=>?@
Waiting for input: 
+++++++++ Line: 
Waiting for input: 
+++++++++ Line: ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`
Waiting for input: 
+++++++++ Line: 
Waiting for input: 
+++++++++ Line: abcdefghijklmnopqrstuvwxyz{|}
Waiting for input: 
+++++++++ Line: 
Waiting for input: 
+++++++++ Line: 
Waiting for input: 
+++++++++ Line: 
Waiting for input: 
+++++++++ Line: 
Waiting for input: 
+++++++++ Line:  
Waiting for input: 
+++++++++ Line: 
Waiting for input: 
+++++++++ Line: !"#$%&'()*+,-./0123456789:;<=>?@
Waiting for input: 
+++++++++ Line: 
Waiting for input: 
+++++++++ Line: ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`
Waiting for input: 
+++++++++ Line: 
Waiting for input: 
+++++++++ Line: abcdefghijklmnopqrstuvwxyz{|}
Waiting for input: 
+++++++++ Line: 
Waiting for input: 

它并不总是发生,而是大多数时候发生。谁能解释这种行为?

标签: c++linux-device-driverinputstreambarcode-scanner

解决方案


推荐阅读