Beispiel #1
0
        public SerialPortTran(string PortName, bool needReceive)
        {
            this.PortName             = PortName;
            sp                        = new SerialPort(PortName, 19200, Parity.None, 8, StopBits.One);
            sp.ReceivedBytesThreshold = 5;
            sp.Handshake              = Handshake.None;
            sp.DtrEnable              = true;
            sp.ReadTimeout            = 2000;
            sp.WriteTimeout           = 2000;
            autoreceive               = needReceive;
            SerialPortFixer.Execute(PortName);
            if (sp.IsOpen)
            {
                sp.Close();
                sp.Open();
            }
            else
            {
                sp.Open();
            }

            if (autoreceive)
            {
                sp.DataReceived += new SerialDataReceivedEventHandler(ReceiveMessage);
            }
            connected = true;
        }
Beispiel #2
0
        public static bool IsTheDevice(string port)
        {
            SerialPortTran spt;

            try
            {
                SerialPortFixer.Execute(port);
                spt = new SerialPortTran(port, false);
                spt.SPWrite(Utils.HexToByte("681600000A"));
                string result = "";
                Thread.Sleep(100);
                result = spt.spReadOnce();
                spt.sp.Close();

                if (result.StartsWith("6817"))
                {
                    return(true);
                }
            }
            catch
            {
                return(false);
            }
            finally
            {
            }

            return(false);
        }