locked
Send command to printer serial port RRS feed

  • Question

  • Hello,

    I have a printer installed on Windows XP which resides on a serial port (COM3).

    I would like to send a command in port in order to get various info by using the manual provided by the printer vendor.

    In order to achieve this, I create a new SerialPort port object with the correct properties (baudRate, port name, etc). However when I'm trying to open the port the following exception is thrown: "The given port name does not start with COM/com or does not resolve to a valid serial port. Parameter name: portName."

    The printer's port name is set correctly to the correct one (COM3).

    How is it possible to send a command to a printer serial port?

    Thanks,

    Felicia

    • Moved by Bob Shen Monday, September 3, 2012 9:27 AM (From:Visual C# General)
    Tuesday, August 21, 2012 2:22 PM

All replies

  • Perhaps if you show a little code we can have better luck.

    From the little you describe, it sounds a bit like you are doing something like

    string portName = "COM3";
    SerialPort port = new SerialPort("portName");
    port.Open();
    

    (Note the superfluous quotes around portName in the constructor).

    For now, I would create a dummy C# console project and in the Main function just add a couple of lines of code to open the port, send a command, sleep for a second and see if you get a response.  Once you get that working, then try to integrate it into your big project.

    Tuesday, August 21, 2012 2:31 PM
  • A code sample is the following:

    I have commented out the sending command lines, since the problem occurs while trying to open the port.

                var port = new SerialPort();
                port.BaudRate = 38400;
                port.DataBits = 8;
                port.Parity = Parity.None;
                port.PortName = "COM3";
                port.StopBits = StopBits.One;
    
                try
                {
                    port.Open();
                    Console.WriteLine("Successfully opened port");
    				
    		////var array = new byte[] { 10, 04, 04 };
                    ////port.Write(array, 0, array.Length);
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }


    Tuesday, August 21, 2012 2:42 PM
  • What does the following program output?

    using System;
    using System.Linq;
    using System.IO.Ports;
    namespace ConsoleApplication2
    {
      class Program
      {
        static void Main(string[] args)
        {
          string[] ports = SerialPort.GetPortNames();
          if (ports.Contains("COM3"))
          {
            SerialPort port = new SerialPort("COM3",38400,Parity.None,8,StopBits.One);
            try
            {
              port.Open();
              Console.WriteLine("Successfully opened port");
            }
            catch(Exception ex)
            {
              Console.WriteLine(ex.Message);
            }
          }
          else
            Console.WriteLine("Port not found");
          Console.ReadLine();
        }
      }
    }

    Tuesday, August 21, 2012 4:10 PM
  • The same exception is being thrown by using the program:

    The given port name does not start with COM/com or does not resolve to a valid serial port. Parameter name: portName.

    Wednesday, August 22, 2012 7:54 AM
  • The same exception is being thrown by using the program:

    The given port name does not start with COM/com or does not resolve to a valid serial port. Parameter name: portName.

    Copy and post the output of this program:

    using System;
    using System.IO.Ports;
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          foreach (string port in SerialPort.GetPortNames())
            Console.WriteLine(port);
          Console.ReadLine();
        }
      }
    }

    Wednesday, August 22, 2012 8:03 AM
  • The same exception is being thrown by using the program:

    The given port name does not start with COM/com or does not resolve to a valid serial port. Parameter name: portName.

    Copy and post the output of this program:

    using System;
    using System.IO.Ports;
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          foreach (string port in SerialPort.GetPortNames())
            Console.WriteLine(port);
          Console.ReadLine();
        }
      }
    }

    The output is the following:

    COM1
    COM2
    COM3
    COM4
    COM5
    COM6
    COM7
    COM8
    COM9
    COM10

    Wednesday, August 22, 2012 8:17 AM
  • Try HyperTerminal.
    Wednesday, August 22, 2012 8:27 AM
  • By using HyperTerminal, I get an similar error, "Unable to open port". What I understand is that is being "used" from printer, since the com port is the correct one. However, there should be a way to access the port of the printer somehow.

    Wednesday, August 22, 2012 8:57 AM
  • You'll have to ask the vendor of the printer.
    Wednesday, August 22, 2012 11:07 AM
  • Hi Felicia V,

    According to your description, I'd like to move this thread to .NET Framework Networking and Communication Forum for better support, where more experts live.
     
    Thanks for your understanding.


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

    Monday, September 3, 2012 9:27 AM
  • Sounds as if some other program has the COM3 device open.  Get SysInternal's ProcExp.exe and see what Find->Handle returns for "COM3".

    http://www.alanjmcf.me.uk/ Please follow-up in the newsgroup. If I help, please vote and/or mark the question answered. Available for contract programming.

    Monday, September 3, 2012 8:38 PM
  • Hi Felicia,

    Have you got the printer installed under 'printers and faxes'? If so, it appears that print spooler has control of the com port & you are denied direct access.

    Neil.


    Neil Arnold

    Friday, October 12, 2012 10:19 AM