private void HandleRemoveAllowedResponse(MGBTCommandData cmd) { InvocationHelper.InvokeIfRequired(this, new Action(() => { AddLineToStatus("Removed device: " + MacBytesToString(cmd.data) + ", status: " + cmd.Status); })); }
public void SendCommand(MGBTCommandData cmd) { lock (lockObj) { commandToSend = cmd; } }
private void HandleGetClosestDevice(MGBTCommandData cmd) { if (cmd.Status == 0) { InvocationHelper.InvokeIfRequired(this, new Action(() => { List <MGBTDevice> devices = MGBTDevice.FromArray(cmd.data); if (devices.Count > 0) { string riderName = "ILLEGAL"; string address = MacBytesToString(devices[0].Address).ToUpperInvariant(); if (riderNames.ContainsKey(address)) { riderName = riderNames[address]; } closestDeviceLbl.Text = devices[0].ToString() + "; " + riderName; } })); } else { InvocationHelper.InvokeIfRequired(this, new Action(() => { AddLineToStatus(String.Format("Error response: {0}", cmd.Status)); })); } }
private void HandleGetCurrentTime(MGBTCommandData cmd) { try { var reader = new BinaryReader(new MemoryStream(cmd.data)); while (reader.BaseStream.Position < reader.BaseStream.Length) { UInt32 time = reader.ReadUInt32(); byte timeType = reader.ReadByte(); InvocationHelper.InvokeIfRequired(this, new Action(() => { AddLineToStatus($"Got time: {time}"); AddLineToStatus($"Of type: {timeType}"); })); } } catch (Exception ex) { InvocationHelper.InvokeIfRequired(this, new Action(() => { AddLineToStatus($"Exception: {ex}"); })); } }
private void button1_Click(object sender, EventArgs e) { MGBTCommandData data = new MGBTCommandData(); data.Status = 0x0000; data.CommandType = 0x0001; data.data = GetCommandData(macTbx.Text); SendCommand(data); }
private void UpdateAllowedDevicesBtn_Click(object sender, EventArgs e) { MGBTCommandData data = new MGBTCommandData(); data.Status = 0x0000; data.CommandType = 0x0003; data.data = new byte[2]; SendCommand(data); }
private void updateClosestDeviceBtn_Click(object sender, EventArgs e) { MGBTCommandData data = new MGBTCommandData(); data.Status = 0x0000; data.CommandType = 0x0005; data.data = new byte[2]; SendCommand(data); }
private void getLapsBtn_Click(object sender, EventArgs e) { MGBTCommandData data = new MGBTCommandData(); data.Status = 0x0000; data.CommandType = 102; data.data = new byte[2]; SendCommand(data); }
private void button2_Click(object sender, EventArgs e) { MGBTCommandData data = new MGBTCommandData(); data.Status = 0x0000; data.CommandType = 0x0002; data.data = TextToMacBytes(macTbx.Text); SendCommand(data); }
public MGBTCommandData GetLatestData() { MGBTCommandData data = null; if (rxQueue.TryDequeue(out data)) { return(data); } return(data); }
private void SetOpMode(int opMode) { MGBTCommandData data = new MGBTCommandData(); data.Status = 0x0000; data.CommandType = 105; data.data = new byte[2]; data.data[0] = (byte)opMode; SendCommand(data); }
private void Proto_NewDataArrived(object sender, EventArgs e) { //Not a pretty solution, too lazy to write a good one. This is a tester after all; while (proto.HasData) { MGBTCommandData cmd = proto.GetLatestData(); if (cmd != null) { switch (cmd.CommandType) { case 1: HandleAddAllowedResponse(cmd); break; case 2: HandleRemoveAllowedResponse(cmd); break; case 3: HandleListAllowedDevices(cmd); break; case 4: HandleListDetectedDevices(cmd); break; case 5: HandleGetClosestDevice(cmd); break; case 101: HandleGetCurrentTime(cmd); break; case 102: HandleGetLaps(cmd); break; case 103: HandleGetCurrentTime(cmd); break; case 104: break; case 255: HandleGetID(cmd); break; default: break; } } } }
private void HandleAddAllowedResponse(MGBTCommandData cmd) { InvocationHelper.InvokeIfRequired(this, new Action(() => { AddLineToStatus("Added device: " + MacBytesToString(cmd.data) + ", status: " + cmd.Status); if (addingMultiple) { SendNextMultiAdd(); } })); }
private void HandleListDetectedDevices(MGBTCommandData cmd) { switch (cmd.Status) { case 8: HandleListDetectedDevicesProgress(cmd.data); break; case 0: InvocationHelper.InvokeIfRequired(this, new Action(() => { AddLineToStatus("Started listing all devices"); })); break; case 1: if (cmd.data.Length > 2) { byte[] devicesData = new byte[cmd.data.Length - 2]; Array.Copy(cmd.data, 2, devicesData, 0, devicesData.Length); if (cmd.data[0] == 0) { InvocationHelper.InvokeIfRequired(this, new Action(() => { detectedDevicesLbx.Items.Clear(); HandleListDetectedDevicesDeviceData(devicesData); })); } else { InvocationHelper.InvokeIfRequired(this, new Action(() => { HandleListDetectedDevicesDeviceData(devicesData); })); } } else { InvocationHelper.InvokeIfRequired(this, new Action(() => { AddLineToStatus("Data not long enough"); })); } break; default: InvocationHelper.InvokeIfRequired(this, new Action(() => { AddLineToStatus(String.Format("Error response: {0}", cmd.Status)); })); break; } }
private void SendNextMultiAdd() { MGBTCommandData data = new MGBTCommandData(); data.Status = 0x0000; data.CommandType = 0x0001; data.data = GetCommandData(multiAddresList[multiIndex]); multiIndex++; if (multiIndex >= multiAddresList.Length) { addingMultiple = false; } SendCommand(data); }
private void SendCommand(MGBTCommandData data) { if (proto == null) { return; } if (proto.ReadyToSend()) { proto.SendCommand(data); } else { AddLineToStatus("Proto not ready to send, try again later"); } }
private void HandleGetID(MGBTCommandData cmd) { try { var reader = new BinaryReader(new MemoryStream(cmd.data)); byte deviceType = reader.ReadByte(); byte[] idDate = reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position)); InvocationHelper.InvokeIfRequired(this, new Action(() => { AddLineToStatus($"Got type: {deviceType} with data: {BitConverter.ToString(idDate)}"); })); } catch (Exception ex) { InvocationHelper.InvokeIfRequired(this, new Action(() => { AddLineToStatus($"Exception: {ex}"); })); } }
public static MGBTCommandData FromArray(byte[] bytes) { try { var reader = new BinaryReader(new MemoryStream(bytes)); var s = new MGBTCommandData(); s.dataLength = reader.ReadUInt16(); s.CRC = reader.ReadUInt16(); s.Status = reader.ReadUInt16(); s.CommandType = reader.ReadUInt16(); s.data = reader.ReadBytes(s.dataLength); return(s); } catch (Exception e) { return(null); } }
private void HandleGetLaps(MGBTCommandData cmd) { try { var reader = new BinaryReader(new MemoryStream(cmd.data)); int byteIndex = 0; int time = 0; int lapIndex = 0; StringBuilder lapLines = new StringBuilder(); while (reader.BaseStream.Position < reader.BaseStream.Length) { byte aByte = reader.ReadByte(); time |= aByte << (8 * byteIndex); byteIndex++; if (byteIndex >= 3) { byteIndex = 0; lapLines.AppendLine($"Got lap {lapIndex} with time {time}"); lapIndex++; } } InvocationHelper.InvokeIfRequired(this, new Action(() => { AddLineToStatus(lapLines.ToString()); })); } catch (Exception ex) { InvocationHelper.InvokeIfRequired(this, new Action(() => { AddLineToStatus($"Exception: {ex}"); })); } }
private void SetTimeBtn_Click(object sender, EventArgs e) { int millis; if (Int32.TryParse(macTbx.Text, out millis)) { var stream = new MemoryStream(); var writer = new BinaryWriter(stream); if (millis > 599999) { millis = 599999; } writer.Write(millis); MGBTCommandData data = new MGBTCommandData(); data.Status = 0x0000; data.CommandType = 104; data.data = stream.ToArray(); SendCommand(data); } else { AddLineToStatus($"{macTbx.Text} is not an int"); } }
private void HandleListAllowedDevices(MGBTCommandData cmd) { if ((cmd.data.Length > 2) && (cmd.Status == 1)) { InvocationHelper.InvokeIfRequired(this, new Action(() => { if (cmd.data[0] == 0) { AllowedDevicesLbx.Items.Clear(); } byte[] devicesData = new byte[cmd.data.Length - 2]; Array.Copy(cmd.data, 2, devicesData, 0, devicesData.Length); List <MGBTDevice> devices = MGBTDevice.FromArray(devicesData); AllowedDevicesLbx.Items.AddRange(devices.ToArray()); })); } else { InvocationHelper.InvokeIfRequired(this, new Action(() => { AddLineToStatus(String.Format("Data length or error response: {0}", cmd.Status)); })); } }
private void run() { byte[] dataBuffer = new byte[256]; int bufferPosition = 0; timeoutTimer = new System.Timers.Timer(); timeoutTimer.Interval = 200; timeoutTimer.Elapsed += TimeoutTimer_Elapsed; try { serialPort.Open(); ConnectionStateChanged?.Invoke(this, null); while (serialPort.IsOpen) { int bytesToRead = serialPort.BytesToRead; if (state == State.Idle) { if (bytesToRead > 0) { timeoutTimer.Start(); state = State.Receiving; } else if (commandToSend != null) { state = State.Sending; } } switch (state) { case State.Receiving: { if (bytesToRead > 0) { if ((bytesToRead + bufferPosition) < dataBuffer.Length) { serialPort.Read(dataBuffer, bufferPosition, bytesToRead); bufferPosition += bytesToRead; } else { serialPort.Read(dataBuffer, bufferPosition, (dataBuffer.Length - bufferPosition)); bufferPosition += (dataBuffer.Length - bufferPosition); } while (CheckDataBuffer(dataBuffer, bufferPosition)) { MGBTCommandData data = MGBTCommandData.FromArray(dataBuffer); if (data.VerifyCRC()) { rxQueue.Enqueue(data); NewDataArrived?.Invoke(this, null); } else { } if (bufferPosition > (data.dataLength + 8)) { byte[] newDataBuffer = new byte[dataBuffer.Length]; Array.Copy(dataBuffer, (data.dataLength + 8), newDataBuffer, 0, dataBuffer.Length - (data.dataLength + 8)); dataBuffer = newDataBuffer; bufferPosition -= (data.dataLength + 8); } else { Array.Clear(dataBuffer, 0, dataBuffer.Length); bufferPosition = 0; state = State.Idle; } } } break; } case State.Sending: { byte[] data = null; lock (lockObj) { commandToSend.UpdateCRC(); data = commandToSend.ToArray(false); commandToSend = null; state = State.Idle; } if (data != null) { serialPort.Write(data, 0, data.Length); } break; } default: { break; } } Thread.Sleep(10); } } catch (Exception e) { if (serialPort.IsOpen) { serialPort.Close(); } } ConnectionStateChanged?.Invoke(this, null); }