当我尝试打开串口时,为什么会出现“CreateFile Failed: 161”?

B. *_*non 4 c# serial-port opennetcf windows-ce zebra-printers

我在 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()"); 
}
Run Code Online (Sandbox Code Playgroud)

我知道这是因为我看到的最后一个“调试消息”是“使它超出了 PrintUtils.PrintBarcode() 中的协议分配”

是因为其中一个协议错误还是格式错误?还是我省略了必需的协议分配?

cta*_*cke 5

错误 161 表示The specified path is invalid.您收到错误是因为您的端口名称无效。

Windows CE 要求端口名称(实际上是所有驱动程序名称)以 ':' 字符为后缀,因此您的代码应该是:

serialPort.PortName = "COM1:"; 
Run Code Online (Sandbox Code Playgroud)