2013-02-07 53 views
4

我得到「的CreateFile失敗:161」的serialPort.Open()行:當我嘗試打開串口時,爲什麼會出現「CreateFile Failed:161」?

. . . 
     MessageBox.Show(string.Format("Made it into PrintUtils.PrintBarcode()")); 
using (SerialPort serialPort = new SerialPort()) 
{ 
    MessageBox.Show("Made it into using statement in PrintUtils.PrintBarcode()"); 
    serialPort.BaudRate = 19200; 
    serialPort.Handshake = Handshake.XOnXOff; 
    serialPort.DataBits = 8; 
    serialPort.Parity = Parity.None; 
    serialPort.StopBits = StopBits.One; 
    serialPort.PortName = "COM1"; // Is this what it wants? 
    MessageBox.Show("Made it beyond the protocol assignments in PrintUtils.PrintBarcode()"); 

    serialPort.Open(); // <-- This causes "CreateFile Failed: 161" 
    MessageBox.Show("Opened the serial port in PrintUtils.PrintBarcode()"); 

    Thread.Sleep(2500); // I don't know why this is needed, or if it really is... 

    // Try this first: 
    serialPort.WriteLine("! 0 200 200 210 1"); 
    MessageBox.Show("Sent the first line in PrintUtils.PrintBarcode()"); 
    serialPort.WriteLine("TEXT 4 0 30 40 Bonjour la Monde"); //Hola el Mundo --- Hallo die Welt 
    MessageBox.Show("Sent the TEXT line in PrintUtils.PrintBarcode()"); 
    serialPort.WriteLine("FORM"); 
    MessageBox.Show("Sent the FORM line in PrintUtils.PrintBarcode()"); 
    serialPort.WriteLine("PRINT"); 
    MessageBox.Show("Sent the PRINT line in PrintUtils.PrintBarcode()"); 
    // or (if WriteLine does not include a carriage return and line feed): 
    //    serialPort.Write("! 0 200 200 210 1\r\n"); 
    //    serialPort.Write("TEXT 4 0 30 40 Bonjour la Monde\r\n"); //Hola el Mundo --- Hallo die Welt 
    //    serialPort.Write("FORM\r\n"); 
    //    serialPort.Write("PRINT\r\n"); 

    serialPort.Close(); 
    MessageBox.Show("Closed the port in PrintUtils.PrintBarcode()"); 
} 

我知道,因爲最後的「調試消息」我看到的是「造它超出PrintUtils.PrintBarcode協議分配()「

是否因爲其中一個協議錯誤或格式不正確?還是我省略了必需的協議分配?

+0

什麼是拋出的異常的類型?查看[MSDN SerialPort.Open page](http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.open.aspx)以幫助將異常類型與原因可能是。 –

+0

System.ArgumentException 值不在預期範圍內 –

+0

我在該msdn頁面看到的是:端口名稱不以「COM」開頭。 - 或 - 端口的文件類型不受支持。 由於端口名稱是「COM1」,可能是第二個原因?但是這是什麼意思?什麼是端口的「文件類型」? –

回答

4

錯誤161意味着The specified path is invalid.,你會得到它,因爲你的端口名稱是無效的。

Windows CE的要求端口名稱(所有驅動程序名稱實際上)與一個後綴「:」字符,所以你的代碼應該是:

serialPort.PortName = "COM1:"; 
+0

謝謝,OpenNETCF的人!我一整天都在和這個戰鬥,這是我看到的那個顯然非常神祕的要求的第一個參考。現在我的問題是Zebra設備在代碼中間關閉(我打開它,燈變綠,並且在通過串行端口發送數據的中間,它會關閉...?!? !?) –

相關問題