Beispiel #1
0
 public SerialPortMessage_EventArgs(MessageTypes mtp, ConnectingStatusTypes status, byte[] received_buffer, string comPortInfor)
 {
     MsgType            = mtp;
     ConectedStatus     = status;
     ComPortInformation = comPortInfor;
     ReceivedBuffer     = (byte[])received_buffer.Clone();
 }
Beispiel #2
0
        private ActionStatus OpenComPort(string portName)
        {
            bool error = false;

            gSerialComPort.BaudRate      = Default_BaudRate;
            gSerialComPort.DataBits      = Default_DataBits;
            gSerialComPort.StopBits      = Default_StopBits;
            gSerialComPort.Parity        = Default_Parity;
            gSerialComPort.PortName      = portName;
            gSerialComPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            try
            {
                // Open the port
                gSerialComPort.Open();
            }
            catch (UnauthorizedAccessException) { error = true; }
            catch (IOException) { error = true; }
            catch (ArgumentException) { error = true; }

            if (error)
            {
                //MessageBox.Show("Could not open the COM port.  Most likely it is already in use, has been removed, or is unavailable.", "COM Port Unavalible", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                CloseComPort();
                Connection = ConnectingStatusTypes.Disconnected;
                return(ActionStatus.Failure);
            }
            else
            {
                Connection = ConnectingStatusTypes.Connected;
                return(ActionStatus.Success);
            }
        }
Beispiel #3
0
 private void CloseComPort()
 {
     try
     {
         if (gSerialComPort.IsOpen)
         {
             Connection = ConnectingStatusTypes.Disconnected;
             gSerialComPort.DataReceived -= new SerialDataReceivedEventHandler(port_DataReceived);
             gSerialComPort.DiscardInBuffer();
             gSerialComPort.DiscardOutBuffer();
             gSerialComPort.Close();
         }
     }
     catch (Exception ex)
     {
         string str = ex.ToString();
     }
 }