Exemplo n.º 1
0
        private static bool SendByte(byte data)
        {
            _frames.Clear();
            _sp.SendData(new byte[] { 0x96, data }, 0, 2);
            DateTime start = DateTime.Now;

            while (_frames.Count <= 0)
            {
                TimeSpan interval = DateTime.Now - start;
                if (interval.TotalMilliseconds > Interval)
                {
                    Console.WriteLine("Receive verify code timeout");
                    return(false);
                }
                Thread.Sleep(Interval / 5);
            }
            if (_frames[0].Data != (byte)~data)
            {
                Console.WriteLine(string.Format("Receive verify code wrong, Send: {0:X2}, Recv: {1:X2}", data, _frames[0].Data));
                return(false);
            }
            else
            {
                return(true);
            }
        }
Exemplo n.º 2
0
        private void SendSerialData(byte[] data, int offset, int count)
        {
            _sp.SendData(data, offset, count);
            string log = string.Empty;

            foreach (byte item in data)
            {
                log += item.ToString("X2") + " ";
            }
            //#if DEBUG
            Console.WriteLine("Send: " + log);
            //#endif
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            _sp = new SerialPortClass("COM6", 9600, Parity.None, 8, StopBits.One);
            try {
                _sp.OpenPort();
                _sp.DataReceived += new SerialPortClass.SerialPortDataReceiveEventArgs(SerialDataReceived);
                _frames           = new List <Frame>();
                Console.WriteLine("Waiting for command");
            } catch (Exception ex) {
                Console.WriteLine("Open serial port error: " + ex.Message);
                throw;
            }
            try {
                while (true)
                {
                    if (_frames.Count > 0)
                    {
                        if (_frames.First().Baud == 0x05 && _frames.First().Data == 0x83)
                        {
                            _sp.SendData(new byte[] { 0x96, 0x55 }, 0, 2);
                            _sp.SendData(new byte[] { 0x96, 0x01 }, 0, 2);
                            _sp.SendData(new byte[] { 0x96, 0x8A }, 0, 2);
                        }
                        else if (_frames.First().Baud == 0x96 && _frames.First().Data == 0x75)
                        {
                            ++_0300Times;
                            SendData(0xF6, "ABSMadeByAPG");
                        }
                        else if (_frames.First().Baud == 0x96 && _frames.First().Data != null)
                        {
                            List <byte> cmd = new List <byte>();
                            RecvCMD(cmd);
                            switch (cmd[0])
                            {
                            case 0x03:
                                switch (cmd[2])
                                {
                                case 0x00:
                                    switch (++_0300Times)
                                    {
                                    case 1:
                                        SendData(0xF6, "ABSMadeByAPG");
                                        break;

                                    case 2:
                                        SendData(0xF6, "APG3550700E3");
                                        break;

                                    case 3:
                                        SendData(0xF6, "SWV ZHU V500");
                                        break;

                                    case 4:
                                        SendData(0xF6, new byte[] { 0x00, 0x07, 0xFA, 0x00, 0x01 });
                                        _0300Times = 0;
                                        break;
                                    }
                                    break;

                                case 0x05:
                                    // 不处理
                                    _0300Times = 0;
                                    SendData(0x09, Array.Empty <byte>());
                                    break;

                                case 0x06:
                                    // 系统复位退出
                                    _0300Times = 0;
                                    _0404Times = 0;
                                    _syncNum   = 0;
                                    break;

                                case 0x07:
                                    SendData(0xFC, new byte[] { 0x01, 0x1B, 0x05, 0x01, 0x1D, 0x05, 0x01, 0x22, 0x05, 0x01, 0x1F, 0x05, 0x01, 0x2D, 0x05, 0x01, 0x2E, 0x05 });
                                    _0300Times = 0;
                                    break;

                                case 0x09:
                                    // 不处理
                                    _0300Times = 0;
                                    break;

                                default:
                                    _0300Times = 0;
                                    break;
                                }
                                break;

                            case 0x04:
                                switch (cmd[2])
                                {
                                case 0x04:
                                    switch (++_0404Times)
                                    {
                                    case 1:
                                        SendData(0xF5, new byte[] { 0x04, 0xFC });
                                        break;

                                    case 2:
                                        SendData(0xF5, new byte[] { 0x02, 0xA1 });
                                        break;

                                    case 3:
                                        SendData(0xF5, new byte[] { 0x02, 0xA3 });
                                        break;

                                    case 4:
                                        SendData(0xF5, new byte[] { 0x02, 0xA4 });
                                        break;

                                    case 5:
                                        SendData(0xF5, new byte[] { 0x02, 0xA5 });
                                        break;

                                    case 6:
                                        SendData(0xF5, new byte[] { 0x02, 0xA6 });
                                        break;

                                    case 7:
                                        SendData(0xF5, new byte[] { 0x02, 0xA3 });
                                        break;

                                    case 8:
                                        SendData(0xF5, new byte[] { 0x02, 0xA2 });
                                        break;

                                    case 9:
                                        SendData(0xF5, new byte[] { 0x02, 0xA1 });
                                        break;

                                    case 10:
                                        SendData(0xF5, new byte[] { 0x02, 0xA8 });
                                        break;

                                    case 11:
                                        SendData(0xF5, new byte[] { 0x02, 0xA9 });
                                        break;

                                    case 12:
                                        SendData(0xF5, new byte[] { 0x02, 0xAA });
                                        break;

                                    case 13:
                                        SendData(0xF5, new byte[] { 0x02, 0xAB });
                                        break;

                                    case 14:
                                        SendData(0xF5, new byte[] { 0x02, 0xA8 });
                                        break;

                                    case 15:
                                        SendData(0xF5, new byte[] { 0x02, 0xA2 });
                                        break;

                                    case 16:
                                        SendData(0xF5, new byte[] { 0x02, 0xA1 });
                                        break;

                                    case 17:
                                        SendData(0xF5, new byte[] { 0x02, 0xAD });
                                        break;

                                    case 18:
                                        SendData(0xF5, new byte[] { 0x02, 0xAE });
                                        break;

                                    case 19:
                                        SendData(0xF5, new byte[] { 0x02, 0xAF });
                                        break;

                                    case 20:
                                        SendData(0xF5, new byte[] { 0x02, 0xB0 });
                                        break;

                                    case 21:
                                        SendData(0xF5, new byte[] { 0x02, 0xAD });
                                        break;

                                    case 22:
                                        SendData(0xF5, new byte[] { 0x02, 0xA2 });
                                        break;

                                    case 23:
                                        SendData(0xF5, new byte[] { 0x02, 0xA1 });
                                        break;

                                    case 24:
                                        SendData(0xF5, new byte[] { 0x02, 0xB2 });
                                        break;

                                    case 25:
                                        SendData(0xF5, new byte[] { 0x02, 0xB3 });
                                        break;

                                    case 26:
                                        SendData(0xF5, new byte[] { 0x02, 0xB4 });
                                        break;

                                    case 27:
                                        SendData(0xF5, new byte[] { 0x02, 0xB5 });
                                        break;

                                    case 28:
                                        SendData(0xF5, new byte[] { 0x02, 0xB2 });
                                        break;

                                    case 29:
                                        SendData(0x0A, new byte[] { 0xAF });
                                        _0404Times = 0;
                                        break;
                                    }
                                    break;

                                case 0x29:
                                    SendData(0xE7, new byte[] { 0x01, 0x01, 0x01, 0x01 });
                                    _0404Times = 0;
                                    break;

                                default:
                                    _0404Times = 0;
                                    break;
                                }
                                break;
                            }
                        }
                        _frames.Clear();
                    }
                }
            } catch (Exception ex) {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
                throw;
            }
        }