960019200384005760074880115200
using System.IO.Ports;
baudrate_cbb.SelectedIndex = 0; string[] ports = SerialPort.GetPortNames(); port_cbb.Items.AddRange(ports); port_cbb.SelectedIndex = 0;
port_cbb.Items.Clear(); string[] ports = SerialPort.GetPortNames(); port_cbb.Items.AddRange(ports); port_cbb.SelectedIndex = 0;
con_btn.Enabled = false; dis_btn.Enabled = true; trackBar1.Enabled = true; if (serialPort1.IsOpen) serialPort1.Close(); serialPort1.PortName = port_cbb.Text; serialPort1.BaudRate = Convert.ToInt32(baudrate_cbb.Text); try { serialPort1.Open(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); }
con_btn.Enabled = true; dis_btn.Enabled = false; trackBar1.Enabled = false; if (serialPort1.IsOpen) serialPort1.Close();
private string DispString; //used to store the values read
DispString = serialPort1.ReadExisting(); this.Invoke(new EventHandler(DisplayText));
private void DisplayText(object sender, EventArgs e) { textBox1.AppendText(DispString); }
string x = trackBar1.Value.ToString(); degree_lb.Text = x; serialPort1.WriteLine(x);
if (serialPort1.IsOpen) serialPort1.Close(); Application.Exit();
#include <Servo.h>Servo myservo; // create servo object to control a servoString inputString = ""; // a String to hold incoming databool stringComplete = false; // whether the string is completevoid setup() { Serial.begin(9600); // initialize serial: myservo.attach(10); // attaches the servo on pin 10 to the servo object inputString.reserve(200); // reserve 200 bytes for the inputString:}void loop() { int x = inputString.length() - 1; int degree = inputString.substring(0, x).toInt(); //convert to int and delete '\n' if (stringComplete) { Serial.println(degree); myservo.write(degree); inputString = ""; // clear the string: stringComplete = false; }}void serialEvent() { while (Serial.available()) { char inChar = (char)Serial.read(); inputString += inChar; if (inChar == '\n') { stringComplete = true; } }}