private void button1_Click(object sender, EventArgs e) { openPort(); if (textBox1.Text.Length <= 0) { textBox1.Text = "0"; } int fromTxtBox = Convert.ToInt32(textBox1.Text); byte percentValue = CMTools.getByteFromPercent(fromTxtBox); byte[] sendValue = new byte[1]; if (chkLftDsp.Checked) { sendValue[0] = percentValue; cm.SendBytes(sendValue); } if (chkRhtDsp.Checked) { sendValue[0] = (byte)(64 | percentValue); // Set the Disp ID # to 1 for the right display cm.SendBytes(sendValue); } //MessageBox.Show(sendValue.ToString()); }
public void SendBytes(byte[] bytes) { if (!(comPort.IsOpen == true)) { OpenPort(); } if (comPort.IsOpen && (bytes.Length > 0)) { bool threadLocked = commLock.WaitOne(100); if (threadLocked) { comPort.Write(bytes, 0, bytes.Length); commLock.Release(); } else { throw new Exception("Failed to lock SendBytes"); } //DisplayData("Sent: " + CMTools.BytesToHex(bytes) + "\n"); } else { DisplayData("Error: Failed to send bytes - [" + CMTools.BytesToHex(bytes) + "]\n"); } //comPort.Close(); }
private void loadPorts() { String[] portList = CMTools.GetCommPortList(); cmbPortList.Items.Clear(); cmbPortList.Items.AddRange(portList); if (cmbPortList.Items.Count > 0) { cmbPortList.SelectedIndex = 0; } }
private void tmrRhtGauge_Tick(object sender, EventArgs e) { if (!cm.isOpen()) { openPort(); } byte[] sendByte = new byte[1]; sendByte[0] = (byte)(AGConstants.DISP_ID_1 | CMTools.getByteFromPercent(rhtStatWrapper.getValue())); cm.SendBytes(sendByte); }
/// <summary> /// method that will be called when there is data waiting in the buffer. /// /// THIS WILL BE CALLED ASYNCHRONOSLY. WATCH FOR RACES. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { int bytes = comPort.BytesToRead; byte[] comBuffer = new byte[bytes]; comPort.Read(comBuffer, 0, bytes); DisplayData("Received: " + CMTools.BytesToHex(comBuffer) + "\n"); //handleData(comBuffer); //DisplayDataQueue(); }
private void btnSend_Click(object sender, EventArgs e) { if (cm.isOpen()) { int i; cm.SendBytes(CMTools.GetBytes(txtMsg.Text, out i)); rtbOutput.AppendText("Sent: " + txtMsg.Text + "\n"); } else { rtbOutput.AppendText("Port is not open.\n"); } }