1. 串口控制
using System ;
using System. IO. Ports ;
using System. Windows. Forms ; namespace 串口控制
{ public partial class Form1 : Form { const byte DeviceOpen1 = 0x01 ; const byte DeviceClose1 = 0x81 ; const byte DeviceOpen2 = 0x02 ; const byte DeviceClose2 = 0x82 ; const byte DeviceOpen3 = 0x03 ; const byte DeviceClose3 = 0x83 ; byte [ ] SerialPortDataBuffer = new byte [ 1 ] ; public Form1 ( ) { InitializeComponent ( ) ; } private void button1_Click ( object sender, EventArgs e) { SearchAndAddSerialToComboBox ( serialPort1, comboBox1) ; } private void Form1_Load ( object sender, EventArgs e) { SearchAndAddSerialToComboBox ( serialPort1, comboBox1) ; } private void SearchAndAddSerialToComboBox ( SerialPort MyPort, ComboBox MyBox) { string Buffer; MyBox. Items. Clear ( ) ; for ( int i = 1 ; i < 20 ; i++ ) { try { Buffer = "COM" + i. ToString ( ) ; MyPort. PortName = Buffer; MyPort. Open ( ) ; MyBox. Items. Add ( Buffer) ; MyPort. Close ( ) ; } catch { } } MyBox. SelectedIndex = 0 ; } private void button2_Click ( object sender, EventArgs e) { if ( serialPort1. IsOpen) { try { serialPort1. Close ( ) ; } catch { } button2. Text = "打开串口" ; } else { try { serialPort1. PortName = comboBox1. Text; serialPort1. Open ( ) ; button2. Text = "关闭串口" ; } catch { MessageBox. Show ( "串口打开失败" , "错误" ) ; } } } private void WriteByteToSerialPort ( byte data) { byte [ ] Buffer = new byte [ 2 ] { 0x00 , data } ; if ( serialPort1. IsOpen) { try { serialPort1. Write ( Buffer, 0 , 2 ) ; } catch { MessageBox. Show ( "串口数据发送出错,请检查" , "错误" ) ; } } else { MessageBox. Show ( "串口未打开,请打开串口" , "错误" ) ; } } private void button3_Click ( object sender, EventArgs e) { int i = 0 ; try { i = Convert. ToInt32 ( textBox1. Text. Substring ( 0 , textBox1. Text. Length) ) ; } catch { MessageBox. Show ( "请输入正确的数字" ) ; return ; } if ( serialPort1. IsOpen) { if ( i == 0 ) { return ; } else { timer1. Interval = i * 1000 ; timer1. Start ( ) ; button3. Enabled = false ; } } WriteByteToSerialPort ( DeviceOpen1) ; } private void button4_Click ( object sender, EventArgs e) { try { timer1. Stop ( ) ; } catch { } button3. Enabled = true ; WriteByteToSerialPort ( DeviceClose1) ; } private void button5_Click ( object sender, EventArgs e) { WriteByteToSerialPort ( DeviceOpen2) ; } private void button6_Click ( object sender, EventArgs e) { WriteByteToSerialPort ( DeviceClose2) ; } private void button7_Click ( object sender, EventArgs e) { WriteByteToSerialPort ( DeviceOpen3) ; } private void button8_Click ( object sender, EventArgs e) { WriteByteToSerialPort ( DeviceClose3) ; } private void timer1_Tick ( object sender, EventArgs e) { button3. Enabled = true ; timer1. Stop ( ) ; WriteByteToSerialPort ( DeviceClose1) ; } }
}
2. 带校验的串口开关控制
using System ;
using System. Drawing ;
using System. IO. Ports ;
using System. Windows. Forms ; namespace ReplyAndControl
{ public partial class Form1 : Form { byte DataSended = 0 ; byte [ ] DataToSend = new byte [ ] { 0x01 , 0x02 , 0x03 } ; public Form1 ( ) { InitializeComponent ( ) ; System. Windows. Forms. Control. CheckForIllegalCrossThreadCalls = false ; serialPort1. DataReceived += new SerialDataReceivedEventHandler ( SerialPortDataReveived) ; } private void SerialPortDataReveived ( object sender, SerialDataReceivedEventArgs e) { byte DataReceived = ( byte ) ( serialPort1. ReadByte ( ) ) ; try { timer1. Stop ( ) ; } catch { } SetButtonColor ( DataReceived) ; } private void SetButtonColor ( int which) { switch ( which) { case 0x01 : button2. BackColor = Color. Green; break ; case 0x11 : button2. BackColor = Color. Red; break ; case 0x02 : button3. BackColor = Color. Green; break ; case 0x12 : button3. BackColor = Color. Red; break ; case 0x03 : button4. BackColor = Color. Green; break ; case 0x13 : button4. BackColor = Color. Red; break ; case 0x04 : button2. BackColor = Color. Green; button3. BackColor = Color. Green; button4. BackColor= Color. Green; break ; case 0x14 : button2. BackColor = Color. Red; button3. BackColor = Color. Red; button4. BackColor = Color. Red; break ; default : break ; } } private void Button_Click ( object sender, EventArgs e) { Button MyButton = ( Button) sender; DataSended = Convert. ToByte ( MyButton. Tag) ; SendDataToSerialPort ( serialPort1, DataToSend[ DataSended - 1 ] ) ; } private void SendDataToSerialPort ( SerialPort MyPort, byte DataToSend) { byte [ ] DatasToWrite = new byte [ ] { DataToSend } ; if ( serialPort1. IsOpen) { try { MyPort. Write ( DatasToWrite, 0 , 1 ) ; timer1. Interval = 3 * 1000 ; timer1. Start ( ) ; } catch { MessageBox. Show ( "串口数据写入错误" , "错误" ) ; } } } private void timer1_Tick ( object sender, EventArgs e) { string MyStr = DataSended. ToString ( ) + "路数据校验超时,请检查" ; timer1. Stop ( ) ; MessageBox. Show ( MyStr, "错误" ) ; } private void button1_Click ( object sender, EventArgs e) { if ( serialPort1. IsOpen) { try { serialPort1. Close ( ) ; } catch { } button1. Text = "打开串口" ; } else { try { serialPort1. PortName = comboBox1. Text; serialPort1. Open ( ) ; } catch { MessageBox. Show ( "串口打开错误,请检查" , "错误" ) ; } button1. Text = "关闭窗口" ; } } private void Form1_Load ( object sender, EventArgs e) { string Buffer; comboBox1. Items. Clear ( ) ; for ( int i = 1 ; i < 20 ; i++ ) { try { Buffer = "COM" + i. ToString ( ) ; serialPort1. PortName = Buffer; serialPort1. Open ( ) ; comboBox1. Items. Add ( Buffer) ; serialPort1. Close ( ) ; } catch { } } comboBox1. SelectedIndex = 0 ; } }
}