我尝试了我的串行 USB 设备。不断改变波特率并检查。ComboBox 包含一系列可能的波特率。
public void initConfig(SerialPort serialPort)
{
// you can assign these values in a combo box
string[] ports= "{COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8"};
//you can assign these values in a combo box in a string format
int[] baudRate = { 4800, 9600, 19200, 38400, 57600, 115200, 230400 };
serialPort.PortName = ports[0]; //else get from combobox : portCombobox.SelectedItem
serialPort.BaudRate = baudRate[0];
//serialPort.BaudRate = Int32.Parse(speedComboBox.SelectedItem.ToString());
//you can have controls to store and change these values if required
serialPort.Handshake = System.IO.Ports.Handshake.None;
serialPort.Parity = System.IO.Ports.Parity.None;
serialPort.DataBits = 8;
serialPort.StopBits = System.IO.Ports.StopBits.One;
serialPort.ReadTimeout = 200;
serialPort.WriteTimeout = 50;
}
将字符串更改为相应的类型并调用 open。
最后:
public void callingMethod() //or your connect event attached control
{
SerialPort serialPort = new SerialPort();
initConfig(serialPort);
try
{
serialPort.Open();
}
catch
{
MessageBox.Show("Error: Unable to Open the serial interface !");
return;
}
}