Exemplo n.º 1
0
        public static bool Init(out string message, WeighScaleType weighScaleType, string port, int baudRate, int dataBits = 8)
        {
            message = "";
            scaletype = weighScaleType;
            _serialPort = new SerialPort();
            if (!_serialPort.IsOpen)
            {
                _serialPort.PortName = port;
                _serialPort.BaudRate = baudRate;
                // _serialPort.Handshake = HandShake;
                _serialPort.DataBits = dataBits;
                _serialPort.Parity = Parity.None;
                _serialPort.StopBits = StopBits.One;
                _serialPort.ReadTimeout = 50000;
                try
                {

                    _serialPort.Open();
                    _isDeviceReady = true;
                }
                catch (IOException ioEx)
                {

                    _isDeviceReady = false;

                    _isDeviceReady = false;

                    message = "Serial Port Error\nDetails:" + ioEx.Message;
                }
                catch (ObjectDisposedException odEx) // This catch is optional
                {

                    _isDeviceReady = false;

                    message = "Serial Port Error\nDetails:" + odEx.Message;


                }
                catch (Exception exception)
                {
                    _isDeviceReady = false;

                    message = "Serial Port Error\nDetails:" + exception.Message;

                }
            }

            return _isDeviceReady;
        }
Exemplo n.º 2
0
        public void Init(WeighScaleType weighScaleType, string port, int baudRate,int dataBits=8 )
        {
            scaletype = weighScaleType;
            if(!_serialPort.IsOpen)
            {
                _serialPort.PortName = port;
                _serialPort.BaudRate = baudRate;
                _serialPort.Handshake = HandShake;
                _serialPort.DataBits = dataBits;
                _serialPort.Parity = Parity.None;
                _serialPort.StopBits = StopBits.One;
                _serialPort.ReadTimeout = 50000;
                try
                {

                    _serialPort.Open();
                    IsDeviceReady = true;
                }
                catch (IOException ioEx)
                {
                   
                    IsDeviceReady = false;
                   
                    IsDeviceReady = false;

                    MessageBox.Show("Serial Port Error\nDetails:" + ioEx.Message);
                }
                catch (ObjectDisposedException odEx) // This catch is optional
                {

                    IsDeviceReady = false;

                    MessageBox.Show("Serial Port Error\nDetails:" + odEx.Message);
                    
                   
                }
               catch (Exception exception)
                {
                    IsDeviceReady = false;
                   
                   MessageBox.Show("Serial Port Error\nDetails:"+exception.Message);
                    
                }
            }

            
        }