Why am I trying to open the serial port "CreateFile Failed: 161"?

I get "CreateFile Failed: 161" in 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 since the last "debug msg" that I see "makes it outside of the protocol assignments in PrintUtils.PrintBarcode ()"

Is it because one of the protocols is incorrect or in a bad format? Or did I omit the required protocol assignment?

+4
source share
1 answer

Error 161 means The specified path is invalid. , and you get it because your port name is invalid.

Windows CE requires port names (all driver names in fact) to be marked with a ':', so your code should be:

 serialPort.PortName = "COM1:"; 
+4
source

All Articles