예제 #1
0
        public static Boolean bleSerialRead(ref DataSerialBuffer dsbuffParser)
        {
            if (dsbuff.length > 0)
            {
                //DataSerialBuffer dsbuffParser = new DataSerialBuffer();
                Buffer.BlockCopy(dsbuff.values, dsbuff.idx_read, dsbuffParser.values, 0, dsbuff.length);
                dsbuffParser.length = dsbuff.length;
                dsbuff.idx_read    += dsbuff.length;
                dsbuff.length      -= dsbuff.length;

                if (dsbuff.idx_read == dsbuff.idx_write)
                {
                    dsbuff.idx_read  = 0;
                    dsbuff.idx_write = 0;
                    dsbuff.length    = 0;
                }



                for (int i = 0; i < dsbuffParser.length; i++)
                {
                    Console.Write((char)dsbuffParser.values[i]);
                }
                return(true);
            }
            return(false);
        }
예제 #2
0
        public static Boolean setiBeaconDeployMode()
        {
            String str_at_cmd = "AT+DISC?";

            str_at_cmd.Trim();

            byte[] at_cmd = Encoding.ASCII.GetBytes(str_at_cmd);

            if (_serialPort.IsOpen)
            {
                try
                {
                    _serialPort.Write(at_cmd, 0, at_cmd.Length);
                    Thread.Sleep(1000);

                    DataSerialBuffer dsbuffParser = new DataSerialBuffer();

                    Boolean dataAvai = bleSerialRead(ref dsbuffParser);
                    if (dataAvai)
                    {
                        Console.WriteLine("\nPayload");
                        for (int i = 0; i < dsbuffParser.length; i++)
                        {
                            Console.Write((char)dsbuffParser.values[i]);
                        }

                        Console.WriteLine("");


                        byte[] byteBuff = new byte[dsbuffParser.length];
                        Buffer.BlockCopy(dsbuffParser.values, 0, byteBuff, 0, dsbuffParser.length);
                        String strBuff = ToString(byteBuff);

                        String[] strArrBuff = strBuff.Split('+');
                        if (strArrBuff[0].Equals("OK"))
                        {
                            Console.WriteLine("iBeacon Deploy mode : OK");
                            return(true);
                        }
                        else
                        {
                            Console.WriteLine("iBeacon Deploy mode : Failed");
                            return(false);
                        }
                    }
                    Console.WriteLine("iBeacon Deploy mode : Failed");
                    return(false);
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0}", e);
                    throw;
                }
            }

            return(false);
        }
예제 #3
0
        public static Boolean getMacAddress()
        {
            String str_at_cmd = "AT+ADDR?";

            str_at_cmd.Trim();

            byte[] at_cmd = Encoding.ASCII.GetBytes(str_at_cmd);

            _serialPort.Write(at_cmd, 0, at_cmd.Length);
            Thread.Sleep(200);

            DataSerialBuffer dsbuffParser = new DataSerialBuffer();

            Boolean dataAvai = bleSerialRead(ref dsbuffParser);

            if (dataAvai)
            {
                Console.WriteLine("\nPayload");
                for (int i = 0; i < dsbuffParser.length; i++)
                {
                    Console.Write((char)dsbuffParser.values[i]);
                }

                Console.WriteLine("");


                byte[] byteBuff = new byte[dsbuffParser.length];
                Buffer.BlockCopy(dsbuffParser.values, 0, byteBuff, 0, dsbuffParser.length);
                String strBuff = ToString(byteBuff);

                String[] strArrBuff = strBuff.Split('+');
                if (strArrBuff[0].Equals("OK"))
                {
                    String[] strArrAckMAC = strArrBuff[1].Split(':');
                    if (strArrAckMAC[0].Equals("ADDR"))
                    {
                        byte[] toBytes = Encoding.ASCII.GetBytes(strArrAckMAC[1]);
                        Console.WriteLine("MAC Parser: {0}", strArrAckMAC[1]);
                        return(true);
                    }
                }
            }
            Console.WriteLine("BLE no ack {0}", str_at_cmd);
            return(false);
        }
예제 #4
0
        public static Boolean connectPeripheral(String peripheralMaxAdr)
        {
            String str_at_cmd = "AT+CON" + peripheralMaxAdr;

            str_at_cmd = str_at_cmd.Trim();

            byte[] at_cmd = Encoding.ASCII.GetBytes(str_at_cmd);

            _serialPort.Write(at_cmd, 0, at_cmd.Length);
            Thread.Sleep(400);

            DataSerialBuffer dsbuffParser = new DataSerialBuffer();

            Boolean dataAvai = bleSerialRead(ref dsbuffParser);

            if (dataAvai)
            {
                Console.WriteLine("\nPayload");
                for (int i = 0; i < dsbuffParser.length; i++)
                {
                    Console.Write((char)dsbuffParser.values[i]);
                }

                Console.WriteLine("");


                byte[] byteBuff = new byte[dsbuffParser.length];
                Buffer.BlockCopy(dsbuffParser.values, 0, byteBuff, 0, dsbuffParser.length);
                String strBuff = ToString(byteBuff);

                String[] strArrBuff = strBuff.Split('+');
                if (strArrBuff[0].Equals("OK"))
                {
                    //String[] strArrKeyValue = strArrBuff[1].Split(':');
                    if (strArrBuff[1].Equals("CONNA") || strArrBuff[1].Equals("CONNAOK"))//CONNAOK
                    {
                        Console.WriteLine("\nConnected Peripheral\n");
                        Thread.Sleep(200);
                        countTime = 7;
                        int intCount = 0;
                        while (true)
                        {
                            Thread.Sleep(150);
                            if (dataAvai = bleSerialRead(ref dsbuffParser))
                            {
                                byte[] byteData = new byte[dsbuffParser.length];
                                Buffer.BlockCopy(dsbuffParser.values, 0, byteData, 0, dsbuffParser.length);
                                if (!checkBLEConnectLost(ref byteData))
                                {
                                    return(false);
                                }
                                else
                                {
                                    if ((netServer.writeBuff.length >= netServer.writeBuff.values.Length) || (netServer.writeBuff.idx_write >= netServer.writeBuff.values.Length))
                                    {
                                        netServer.writeBuff.idx_write = 0;
                                        netServer.writeBuff.length    = 0;
                                        Console.WriteLine("Buffer Overflow(length or idx_write)");
                                    }
                                    else
                                    {
                                        Buffer.BlockCopy(byteData, 0, netServer.writeBuff.values, netServer.writeBuff.idx_write, dsbuffParser.length);
                                        netServer.writeBuff.idx_write += dsbuffParser.length;
                                        netServer.writeBuff.length    += dsbuffParser.length;
                                    }
                                }
                            }

                            //if ((countTime--) == 0)
                            //{

                            //    //Connected
                            //    //Thread.Sleep(1000);
                            //    intCount += 1;
                            //    String strSend = "BLEtest" + string.Format("{0}", intCount);
                            //    strSend = strSend.Trim();
                            //    byte[] b_data = Encoding.ASCII.GetBytes(strSend);
                            //    sendBLEData(ref b_data, b_data.Length);
                            //    countTime = 10;
                            //}


                            if (netServer.readBuff.length > 0)
                            {
                                sendBLEData(ref netServer.readBuff.values, netServer.readBuff.length);
                                netServer.readBuff.idx_read += netServer.readBuff.length;
                                netServer.readBuff.length   -= netServer.readBuff.length;



                                if ((netServer.readBuff.idx_read < 0) || (netServer.readBuff.length < 0))
                                {
                                    netServer.readBuff.idx_read = 0;
                                    netServer.readBuff.length   = 0;
                                }

                                if (netServer.readBuff.idx_read == netServer.readBuff.idx_write)
                                {
                                    netServer.readBuff.idx_write = 0;
                                    netServer.readBuff.idx_read  = 0;
                                    netServer.readBuff.length    = 0;
                                }
                            }
                        }
                    }
                }
            }
            Console.WriteLine("BLE no ack {0}", str_at_cmd);
            return(false);
        }
예제 #5
0
        public static Boolean getPeripheral(ref List <BLEInfo> BLEInfoArg)
        {
            String str_at_cmd = "AT+DISI?";

            str_at_cmd.Trim();
            BLEInfo BLEInfoBuff = new BLEInfo();

            byte[] at_cmd = Encoding.ASCII.GetBytes(str_at_cmd);

            _serialPort.Write(at_cmd, 0, at_cmd.Length);
            Thread.Sleep(4000);

            DataSerialBuffer dsbuffParser = new DataSerialBuffer();

            Boolean dataAvai = bleSerialRead(ref dsbuffParser);

            if (dataAvai)
            {
                Console.WriteLine("\nPayload");
                for (int i = 0; i < dsbuffParser.length; i++)
                {
                    Console.Write((char)dsbuffParser.values[i]);
                }

                Console.WriteLine("");


                byte[] byteBuff = new byte[dsbuffParser.length];
                Buffer.BlockCopy(dsbuffParser.values, 0, byteBuff, 0, dsbuffParser.length);
                String strBuff = ToString(byteBuff);

                //string[] stringSeparators = new string[] { "OK" };
                //String[] strArrBuff = strBuff.Split(stringSeparators, StringSplitOptions.None);


                String[] strArrBuff = strBuff.Split('+');
                if (strArrBuff[0].Equals("OK"))
                {
                    string[] stringSeparators = new string[] { "OK" };
                    strArrBuff = strBuff.Split(stringSeparators, StringSplitOptions.None);
                    List <String[]> arrKeyValue = new List <string[]>();
                    for (int i = 0; i < strArrBuff.Length; i++)
                    {
                        int count = strArrBuff[i].Split(':').Length;
                        if (count == 6)
                        {
                            arrKeyValue.Add(strArrBuff[i].Split(':'));
                        }
                    }

                    foreach (String[] key in arrKeyValue)
                    {
                        Console.WriteLine("{0},{1},{2},{3},{4}", key[1], key[2], key[3], key[4], key[5]);
                        BLEInfoBuff           = new BLEInfo();
                        BLEInfoBuff.factoryID = key[1];
                        BLEInfoBuff.iBeaconID = key[2];
                        //BLEInfoBuff.majorValue = key[3].Substring(0, 4);
                        //BLEInfoBuff.minorValue = key[3].Substring(4, 4);
                        //BLEInfoBuff.measuredPwr = key[3].Substring(8, 2);
                        BLEInfoBuff.macAdr = key[4];
                        BLEInfoBuff.rssi   = key[5];

                        BLEInfoArg.Add(BLEInfoBuff);
                        //
                    }

                    if (arrKeyValue.Count > 0)
                    {
                        return(true);
                    }
                }
            }
            Console.WriteLine("BLE no ack {0}", str_at_cmd);
            return(false);
        }
예제 #6
0
        public static Boolean getModeOperate()
        {
            String str_at_cmd = "AT+ROLE?";

            str_at_cmd.Trim();

            byte[] at_cmd = Encoding.ASCII.GetBytes(str_at_cmd);

            _serialPort.Write(at_cmd, 0, at_cmd.Length);
            Thread.Sleep(200);

            DataSerialBuffer dsbuffParser = new DataSerialBuffer();

            Boolean dataAvai = bleSerialRead(ref dsbuffParser);

            if (dataAvai)
            {
                Console.WriteLine("\nPayload");
                for (int i = 0; i < dsbuffParser.length; i++)
                {
                    Console.Write((char)dsbuffParser.values[i]);
                }

                Console.WriteLine("");

                byte[] byteBuff = new byte[dsbuffParser.length];
                Buffer.BlockCopy(dsbuffParser.values, 0, byteBuff, 0, dsbuffParser.length);
                String strBuff = ToString(byteBuff);

                String[] strArrBuff = strBuff.Split('+');
                if (strArrBuff[0].Equals("OK"))
                {
                    String[] strArrKeyValue = strArrBuff[1].Split(':');
                    if (strArrKeyValue[0].Equals("Get"))
                    {
                        byte[] toBytes = Encoding.ASCII.GetBytes(strArrKeyValue[1]);
                        Console.WriteLine("Mode Operation Parser: {0}", strArrKeyValue[1]);
                        if (toBytes[0] == (byte)'1')
                        {
                            Console.WriteLine("Mode : Central");
                            return(true);
                        }
                        else if (toBytes[0] == (byte)'0')
                        {
                            Console.WriteLine("Mode : Peripheral");
                            return(false);
                        }
                    }
                    else
                    {
                        Console.WriteLine("BLE no ack {0}", str_at_cmd);
                        return(false);
                    }
                }
                else
                {
                    Console.WriteLine("BLE no ack {0}", str_at_cmd);
                    return(false);
                }
            }
            Console.WriteLine("BLE no ack {0}", str_at_cmd);
            return(false);
        }
예제 #7
0
        public static Boolean getAT()
        {
            String str_at_cmd = "AT";

            str_at_cmd.Trim();
            Boolean dataAvai = false;

            Boolean reGetAT = false;

            byte[] at_cmd = Encoding.ASCII.GetBytes(str_at_cmd);

            if (_serialPort.IsOpen)
            {
                try
                {
                    _serialPort.Write(at_cmd, 0, at_cmd.Length);
                    Thread.Sleep(200);

                    DataSerialBuffer dsbuffParser = new DataSerialBuffer();

                    dataAvai = bleSerialRead(ref dsbuffParser);

                    byte[] byteBuff = new byte[dsbuffParser.length];
                    Buffer.BlockCopy(dsbuffParser.values, 0, byteBuff, 0, dsbuffParser.length);
                    String strBuff = ToString(byteBuff);

                    if (dataAvai)
                    {
                        if (strBuff.Length == 2)
                        {
                            if (strBuff.Equals("OK") == true)
                            {
                                Console.WriteLine("{0} : BLE ack AT.", dataAvai);
                                return(true);
                            }
                            else
                            {
                                Console.WriteLine("{0} : BLE no ack AT.", dataAvai);
                                return(false);
                            }
                        }
                        else
                        {
                            Console.WriteLine("{0} : Length Over AT at 2Byte : {1}.", dataAvai, strBuff.Length);
                            reGetAT = true;

                            return(false);
                        }

                        //if (strBuff.Length  > 2)
                        //{
                        //    if (strBuff.Equals("OK+LOSS") == true)
                        //    {
                        //        Console.WriteLine("{0} : BLE ack AT is loss.", dataAvai);
                        //        return false;
                        //    }
                        //    return false;
                        //}
                        //else
                        //{

                        //    Console.WriteLine("{0} : Length Over AT at 2Byte : {1}.", dataAvai, strBuff.Length);
                        //    return false;
                        //}
                    }
                    else
                    {
                        Console.WriteLine("{0} : BLE no ack AT.", dataAvai);
                        return(false);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0} : BLE no ack AT.", e);
                    return(false);
                }
            }

            //if (_serialPort.IsOpen && reGetAT)
            //{
            //    try
            //    {
            //        _serialPort.Write(at_cmd, 0, at_cmd.Length);
            //        Thread.Sleep(200);

            //        DataSerialBuffer dsbuffParser = new DataSerialBuffer();

            //        dataAvai = bleSerialRead(ref dsbuffParser);

            //        byte[] byteBuff = new byte[dsbuffParser.length];
            //        Buffer.BlockCopy(dsbuffParser.values, 0, byteBuff, 0, dsbuffParser.length);
            //        String strBuff = ToString(byteBuff);

            //        if (dataAvai)
            //        {
            //            if (strBuff.Length == 2)
            //            {
            //                if (strBuff.Equals("OK") == true)
            //                {
            //                    Console.WriteLine("{0} : BLE ack AT.", dataAvai);
            //                    return true;
            //                }
            //                else
            //                {
            //                    Console.WriteLine("{0} : BLE no ack AT.", dataAvai);
            //                    return false;
            //                }
            //            }
            //            else
            //            {
            //                Console.WriteLine("{0} : Length Over AT at 2Byte : {1}.", dataAvai, strBuff.Length);
            //                reGetAT = true;

            //                return false;
            //            }

            //            if (strBuff.Length > 2)
            //            {
            //                if (strBuff.Equals("OK+LOSS") == true)
            //                {
            //                    Console.WriteLine("{0} : BLE ack AT is loss.", dataAvai);
            //                    return false;
            //                }
            //                return false;
            //            }
            //            else
            //            {

            //                Console.WriteLine("{0} : Length Over AT at 2Byte : {1}.", dataAvai, strBuff.Length);
            //                return false;
            //            }

            //        }
            //        else
            //        {
            //            Console.WriteLine("{0} : BLE no ack AT.", dataAvai);
            //            return false;
            //        }
            //    }
            //    catch (Exception e)
            //    {
            //        Console.WriteLine("{0} : BLE no ack AT.", e);
            //        return false;
            //    }
            //}


            Console.WriteLine("{0} : BLE no ack AT.", dataAvai);
            return(false);
        }
예제 #8
0
        static void Main(string[] args)
        {
            BLEComportConfigCurrent = new BLEComportConfig();
            dsbuff = new DataSerialBuffer();


            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;

            //Thread readThread = new Thread(Read);

            // Create a new SerialPort object with default settings.
            //_serialPort = new SerialPort();



            //netServer = new Network();

            //netThread = new Thread(new ThreadStart(netServer.ListenForClients));
            //netThread.Start();

            //byte[] buffer = new byte[] { (byte)'A', (byte)'B', (byte)'C', (byte)'D' };
            //Buffer.BlockCopy(buffer, 0, netServer.writeBuff.values, netServer.writeBuff.idx_write, buffer.Length);
            //netServer.writeBuff.idx_write += buffer.Length;
            //netServer.writeBuff.length += buffer.Length;

            while (true)
            {
                netServer = new Network();

                try
                {
                    netThread = new Thread(new ThreadStart(netServer.ListenForClients));
                    netThread.Start();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Main Network Thread: {0}", e);
                }

                if (scanBLESerialPort())
                {
                    Console.WriteLine("Found have has central ble device({0}).", BLEComportConfigCurrent.PortName);

                    //iBeacon Deploy mode
                    if (setiBeaconDeployMode())
                    {
                        //Scan Peripheral
                        if (getPeripheral(ref BLEPeripheralTable))
                        {
                            for (int i = 0; i < BLEPeripheralTable.Count; i++)
                            {
                                int rssiInt = Int16.Parse(BLEPeripheralTable[i].rssi);
                                if (rssiInt >= rssiRangeConnect)
                                {
                                    if (connectPeripheral(BLEPeripheralTable[i].macAdr))
                                    {
                                    }
                                }
                            }
                        }
                    }
                }

                if (readThread.IsAlive)
                {
                    readThread.Abort();
                }

                if (_serialPort.IsOpen)
                {
                    _serialPort.Close();
                }

                if (netThread.IsAlive)
                {
                    netThread.Abort();
                }
            }

            if (readThread.IsAlive)
            {
                readThread.Abort();
            }

            if (_serialPort.IsOpen)
            {
                _serialPort.Close();
            }
        }