-
-
Save MKo-xx/754798 to your computer and use it in GitHub Desktop.
connect to serial port and send/receive data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void connect_button_Click(object sender, EventArgs e) | |
{ | |
... | |
string port_name = portlist_comboBox.SelectedItem.ToString(); // get serial port name | |
serial_port_ = new SerialPort(port_name, 9600); // set port name and speed 9600 bits/second | |
serial_port_.Open(); // connect to port | |
thread_ = new Thread(read_serial_port); // create thread for reading received data | |
thread_.Start(); | |
... | |
} | |
... | |
public void read_serial_port() | |
{ | |
while (serial_port_.IsOpen) | |
{ | |
try | |
{ | |
string str = serial_port_.ReadLine(); // | |
add_output(str); // add to output text box. | |
} | |
catch (TimeoutException) { } | |
} | |
} | |
... | |
private void send_button_Click(object sender, EventArgs e) | |
{ | |
serial_port_.Write(send_textBox.Text); | |
send_textBox.Clear(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment