4

I get "CreateFile Failed: 161" on the serialPort.Open() line:

. . .
      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()"); 
}

I know that because the last "debug msg" I see is "Made it beyond the protocol assignments in PrintUtils.PrintBarcode()"

Is it because one of the protocols is wrong or in a bad format? Or have I omitted a required protocol assignment?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • What is the type of the exception that is being thrown? Take a look at the [MSDN SerialPort.Open page](http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.open.aspx) to help correlate the exception type with what the reason may be. – JG in SD Feb 07 '13 at 23:18
  • System.ArgumentException Value does not fall within the expected range – B. Clay Shannon-B. Crow Raven Feb 07 '13 at 23:40
  • What I see at that msdn page is: The port name does not begin with "COM". - or - The file type of the port is not supported. Since the port's name is "COM1" perhaps the second reason? But what does it mean? What is the "file type" of a port? – B. Clay Shannon-B. Crow Raven Feb 07 '13 at 23:42

1 Answers1

5

Error 161 means The specified path is invalid. and you're getting it because your port name is invalid.

Windows CE requires that port names (all driver names actually) be suffixed with a ':' character, so your code should be:

serialPort.PortName = "COM1:"; 
ctacke
  • 66,480
  • 18
  • 94
  • 155
  • Thanks, OpenNETCF man! I've been battling this all day, and this is the first reference I've seen to that apparently arcane requirement. Now my problem is that the Zebra device turns off in the middle of the code (I turn it on, light goes green, and in the middle of those calls to send data via the serial port, it turns off...?!?!?) – B. Clay Shannon-B. Crow Raven Feb 08 '13 at 00:27