裡面是讀取Rs232,如果要傳送read改成write就行了。
宣告SerialPort 要記得給它BaudRate 和PortName
接下來就可以Open SerialPort收發訊息,
再加個Thread就可以做各式應用了。
public void SerialPortRead()
{
SerialPort SerialPort1 = new SerialPort();
SerialPort1.BaudRate = int.Parse(comboBox2.SelectedItem.ToString());
SerialPort1.PortName = comboBox1.SelectedItem.ToString();
try
{
SerialPort1.Open();
SerialPort1.WriteLine("m");
int bytes = SerialPort1.BytesToRead;
byte[] buffer = new byte[bytes];
char[] test = new char[bytes];
SerialPort1.Read(test, 0, bytes);
MessageBox.Show("Connect Successful!");
workerThread = new Thread(RunSample01);
workerThread.Start();
while (!workerThread.IsAlive) ;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
沒有留言:
張貼留言