Example #1
0
        private void CheckConnection()
        {
            if (this.Project.PortName == null)
            {
                pbConnectionStatus.Image = TicketPrinter.Properties.Resources.status_error;
                lblConnectionStatus.Text = "No connection set up.";
                this.isConnected         = false;
            }

            else if (this.connection == null)
            {
                // Try connecting
                string[]      portNames    = SerialPort.GetPortNames();
                List <string> portNameList = new List <string>(portNames);
                if (portNameList.Contains(this.Project.PortName))
                {
                    this.connection = new SerialPort(this.Project.PortName);
                    UsbUtil.PrepareSerialPort(this.connection);
                    TryEstablishConnection(false);
                }
                else
                {
                    pbConnectionStatus.Image = TicketPrinter.Properties.Resources.status_error;
                    lblConnectionStatus.Text = this.Project.PortName + " not connected. Check setup or cable";
                    this.isConnected         = false;
                }
            }
            else if (!this.connection.IsOpen)
            {
                TryEstablishConnection(true);
            }

            UpdateLayoutConnected();
        }
        private void btnConnect_Click(object sender, EventArgs e)
        {
            port = new SerialPort(this.lbPortName.SelectedItem.ToString());
            UsbUtil.PrepareSerialPort(port);
            port.ReadTimeout   = 500;
            port.WriteTimeout  = 500;
            port.DataReceived += WriteReceivedData;
            port.PinChanged   += port_PinChanged;

            port.ErrorReceived         += port_ErrorReceived;
            port.WriteBufferSize        = 16;
            port.ReadBufferSize         = 14;
            port.ReceivedBytesThreshold = 1;
            port.Open();

            tbLog.Text = "[TicketPrinterTest] Connected to printer on port " + this.lbPortName.SelectedItem.ToString();
            updateConnectLayout();
        }