Beispiel #1
0
        public bool SendAndReceive(ref PacketSpinel97 txPacket, out PacketSpinel97 rxPacket)
        {
            txPacket.SIG = fSIG++;

            if (!bwListen.IsBusy)
            {
                byte[] txData = txPacket.GetBin();
                ci.Write(txData, 0, txData.Length);

                return(Papouch.Spinel.Spinel97.Spinel97Utils.Receive(ref ci, out rxPacket, 250));
            }
            else
            {
                SpinelStackRec ssr = new SpinelStackRec(txPacket);
                packets_to_send.Enqueue(ssr);
                bool result = ssr.WaitFor(TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(200));

                rxPacket = ssr.rx_packet;
                return(result);
            }
        }
Beispiel #2
0
        private void bwListen_DoWork(object sender, DoWorkEventArgs e)
        {
            bwListen_stopped.Reset();

            int            i        = 0;
            int            del      = 0;
            int            len      = 0;
            PacketSpinel97 rxPacket = null;

            int bytesToRead = 0;

            while (!bwListen.CancellationPending)
            {
                if (packets_to_send.Count > 0)
                {
                    SpinelStackRec ssr = packets_to_send.Peek();
                    packets_to_send.Dequeue();

                    byte[] txData = ssr.tx_packet.GetBin();
                    ci.Write(txData, 0, txData.Length);

                    packets_to_receive.Add(ssr);

                    ssr.Sended();
                }

                System.Threading.Thread.Sleep(1);
                bytesToRead = (int)ci.BytesToRead();
                if (bytesToRead > 0)
                {
                    rx_buffer_index += ci.Read(ref rx_buffer, rx_buffer_index, bytesToRead);

                    Debug.Print("{0}/{1}", bytesToRead, rx_buffer_index);

                    i = 0;
                    while ((i + 8 < rx_buffer_index))
                    {
                        del = 0;
                        if ((rx_buffer[i] == 0x2A) && (rx_buffer[i + 1] == 0x61))
                        {
                            len = rx_buffer[i + 2] * 0x100 + rx_buffer[i + 3];

                            del = len + 4 + i;

                            if (del > rx_buffer_index)
                            {
                                break;
                            }

                            byte[] buffer = new byte[del];
                            Array.Copy(rx_buffer, buffer, del);

                            rxPacket = null;
                            Spinel97Utils.GetSpinelPacket(ref buffer, del, out rxPacket);
                            if (rxPacket != null)
                            {
                                Debug.Print(Papouch.Utils.PapUtils.ConvertBinToHex(buffer));
                                PacketReceive(ref rxPacket);
                            }
                        }

                        if (del > 0)
                        {
                            Array.Copy(rx_buffer, del, rx_buffer, 0, rx_buffer_index - del);
                            rx_buffer_index = rx_buffer_index - del;
                        }
                        else
                        {
                            i++;
                        }
                    }
                }
            }
        }