下面是我用C#语言编写的一个上位机模板(课堂上跟老师学习的)
1、 MainForm : Form下的定义和代码
[DllImport("kernel32")]private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); // 系统dll导入ini写函数[DllImport("kernel32")]private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); // 系统dll导入ini读函数string FileName = System.AppDomain.CurrentDomain.BaseDirectory + "Backup.ini"; // ini 文件名StringBuilder BackupBuf = new StringBuilder(50);//存储读出ini内容变量private bool isRxShow = true;private string strRead;
2、主界面的代码
private void MainForm_Load(object sender, EventArgs e){SerialPortInit();chbCheckCRC.Enabled = false;// 恢复发送栏GetPrivateProfileString("串口1", "发送栏", "", BackupBuf, 50, FileName);txtSendInfo.Text = BackupBuf.ToString();}
public MainForm(){InitializeComponent();serialPort1.Encoding = Encoding.GetEncoding("GB2312"); //支持汉字//System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;}
3、 private void SerialPortInit()的代码
private void SerialPortInit(){RefreshSerialportList(cmbSerialPort);string[] baudRates = { "4800", "9600", "115200" };cmbBaudRate.Items.AddRange(baudRates);cmbBaudRate.SelectedIndex = 1;string[] dataBits = { "5", "6", "7", "8" };cmbDataBits.Items.AddRange(dataBits);cmbDataBits.SelectedIndex = 3;string[] stopBits = { "One", "OnePointFive", "Two" };cmbStopBits.Items.AddRange(stopBits);cmbStopBits.SelectedIndex = 0;string[] parity = { "None", "Even", "Odd", "Mark", "Space" };cmbParity.Items.AddRange(parity);cmbParity.SelectedIndex = 0;}
由于代码太多我就不一一说明了,有什么大家自己开代码就知道了
private void AppendTextToInfoBox(string text, TextBox textBox){textBox.AppendText(text);}
private void RefreshSerialportList(ComboBox comboBox){// 本地设备注册表中获取串口列表: 枚举计算机上可用的串口RegistryKey keyCom = Registry.LocalMachine.OpenSubKey(@"HardwareDeviceMapSerialComm");string[] sSubKeys = keyCom.GetValueNames();string BackupPort;// 恢复串口号GetPrivateProfileString("串口1", "端口号", "", BackupBuf, 50, FileName);BackupPort = BackupBuf.ToString();comboBox.Items.Clear(); // 清空串口列表// 添加端口号foreach (var sValue in sSubKeys){try{string portName = (string)keyCom.GetValue(sValue);comboBox.Items.Add(portName);//检测备份端口号是否有效,端口号初始化为备份端口号if (BackupPort == portName){comboBox.Text = BackupPort;}//如果备份端口号无效,端口号初始化为第一个可用端口号if (comboBox.Text == "")comboBox.Text = portName;}catch{}}// 设置默认显示的串口号if (cmbSerialPort.Items.Count > 0){comboBox.SelectedIndex = 0;}// 没有可用端口号时进行提示else{AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm;ss") + "]" + "-> ", txtRecvInfo);AppendTextToInfoBox("没检测到可用串口!rn", txtRecvInfo);}//备份端口号 WritePrivateProfileString("串口1", "端口号", comboBox.Text, FileName);}
扫描串口按键的代码
private void btnRefreshSerialPor_Click(object sender, EventArgs e){// 先关闭串口if (serialPort1.IsOpen == true){serialPort1.Close();btnOpenOrClose.Text = "打开串口";AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "->", txtRecvInfo);AppendTextToInfoBox("扫描和添加串口时,关闭串口。rn", txtRecvInfo);}// 再扫描添加串口RefreshSerialportList(cmbSerialPort);}
打开串口按键的代码
private void btnOpenOrClose_Click(object sender, EventArgs e){if (serialPort1 != null && serialPort1.IsOpen){// 关闭串口serialPort1.Close();btnOpenOrClose.Text = "打开串口";AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm;ss") + "]" + "-> ", txtRecvInfo);AppendTextToInfoBox("手动关闭串口。rn", txtRecvInfo);timer1.Stop(); // 关闭串口工具异常检测}else{// 打开串口try{serialPort1.PortName = cmbSerialPort.Text; // 端口号serialPort1.Open();btnOpenOrClose.Text = "关闭串口";AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "->", txtRecvInfo);AppendTextToInfoBox("串口打开成功。rn", txtRecvInfo);timer1.Start(); // 启用串口工具异常检测}catch (Exception ex){// 串口异常AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "-> ", txtRecvInfo);AppendTextToInfoBox("串口打开失败。rn", txtRecvInfo);AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "-> ", txtRecvInfo);AppendTextToInfoBox(ex.Message + "rn", txtRecvInfo);timer1.Stop(); // 关闭串口工具异常检测}}}
下拉框切换串口的代码
private void cmbSerialPort_SelectedIndexChanged(object sender, EventArgs e){if (serialPort1 != null && serialPort1.IsOpen){// 如果串口已经打开了 则需要先关闭串口serialPort1.Close();btnOpenOrClose.Text = "打开串口";// 获取要打开的串口号//serialPort1 = new Serialport(cmbSerialport.SelectedItem.ToString());// 打开串口try{serialPort1.PortName = cmbSerialPort.Text; // 端口号serialPort1.Open();btnOpenOrClose.Text = "关闭串口";AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "-> ", txtRecvInfo);AppendTextToInfoBox("串口变更成功。rn", txtRecvInfo);timer1.Start();// 启用串口工具异常检测}catch (Exception ex){serialPort1.Close();AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "" + "-> ", txtRecvInfo);AppendTextToInfoBox("串口变更失败。rn", txtRecvInfo);AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "->", txtRecvInfo);AppendTextToInfoBox(ex.Message + " rn", txtRecvInfo);timer1.Stop(); // 关闭串口工具异常检测}//备份端口号 WritePrivateProfileString("串口1", "端口号", cmbSerialPort.Text, FileName);}}
下拉框更换波特率的代码
private void cmbBaudRate_SelectedIndexChanged(object sender, EventArgs e){serialPort1.BaudRate = Convert.ToInt32(cmbBaudRate.Text, 10); //设置波特率//AppendTextToInfoBox("["+ DateTime.Now.ToString("HH:mm:ss") + "]"+ "->", txtRecvInfo);//AppendTextToInfoBox("波特率变更成功。 rn",txtRecvInfo);}
timer1的代码展示
private void timer1_Tick(object sender, EventArgs e){if (!serialPort1.IsOpen){serialPort1.Close();btnOpenOrClose.Text = "打开串口";// 程序扫描并添加串口RefreshSerialportList(cmbSerialPort);}}
// 字符串转换为 16 进制
public static string ToHexString(byte[] buffer, int startIndex, string format){string hex = BitConverter.ToString(buffer, startIndex).Replace("-", format);return hex;}
清空接收按键的代码
private void btnClearRecvData_Click(object sender, EventArgs e){txtRecvInfo.Text = "";}
CRC校验
private UInt16 CRC_Check(byte[] Data, byte DataLen){UInt16 CRC = 0xFFFF;for (byte i = 0; i < DataLen; i++){CRC ^= Data[i];for (byte j = 0; j < 8; j++){if ((CRC & 0x0001) == 0x0001){CRC = (UInt16)((CRC >> 1) ^ 0xA001);}else{CRC = (UInt16)(CRC >> 1);}}}CRC = (UInt16)((CRC >> 8) + (CRC << 8));return CRC;}
手动发送按键代码
private void btnSendData_Click(object sender, EventArgs e){byte[] data = new byte[1];// 以 ASCII 格式发送if (!chbHexSend.Checked){try{//支持中文Encoding Chinese = System.Text.Encoding.GetEncoding("GB2312");byte[] Sendbytes = Chinese.GetBytes(txtSendInfo.Text);foreach (byte Member in txtSendInfo.Text){data[0] = Member;serialPort1.Write(data, 0, 1); // 一个字节一个字节地发送}// 发送自动换行if (chbNewLine.Checked == true){data[0] = 0xeD;serialPort1.Write(data, 0, 1); // 发送回车data[0] = 0x0A;serialPort1.Write(data, 0, 1); // 发送换行}// 统计发送字节数UInt32 sendBytes = Convert.ToUInt32(tsslSendByte.Text, 10); // 定义发送字节数变量,并初始化为已发送字节数sendBytes += (UInt32)Sendbytes.Length; // 加ASCII码字节数if (chbNewLine.Checked == true) // 加回车换行2个字节sendBytes += 2;tsslSendByte.Text = Convert.ToString(sendBytes, 10); // 显示总发送字节数}catch (Exception ex){// 在信息框中追加异常信息AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "<-", txtRecvInfo);AppendTextToInfoBox(ex.Message + "串口数据发送错误! rn", txtRecvInfo);serialPort1.Close();btnOpenOrClose.Text = "打开串口";}}//以HEX 格式发送else{// 发送前先要处理字符串数据string strBuf = txtSendInfo.Text;strBuf = strBuf.Replace("0x", string.Empty); // 除前缀 0xstrBuf = strBuf.Replace("0x", string.Empty); // 除前缀 0xstrBuf = strBuf.Replace(" ", string.Empty); // 消除空格byte[] Calculate_CRC = new byte[(strBuf.Length - strBuf.Length % 2) / 2];txtSendInfo.Text = "";for (int i = 0; i < (strBuf.Length - strBuf.Length % 2) / 2; i++) // 取余运算作用是防止用户输入的字符为奇数个{txtSendInfo.AppendText(strBuf.Substring(i * 2, 2) + "");try{data[0] = Convert.ToByte(strBuf.Substring(i * 2, 2), 16);serialPort1.Write(data, 0, 1); // 一个字节一个字节地发送Calculate_CRC[i] = data[0];// 统计发送字节数UInt32 sendBytes = Convert.ToUInt32(tsslSendByte.Text, 10); // 定义发送字节数变量,并初始化为已发送字节数sendBytes += (UInt32)Calculate_CRC.Length; // 加HEX字节数if (chbCheckCRC.Checked == true) // 加CRC校验2个字节sendBytes += 2;tsslSendByte.Text = Convert.ToString(sendBytes, 10); // 显示总发送字节数}catch (Exception ex){// 在信息框中追加异常信息AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "<-", txtRecvInfo);AppendTextToInfoBox(ex.Message + "串口数据发送错误!rn", txtRecvInfo);serialPort1.Close();btnOpenOrClose.Text = "打开串口";}}// 发送 CRC 校验 (ModbBus CRC16)if (chbCheckCRC.Checked == true){UInt32 CRC = CRC_Check(Calculate_CRC, (byte)Calculate_CRC.Length);byte CRC_H = (byte)(CRC >> 8);byte CRC_L = (byte)CRC;try{data[0] = CRC_L;serialPort1.Write(data, 0, 1); // 发送低位data[0] = CRC_H;serialPort1.Write(data, 0, 1); //发送高位}catch (Exception ex){//在信息框中追加异常信息AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "<- ", txtRecvInfo);AppendTextToInfoBox(ex.Message + "串口数据发送错误!rn", txtRecvInfo);serialPort1.Close();btnOpenOrClose.Text = "打开串口";}}}}
private void txtSendInfo_TextChanged(object sender, EventArgs e){//备份发送栏WritePrivateProfileString("串口1", "发送栏", txtSendInfo.Text, FileName);}
清空发送
private void btnClearSend_Click(object sender, EventArgs e){txtSendInfo.Text = "";}
//自动换行
private void chbHexSend_CheckedChanged(object sender, EventArgs e){if (chbHexSend.Checked == true) // HEX发送{chbNewLine.Enabled = false; // 自动换行无效}else // ASCII码发送{chbNewLine.Enabled = true; // 自动换行有效}}
暂停按键
private void btnPuase_Click(object sender, EventArgs e){if (isRxShow == true){isRxShow = false;btnPuase.Text = "取消暂停";}else{isRxShow = true;btnPuase.Text = "暂停";}}
private void tsslClearCnt_Click(object sender, EventArgs e){tsslRecvByte.Text = "0";tsslSendByte.Text = "0";}
private void chbAutoClear_CheckedChanged(object sender, EventArgs e){if (chbAutoClear.Checked == true){ timer2.Start(); }else{ timer2.Stop(); }}
timer2
private void timer2_Tick(object sender, EventArgs e){if (txtRecvInfo.Text.Length > 4096){txtRecvInfo.Text = "";tsslRecvByte.Text = "0";}}
private void chbAutoSend_CheckedChanged(object sender, EventArgs e){// 启动定时发送if (chbAutoSend.Checked == true){AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "-> ", txtRecvInfo);AppendTextToInfoBox("启动定时发送!rn", txtRecvInfo);// 设置定时参数try{timer3.Interval = Convert.ToUInt16(txtSendCycle.Text, 10);}catch (Exception ex){AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "-> ", txtRecvInfo);AppendTextToInfoBox(ex.Message + " 输入时间有误, 设定为默认值rn", txtRecvInfo);txtSendCycle.Text = "1000";timer3.Interval = 1000;}timer3.Start(); // 启动定时器3}// 关闭定时发送else{AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "-> ", txtRecvInfo);AppendTextToInfoBox("关闭定时发送!rn", txtRecvInfo);timer3.Stop(); // 停止定时器3}}
timer3
private void timer3_Tick(object sender, EventArgs e){// 定时发送,触发发送单击事件btnSendData.PerformClick();}
选择路径按键
private void btnSelectPath_Click(object sender, EventArgs e){FolderBrowserDialog fbDialog = new FolderBrowserDialog();if (fbDialog.ShowDialog() == DialogResult.OK){txtSavePath.Text = fbDialog.SelectedPath;}}
保存数据按键
private void btnSaveData_Click(object sender, EventArgs e){if (txtRecvInfo.Text == "") return;string fileName = txtSavePath.Text + "\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".txt";StreamWriter sw = new StreamWriter(fileName); // 创建数据流sw.Write(txtRecvInfo.Text); // 写入接收框内容sw.Flush();sw.Close();MessageBox.Show("保存成功!");}
打开文件按键
private void btnOpenFile_Click(object sender, EventArgs e){OpenFileDialog ofDialog = new OpenFileDialog();ofDialog.Title = "请选择文件";ofDialog.Filter = "文本文件(*.txt)|*.txt";ofDialog.RestoreDirectory = true;if (ofDialog.ShowDialog() == DialogResult.OK){string fileName = ofDialog.FileName;txtFilePath.Text = fileName;StreamReader sr = new StreamReader(fileName, Encoding.GetEncoding("gb2312"));string strRead = sr.ReadToEnd();txtSendInfo.Text = strRead;sr.Close();}}
发送文件按键
private void btnSendFile_Click(object sender, EventArgs e){if (strRead == ""){MessageBox.Show("请先选择文件!");return;}try{// 转换数据byte[] data = Encoding.GetEncoding("gb2312").GetBytes(strRead);// 统计发送字节数UInt32 sendBytes = Convert.ToUInt32(tsslSendByte.Text, 10);sendBytes += (UInt32)data.Length;tsslSendByte.Text = sendBytes.ToString();// 分页发送int pagenum = data.Length / 4096; // 获取发送数据的页数int remaind = data.Length % 4096; // 不足一页的剩余数据for (int i = 0; i < pagenum; i++){serialPort1.Write(data, (i * 4096), 4096);Thread.Sleep(10);}if (remaind > 0){serialPort1.Write(data, (pagenum * 4096), remaind);}}catch (Exception ex){MessageBox.Show("发送数据失败" + ex.Message.ToString(), "错误");}}
private void serialPort1_DataReceived_1(object sender, SerialDataReceivedEventArgs e){if (isRxShow == false) return;Thread.Sleep(20);// 异步线程操作: 创建异步委托操作this.Invoke(new EventHandler(delegate{//以 ASCII 格式接收if (!chbHexRecv.Checked){try{if (serialPort1.BytesToRead > 0) //当串口缓存区中有数据时{// 以字符串方式读取所有接收到的数据string Str = serialPort1.ReadExisting();// 将接收到的所有 0x00 替换成 显示在界面上,因为 0x00 是一个控制字符,不会在文本框中显示Str = Str.Replace("