2016-07-28 144 views
0

所以,我試圖使用我的條碼掃描器作爲'串行'設備,而不是鍵盤模擬器,但它不創建com端口。我掃描了手冊中的設置代碼,將其設置爲串行設備,似乎正確配置掃描儀(它停止將掃描代碼發送到文本框\文本編輯器),但由於沒有COM端口,我無法當我掃描條形碼時捕獲數據......條碼掃描器 - 串行端口

Windows第一次插入時安裝了驅動程序,沒有提供磁盤\驅動程序......想知道是否有其他人遇到過相同的問題.....

這裏是我的代碼....

class Program 
{ 
    // Create the serial port with basic settings 
    private SerialPort port = new SerialPort("com1", 9600, Parity.None, 8, StopBits.One); 

    [STAThread] 
    static void Main(string[] args) 
    { 
     new Program(); 
    } 

    private Program() 
    { 

     string[] ports = System.IO.Ports.SerialPort.GetPortNames(); 

     Console.WriteLine("Incoming Data:"); 

     // Attach a method to be called when there 
     // is data waiting in the port's buffer 
     port.DataReceived += new 
      SerialDataReceivedEventHandler(port_DataReceived); 

     // Begin communications 
     port.Open(); 

     // Enter an application loop to keep this thread alive 
     Application.Run(); 
    } 

    private void port_DataReceived(object sender, 
     SerialDataReceivedEventArgs e) 
    { 
     // Show all the incoming data in the port's buffer 
     Console.WriteLine(port.ReadExisting()); 
    } 
} 

我得到錯誤信息...... '端口 'COM1' 不存在' ......當我嘗試打開P ORT。

當我創建了一個虛擬端口(使用第三方應用程序)代碼運行,但我仍然沒有從掃描儀獲取數據....

+0

您必須編寫一段代碼才能連接到COM端口,如果沒有通過指定端口,波特率,數據位和其他幾個參數顯式連接到掃描器,掃描器將無法工作。它與捕捉數據不同,而不是鍵盤模式。 – lucas

+0

@ mauro21pl是的,這樣做,是否已經添加了我的代碼,我收到錯誤信息.....'端口'com1'不存在'.....當我嘗試打開端口時。也許我做錯了什麼.... – User45362

+0

這將是「COM1」,而不是「COM1」?無法記住它是否在DOS時間區分大小寫......只需在您的'ports'中調試值即可。 –

回答

0

你可以使用下面的代碼。我可以打開我在特定端口配置的COM。 SerialPort _serialPort;

// delegate is used to write to a UI control from a non-UI thread 
    private delegate void SetTextDeleg(string text); 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     // all of the options for a serial device 
     // can be sent through the constructor of the SerialPort class 
     // PortName = "COM1", Baud Rate = 19200, Parity = None, 
     // Data Bits = 8, Stop Bits = One, Handshake = None 
     //_serialPort = new SerialPort("COM8", 19200, Parity.None, 8, StopBits.One); 
     _serialPort = new SerialPort("COM8", 19200, Parity.None, 8, StopBits.One); 
     _serialPort.Handshake = Handshake.None; 
     _serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived); 
     _serialPort.ReadTimeout = 500; 
     _serialPort.WriteTimeout = 500; 
     _serialPort.Open(); 
    } 
0

我正在編寫自己的條形碼腳本。我的掃描儀默認爲即插即用USB-HID ...人機接口設備...與USB-COMn端口不同。我必須掃描一個條形碼才能切換到串口模式。您可以在設備管理器樹中觀看轉換過程...作爲「端口」分支發芽,包含您的條碼掃描器的詳細信息。我的COM3。

+0

它只是有助於知道即插即用設置不會保證你會看到COM端口...當默認設置是HID。我很好奇。現在我可以打開COM端口...但我沒有收到任何數據rec'd。有什麼建議麼? – Joe