Port öffnen/schließen etc.
Code:
private void Form1_Load(object sender, EventArgs e) {
string[] ports = SerialPort.GetPortNames();
comboBox1.Items.AddRange(ports);
if (ports.Length > 0)
{
comboBox1.SelectedIndex = 0;
btnOpen.Enabled = true;
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
if (btnOpen.Text == "Open")
{
serialPort1.PortName = comboBox1.Text;
serialPort1.Open();
btnOpen.Text = "Close";
comboBox1.Enabled = false;
groupBox1.Enabled = true;
timer1.Enabled = true;
}
else
{
serialPort1.Close();
btnOpen.Text = "Open";
comboBox1.Enabled = true;
groupBox1.Enabled = false;
timer1.Enabled = false;
}
}
Daten rausschreiben
Code:
serialPort1.Write(dataSend.ToArray(), 0, dataSend.Count);
Daten empfangen
Code:
// check for new data
while (serialPort1.BytesToRead > 0)
{
byte rxByte = (byte)serialPort1.ReadByte();
protocol_ParseByte(rxByte);
}
Sind nur Bruchstücke aus meinem Code, sollte aber alles nötige drin sein.
mfg
Lesezeichen