locked
Closing a Serial Port Connection RRS feed

  • Question

  • When I try to write my ZPL code to the serial port to print a label it it will not print anything unless put a System.Threading.Thread.Sleep(5000) in there right after the write.  Im guessing this is because I am closing the connection before everything has been writen to the printer.  Is there a way to close the connection once the write has completed?

    SerialPort serialPort = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One)
    serialPort.Handshake = System.IO.Ports.Handshake.None;
    serialPort.ReadTimeout = 25000;
    serialPort.WriteTimeout = 25000;

    byte[] sendData;
    sendData = Encoding.ASCII.GetBytes(sendString);

    serialPort.Open();
    serialPort.Write(sendData, 0, sendData.Length);

    System.Threading.Thread.Sleep(5000);

    serialPort.Close();

    Thursday, March 1, 2007 3:33 PM

Answers

  • Several things come to mind, first unless you're sending a massive amount of data, 5 seconds at 19.2k baud is a long time.  Also, the read and write timeouts are equally huge at 25 seconds a piece.  All of these can easily be sub-second, as the read and write timeouts are related to the amount of time between bytes before deciding there's nothing more.  So, maybe try shortening them to something reasonable and see if that helps. Another thing is, if you're trying to use BytesToWrite and its already zero, then the data's gone, and that's not the problem.
    Friday, March 2, 2007 1:10 PM

All replies

  • I also tried putting in a while loop checking the WriteBuffer size using serialPort.BytesToWrite but its always equal to 0 so I am not sure if I am implementing this correctly.

     

                    SerialPort serialPort = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
                   
                    serialPort.Handshake = System.IO.Ports.Handshake.None;
                    serialPort.ReadTimeout = 25000;
                    serialPort.WriteTimeout = 25000;

                    byte[] sendData;
                    sendData = Encoding.ASCII.GetBytes(sendString);
                       
                    serialPort.Open();
                    serialPort.Write(sendData, 0, sendData.Length);

                    while (!serialPort.BytesToWrite.Equals(0))
                    {
                       
                    }
                   
                    serialPort.Close();

    Thursday, March 1, 2007 4:08 PM
  • Several things come to mind, first unless you're sending a massive amount of data, 5 seconds at 19.2k baud is a long time.  Also, the read and write timeouts are equally huge at 25 seconds a piece.  All of these can easily be sub-second, as the read and write timeouts are related to the amount of time between bytes before deciding there's nothing more.  So, maybe try shortening them to something reasonable and see if that helps. Another thing is, if you're trying to use BytesToWrite and its already zero, then the data's gone, and that's not the problem.
    Friday, March 2, 2007 1:10 PM