C#:带校验的串口开关控制
1. 效果图
2. 单片机端代码
/*********************************************************************************************/ #include//标准51头文件 /********************************************************************************************* 函数名:UART串口初始化函数 调 用:UART_init(); 参 数:无 返回值:无 结 果:启动UART串口接收中断,允许串口接收,启动T/C1产生波特率(占用) 备 注:振荡晶体为12MHz,PC串口端设置 [ 4800,8,无,1,无 ] /**********************************************************************************************/ void UART_init (void){ EA = 1; //允许总中断(如不使用中断,可用//屏蔽) ES = 1; //允许UART串口的中断 TMOD = 0x20; //定时器T/C1工作方式2 SCON = 0x50; //串口工作方式1,允许串口接收(SCON = 0x40 时禁止串口接收) TH1 = 0xF3; //定时器初值高8位设置 TL1 = 0xF3; //定时器初值低8位设置 PCON = 0x80; //波特率倍频(屏蔽本句波特率为2400) TR1 = 1; //定时器启动 } /**********************************************************************************************/ /********************************************************************************************* 函数名:UART串口接收中断处理函数 调 用:[SBUF收到数据后中断处理] 参 数:无 返回值:无 结 果:UART串口接收到数据时产生中断,用户对数据进行处理(并发送回去) 备 注:过长的处理程序会影响后面数据的接收 /**********************************************************************************************/ void UART_R (void) interrupt 4 using 1{ //切换寄存器组到1 unsigned char UART_data; //定义串口接收数据变量 RI = 0; //令接收中断标志位为0(软件清零) UART_data = SBUF; //将接收到的数据送入变量 UART_data SBUF = ~UART_data; //将接收的数据发送回去 while(TI == 0); //检查发送中断标志位 TI = 0; //令发送中断标志位为0(软件清零) } /**********************************************************************************************/ void main() { UART_init(); //初始化 while(1); //主循环 } /**********************************************************************************************/
3. C#代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO.Ports; namespace Reply { 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(SerialPortDataReceived); //添加串口中断事件 } private void SetOvlShape(int which) //填充颜色 { switch(which) { case 1: ovalShape1.FillColor = Color.Green; ovalShape2.FillColor = Color.Red; ovalShape3.FillColor = Color.Red; break; case 2: ovalShape1.FillColor = Color.Red; ovalShape2.FillColor = Color.Green; ovalShape3.FillColor = Color.Red; break; case 3: ovalShape1.FillColor = Color.Red; ovalShape2.FillColor = Color.Red; ovalShape3.FillColor = Color.Green; break; case 4: ovalShape1.FillColor = Color.Green; ovalShape2.FillColor = Color.Green; ovalShape3.FillColor = Color.Green; break; default: break; } } private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e) { byte DataReceived = (byte)(~serialPort1.ReadByte()); //单字节读取 try { timer1.Stop(); //关定时器 } catch { } if (DataSended == 0) //防止下位机乱发,不处理 return; SetOvlShape(DataReceived); try { if (DataToSend[DataSended - 1] == DataReceived) //校验数据 { MessageBox.Show("数据校验成功", "成功!"); //弹出提示 } else { MessageBox.Show("数据校验失败", "数据校验失败"); } } catch { } } 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 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 Button_Click(object sender, EventArgs e) //三个按键共用一个处理函数 { Button MyButton = (Button)sender; //通过tag属性来区分 DataSended = Convert.ToByte(MyButton.Tag); SendDataToSerialPort(serialPort1, DataToSend[DataSended - 1]); //发数据 } private void timer1_Tick(object sender, EventArgs e) //定时器事件 { string MyStr = DataSended.ToString() + "路数据校验超时,请检查"; //Messagebox内容 timer1.Stop(); MessageBox.Show(MyStr, "错误"); } } }
//TODO:P13