Esempio n. 1
0
 public void TX(DevicePacket packet)
 {
     lock (TX_Buffer)
     {
         TX_Buffer.Add(packet);
     }
 }
Esempio n. 2
0
 public void TX(DevicePacket packet, string destination)
 {
     if (destination == "")
     {
         foreach (Device ph in devices)
             ph.TX(packet);
     }
     else
     {
         foreach (Device ph in devices)
             if (ph.Name == destination)
                 ph.TX(packet);
     }
 }
Esempio n. 3
0
        private void SP_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                while (SP.BytesToRead > 0)
                {
                    byte[] bf = new byte[SP.BytesToRead];
                    SP.Read(bf, 0, bf.Length);
                    RX_Buffer.AddRange(bf);
                }
                // Search for packages
                for (int i = 0; i < RX_Buffer.Count; i++)
                {
                    if (RX_Buffer[i] == '$' && RX_Buffer[i + 1] == '&' && RX_Buffer.Count - i > 4)
                    {

                        int length = BitConverter.ToUInt16(RX_Buffer.ToArray(), i + 2);
                        int id = RX_Buffer[i + 4];
                        byte[] data = new byte[length];
                        if (length + i > RX_Buffer.Count && length < 128)
                            break;

                        ByteMethods.memcpy(data, RX_Buffer.ToArray(), length, 0, i + 5);

                        DevicePacket packet = new DevicePacket{Data=data, ID=id, Length=length};
                        if (RX != null) RX(packet, this);

                        // Done
                        if (length + i > RX_Buffer.Count)
                            RX_Buffer.Clear();
                        else
                            RX_Buffer.RemoveRange(i, length + 5);

                    }

                }
                if (RX_Buffer.Count > 300)
                    RX_Buffer.Clear();
            }
            catch (Exception ex) { }
        }
Esempio n. 4
0
        private void TX_Sync(DevicePacket devicePacket)
        {
            SerialPort_Lock.WaitOne();
            try
            {
                DashboardPacket packet = new DashboardPacket();
                packet.Sync1 = '$';
                packet.Sync2 = '&';
                packet.length = (UInt16)(devicePacket.Length);
                packet.id = (byte)devicePacket.ID;
                packet.crc = 0xAA;
                for (int i = 0; i < devicePacket.Data.Length; i++)
                    packet.crc += devicePacket.Data[i];

                // Write header
                byte[] bPacket = ByteMethods.ToBytes(packet);
                SP.Write(bPacket, 0, 6);

                // Write packet data
                SP.Write(devicePacket.Data, 0, devicePacket.Length);

                // Write junk incase bytes are missed on embedded system.
                // This way there is a better chance the next package is received.
                byte[] bf = new byte[2];
                bf[0] = 0;
                bf[1] = 0;
                SP.Write(bf, 0, 2);
            }
            catch (Exception ex)
            { }
            SerialPort_Lock.Release();
        }
Esempio n. 5
0
        private void TX_Sync(DevicePacket devicePacket)
        {
            DashboardPacket packet = new DashboardPacket();
            packet.Sync1 = '$';
            packet.Sync2 = '&';
            packet.length = (UInt16)(devicePacket.Length);
            packet.id = (byte)devicePacket.ID;

            // Write header
            byte[] bPacket = ByteMethods.ToBytes(packet);
            SP.Write(bPacket, 0, 5);

            // Write packet data
            SP.Write(devicePacket.Data, 0, devicePacket.Length);

            // Write junk incase bytes are missed on embedded system.
            // This way there is a better chance the next package is received.
            byte[] bf = new byte[2];
            bf[0] = 0;
            bf[1] = 0;
            SP.Write(bf, 0, 2);
        }
Esempio n. 6
0
 private void ph_RX(DevicePacket packet, object sender)
 {
     if (RX != null)
         RX(packet, sender);
 }
Esempio n. 7
0
 public void TX(DevicePacket packet)
 {
     if(!TX_Buffer.Any(x => x.ID == packet.ID))
         TX_Buffer.Add(packet);
 }
Esempio n. 8
0
        private void TX_Sync(DevicePacket devicePacket)
        {
            SerialPort_Lock.WaitOne();
            try
            {
                DashboardPacket packet = new DashboardPacket();
                packet.Sync1 = '$';
                packet.Sync2 = '&';
                packet.length = (UInt16)(devicePacket.Length);
                packet.id = (byte)devicePacket.ID;
                packet.crc = 0xAA;
                foreach (var t in devicePacket.Data)
                    packet.crc += t;

                // Write header
                byte[] bPacket = ByteMethods.ToBytes(packet);
                SP.Write(bPacket, 0, 6);

                // Write packet data
                SP.Write(devicePacket.Data, 0, devicePacket.Length);

                byte[] dump = new byte[64];
                for (int lol = 0; lol < 64; lol++)
                    dump[lol] = 64;

                    SP.Write(dump, 0, 16);

            }
            catch (Exception ex)
            { }
            SerialPort_Lock.Release();
        }
Esempio n. 9
0
        private void SendPackage(DashboardPackages package, byte[] data)
        {
            DevicePacket pk = new DevicePacket { Data = data, ID = Convert.ToInt32(package), Length = data.Length };
            Telemetry.m.Peripherals.TX(pk, "");
            return;
            SerialPort_Lock.WaitOne();
            DashboardPacket packet = new DashboardPacket();
            packet.Sync1 = '$';
            packet.Sync2 = '&';
            packet.length = (UInt16)(data.Length);
            packet.id = (byte)package;

            byte[] bPacket = ByteMethods.ToBytes(packet);
            sp.Write(bPacket, 0, 5);
            sp.Write(data, 0, data.Length);
            byte[] bf = new byte[2];
            bf[0] = 0;
            bf[1] = 0;
            sp.Write(bf, 0, 2);
            SerialPort_Lock.Release();
        }