首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我尝试打开串口时,为什么会出现"CreateFile Failed: 161“?

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

Stack Overflow用户
提问于 2013-02-08 07:15:44
回答 1查看 1.7K关注 0票数 4

我在serialPort.Open()行得到"CreateFile Failed: 161“:

代码语言:javascript
复制
. . .
      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()"); 
}

我之所以知道这一点,是因为我看到的最后一个"debug msg“是”超出了PrintUtils.PrintBarcode()中的协议赋值“。

是否因为其中一个协议错误或格式不正确?或者,我是否忽略了所需的协议分配?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-08 08:04:09

错误161表示为The specified path is invalid.,您将得到它,因为您的端口名称无效。

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

代码语言:javascript
复制
serialPort.PortName = "COM1:"; 
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14762929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档