private void ConnectSerial_Click(object sender, EventArgs e) { if (USBport.IsOpen == false) //buttonConnectSerial.Text == "Connect (USB Port)" { // Set COM Port string str = comboBox1.GetItemText(comboBox1.SelectedItem); if (comboBox1.SelectedItem != null) { USBport.PortName = str; } textBox1.Text = str; try { USBport.Open(); buttonConnectSerial.Text = "Disconnect (USB Port)"; } catch (Exception err) { MessageBox.Show(err.Message); //throw; } } else if (USBport.IsOpen == true) //(buttonConnectSerial.Text == "Disconnect (USB Port)" { USBport.Close(); buttonConnectSerial.Text = "Connect (USB Port)"; } }
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { // richTextBox2.Text = ""; while (USBport.BytesToRead > 0) { int c = USBport.ReadByte(); // richTextBox2.AppendText(c.ToString()); } }
private void JsendCommand() { int RightStickY = state.Rz; //RYY int RightStickX = state.Z; int LeftStickX = state.X; int LeftStickY = state.Y; //LYY byte val = 0; if (LeftStickY <= 32768) { val = Convert.ToByte(60 + LeftStickY * (185 - 60) / (32767)); } else if (LeftStickY > 32768) { val = Convert.ToByte(100 + LeftStickY * (65) / (32768)); } byte val2 = 0; if (RightStickY <= 32768) { val2 = Convert.ToByte(250 + RightStickY * (175 - 250) / (32767)); } else if (RightStickY > 32768) { val2 = Convert.ToByte(280 + RightStickY * (60 - 175) / (32767)); } // Provide appropriate Data to send (via USB) byte[] b = { val, val2, 10 }; USBport.Write(b, 0, 3); /* * for (int j = 0; j < 4; j++) * { * USBport.Write(b, j, 1); * //USBport.WriteLine(""); * }*/ richTextBox3.Text = "\n" + "\t" + b[0] + "\t" + b[1]; }
// THIS METHOD IS FOR TESTING PURPOSE /* * private void JsendCommandTest() * { * int axisA = state.Rz; * int axisB = state.Z; * int axisC = state.X; * int axisD = state.Y; * * byte val = 0; * //byte g = 255; * if (axisD <= 32768) * { * //val = Convert.ToByte(128 - (axisD / 256)); * val = Convert.ToByte(250 + axisD * (175 - 250) / (32767)); * } * else if (axisD > 32768) * { * val = Convert.ToByte( 280 + axisD * ( 60- 175) / (32767)); * } * textBox1.Text = "" + Convert.ToString(val); * }*/ private void Form1_FormClosing(object sender, FormClosingEventArgs e) { USBport.Close(); }