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(); }
private void ph_RX(DevicePacket packet, object sender) { if (RX != null) { RX(packet, sender); } }
public void TX(DevicePacket packet) { if (!TX_Buffer.Any(x => x.ID == packet.ID)) { TX_Buffer.Add(packet); } }
public void TX(DevicePacket packet) { lock (TX_Buffer) { TX_Buffer.Add(packet); } }
public TestEnumerable SetMotherBoard(DeviceID id) { var packet = new DevicePacket() { ID = id, ModuleType = ModuleTypeEnum.MotherBoard, }; var state = PicDeviceFactoryProvider.MotherBoardFactory.DeviceStateCreate(); state.BasePacket = packet; state.Data.ModuleType [0] = 0x10; //mb, sens return setStack(() => new [] { state }); }
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(id, data); 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) { } }
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); } } } }
void Peripherals_RX(DevicePacket packet, object sender) { IDevice device = (IDevice)sender; if (packet.ID == Convert.ToInt32(DashboardPackages.PACK_POTSINFO)) { // new potentiometer information! for (int i = 0; i < 6; i++) { Int16 current = BitConverter.ToInt16(packet.Data, i * 2); int diff = current - PotSettings[i]; PotSettings[i] = current; if (Math.Abs(diff) < 100) { if (diff != 0 && PotTurn != null) { PotTurn(i, ((diff > 0) ? 1 : -1), Math.Abs(diff)); } } } } }
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); }
//private Semaphore SerialPort_Lock = new Semaphore(1, 1); private void SendPackage(DashboardPackages package, byte[] data) { DevicePacket packet = new DevicePacket { Data = data, ID = Convert.ToInt16(package), Length = data.Length }; Telemetry.m.Peripherals.TX(packet, ""); /*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();*/ }
private void SendPackage(DashboardPackages package, byte[] data) { DevicePacket pk = new DevicePacket((int)package, data); 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(); }
void Switchboard_PotTurn(int pot, int direction, int steps) { return; // TODO: handle brake & throttle mapping int step = steps; switch (pot) { case 0: // brake bias break; case 1: // engine boost break; case 2: // engine revs if (Revs < 1000) { step *= 5; } else if (Revs > 1750) { step *= 50; } else { step *= 10; } if (direction == 1) { Revs += step; } else { Revs -= step; } DevicePacket MaximumRPM = new DevicePacket(Convert.ToInt32(DashboardPackages.PACK_PREFS_ENGINEREVS), BitConverter.GetBytes(Convert.ToInt32(Revs))); Telemetry.m.Peripherals.TX(MaximumRPM, "Dashboard"); break; case 3: // menu break; case 4: // throttle map break; case 5: // brake map // NOW CRUISE CONTROL if (CruiseControl < 50) { step *= 1; } if (CruiseControl < 100) { step *= 2; } else if (CruiseControl < 180) { step *= 5; } else { step *= 10; } if (direction == 1) { CruiseControl += step; } else { CruiseControl -= step; } DevicePacket Speed = new DevicePacket(Convert.ToInt32(DashboardPackages.PACK_PREFS_CRUISECONTROL), BitConverter.GetBytes(Convert.ToInt32(CruiseControl))); Telemetry.m.Peripherals.TX(Speed, "Switchboard"); Telemetry.m.Peripherals.TX(Speed, "Dashboard"); break; } }
public static byte[] Encode(this DevicePacket packet) => DevicePacketEncoding.Instance.Encode(packet);
public IObservable<System.Reactive.Unit> GetWritingPacket(DevicePacket pack) { throw new NotImplementedException(); }
public void WritePacket(DevicePacket packet) { // do nothing }