private void btnReadStates_Click(object sender, EventArgs e) { byte[] bytes; if (PortDataComm.SendCommand(0x0D, out bytes) != PortDataComm.CommandStatus.OK) { MessageBox.Show(this, "Error", "Unable to read statuses", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } textBox1.Lines = new[] { $"Host->gState:{bytes[0]:X2}", $"Host->EnumState:{bytes[1]:X2}", $"Host->RequestState:{bytes[2]:X2}", $"Host->Control.state:{bytes[3]:X2}", $"Host->device.speed:{bytes[4]:X2}", $"HID->State:{bytes[5]:X2}", $"HID->ContrlolState:{bytes[6]:X2}", $"ReportLength:{bytes[7]:0}", $"MiltiRerport:{bytes[8]:0}", $"DataFlow:{bytes[9]:0}", }; }
private void ReadVariables(IEnumerable <EEPROMMapVariablePanel> panels, Action onComplete) { bool errorSet = false; string errorMessage = string.Empty; foreach (var currentReadPanel in panels) { try { var b1 = (byte)((currentReadPanel.Addr >> 8) & 0xFF); var b2 = (byte)(currentReadPanel.Addr & 0xFF); byte[] data; if (PortDataComm.SendCommand(0x6, out data, b1, b2) != PortDataComm.CommandStatus.OK || data == null) { errorMessage = CommonResources.ComunicationError; errorSet = true; break; } if (data.Length != 4) { errorMessage = CommonResources.WrongPacketStructure; errorSet = true; break; } var w1 = (ushort)((data[0] << 8) | data[1]); if (w1 != 0) { errorSet = true; errorMessage = Resources.EEPMapDialog_ReadFault; break; } var w2 = (short)((data[2] << 8) | data[3]); currentReadPanel.Value = w2; Invoke(new Action(() => { sbProgress.Value++; })); } catch (Exception exx) { errorSet = true; errorMessage = exx.Message; break; } } Invoke(onComplete); if (errorSet) { Invoke(new Action(() => MessageBox.Show(this, errorMessage, CommonResources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error))); } }
private bool DoSendFinishedCommand() { byte[] ret; if (PortDataComm.SendCommand(0x69, out ret) != PortDataComm.CommandStatus.OK || ret == null || ret.Length != 1 || ret[0] != 1) { SetStatus(-9, 0, 1); return(false); } return(true); }
private void RunReaderOnTick() { while (true) { try { Thread.Sleep(250); if (!PortDataComm.IsOpen) { Invoke(new Action(() => { connectToolStripMenuItem.Enabled = true; disconnectToolStripMenuItem.Enabled = false; toolStripConnection.Text = "Not connected"; })); break; } if (ReaderEnabled) { byte[] data; if (PortDataComm.SendCommand(2, out data) == PortDataComm.CommandStatus.OK) { PortOnOnDataRecieved(2, PortDataComm.CommandStatus.OK, data); } if (PortDataComm.SendCommand(5, out data) == PortDataComm.CommandStatus.OK) { PortOnOnDataRecieved(5, PortDataComm.CommandStatus.OK, data); } if (PortDataComm.SendCommand(8, out data) == PortDataComm.CommandStatus.OK) { PortOnOnDataRecieved(8, PortDataComm.CommandStatus.OK, data); } } } catch (ThreadAbortException) { break; } } __Reader = null; }
private void ReadCom() { btnComRead.Enabled = false; new Task(() => { byte[] backdata = null; byte[] vendordata = null; for (var tries = 0; tries < 5; tries++) { if (PortDataComm.SendCommand(1, out backdata, null) == PortDataComm.CommandStatus.OK && backdata != null && backdata.Length > 0) { break; } } for (var tries = 0; tries < 5; tries++) { if (PortDataComm.SendCommand(0xA, out vendordata, null) == PortDataComm.CommandStatus.OK && vendordata != null && vendordata.Length == 4) { break; } } Invoke(new ThreadSafeInvokeDelegate(() => { if (backdata == null || backdata.Length == 0 || vendordata == null || vendordata.Length != 4) { lblPortState.Text = Resources.ParseJoyDialog_ReadCom_Communication_error; lblPortState.ForeColor = Color.Red; } else { lblPortState.Text = Resources.ParseJoyDialog_ReadCom_Complete; lblPortState.ForeColor = Color.Red; tbValues.Text = string.Join(" ", backdata.Select(b => b.ToString("X2"))); tbVendor.Text = string.Join("", vendordata.Take(2).Select(b => b.ToString("X2"))); tbProduct.Text = string.Join("", vendordata.Skip(2).Select(b => b.ToString("X2"))); } btnComRead.Enabled = true; })); }).Start(); }
private void clickPort(object sender, EventArgs eventArgs) { #if MEGA2560 __Port = new SerialPort(((ToolStripMenuItem)sender).Text, 115200); __Port.DataReceived += PortOnDataReceived; __Port.Open(); __ComBuffer = String.Empty; #endif #if STM32 try { if (PortDataComm.IsOpen) { return; } PortDataComm.Open(((ToolStripMenuItem)sender).Text, 115200); byte[] tbuff; var answer = PortDataComm.SendCommand(4, out tbuff, 1, 2, 3, 4); if (answer != PortDataComm.CommandStatus.OK) { ShowError("Unable to connect"); PortDataComm.Close(); } toolStripConnection.Text = "Connected"; } catch { ShowError("Unable to connect"); } #endif __Reader = new Thread(RunReaderOnTick) { IsBackground = true }; __Reader.Start(); connectToolStripMenuItem.Enabled = false; disconnectToolStripMenuItem.Enabled = true; }
private void btnComSend_Click(object sender, EventArgs e) { var line = (string)lbLines.SelectedItem; if (line == null) { return; } var values = line.Split(' ').Select(vvs => byte.Parse(vvs, NumberStyles.HexNumber)).ToArray(); byte[] bytes; if (PortDataComm.SendCommand(0x0E, out bytes, values) != PortDataComm.CommandStatus.OK) { MessageBox.Show("Command execution fails", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void ExecuteCollection() { byte[] bytes; while (!StopCollection) { if (PortDataComm.SendCommand(0x08, out bytes) != PortDataComm.CommandStatus.OK) { Invoke(new Action(ModeNotCollecting)); return; } Invoke(new Action(() => { var line = string.Join(" ", bytes.Select(b => b.ToString("X2"))); textBox1.Lines = textBox1.Lines.Union(new[] { line }).ToArray(); })); Thread.Sleep(200); } Invoke(new Action(ModeNotCollecting)); }
private void WriteVariables(IEnumerable <EEPROMMapVariablePanel> panels, Func <EEPROMMapVariablePanel, short> conv, Action onComplete) { bool errorSet = false; string errorMessage = String.Empty; byte[] data; foreach (var currentReadPanel in panels) { try { var b1 = (byte)((currentReadPanel.Addr >> 8) & 0xFF); var b2 = (byte)(currentReadPanel.Addr & 0xFF); var val = conv(currentReadPanel); var b3 = (byte)((val >> 8) & 0xFF); var b4 = (byte)(val & 0xFF); if (PortDataComm.SendCommand(0x7, out data, b1, b2, b3, b4) != PortDataComm.CommandStatus.OK || data == null) { errorMessage = CommonResources.ComunicationError; errorSet = true; break; } if (data.Length != 2) { errorSet = true; errorMessage = CommonResources.WrongPacketStructure; break; } var w2 = data[1]; if (w2 != 0) { errorSet = true; errorMessage = Resources.EEPMapDialog_WriteFault; break; } Invoke(new Action(() => { sbProgress.Value++; })); } catch (Exception exx) { errorSet = true; errorMessage = exx.Message; break; } } if (!errorSet) { if (PortDataComm.SendCommand(0x9, out data) != PortDataComm.CommandStatus.OK) { errorMessage = Resources.EEPMapDialog_WriteFault; errorSet = true; } } Invoke(onComplete); if (errorSet) { Invoke( new Action( () => MessageBox.Show(this, errorMessage, CommonResources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error))); } }