本文主要是介绍上位机的模板展示和学习上位机的心得体会,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
上位机,从广义的角度来说,一切可以和下位机进行通信的HMI都可以称作为上位机,从狭义的角度来说,是指采用计算机高级语言编写的上层监控系统,这里常用的编程语言包括C++、JAVA、Python、C#,毋庸置疑,在工控行业,最适合工控上位机开发的编程语言莫过于微软旗下的C#语言。
下面是我用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(@"Hardware\DeviceMap\SerialComm");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("没检测到可用串口!\r\n", 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("扫描和添加串口时,关闭串口。\r\n", 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("手动关闭串口。\r\n", txtRecvInfo);timer1.Stop(); // 关闭串口工具异常检测}else{// 打开串口try{serialPort1.PortName = cmbSerialPort.Text; // 端口号serialPort1.Open();btnOpenOrClose.Text = "关闭串口";AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "->", txtRecvInfo);AppendTextToInfoBox("串口打开成功。\r\n", txtRecvInfo);timer1.Start(); // 启用串口工具异常检测}catch (Exception ex){// 串口异常AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "-> ", txtRecvInfo);AppendTextToInfoBox("串口打开失败。\r\n", txtRecvInfo);AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "-> ", txtRecvInfo);AppendTextToInfoBox(ex.Message + "\r\n", 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("串口变更成功。\r\n", txtRecvInfo);timer1.Start();// 启用串口工具异常检测}catch (Exception ex){serialPort1.Close();AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "" + "-> ", txtRecvInfo);AppendTextToInfoBox("串口变更失败。\r\n", txtRecvInfo);AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "->", txtRecvInfo);AppendTextToInfoBox(ex.Message + " r\n", 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("波特率变更成功。 r\n",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 + "串口数据发送错误! \r\n", 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 + "串口数据发送错误!\r\n", 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 + "串口数据发送错误!\r\n", 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("启动定时发送!\r\n", txtRecvInfo);// 设置定时参数try{timer3.Interval = Convert.ToUInt16(txtSendCycle.Text, 10);}catch (Exception ex){AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "-> ", txtRecvInfo);AppendTextToInfoBox(ex.Message + " 输入时间有误, 设定为默认值\r\n", txtRecvInfo);txtSendCycle.Text = "1000";timer3.Interval = 1000;}timer3.Start(); // 启动定时器3}// 关闭定时发送else{AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "-> ", txtRecvInfo);AppendTextToInfoBox("关闭定时发送!\r\n", 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 替换成 \0 显示在界面上,因为 0x00 是一个控制字符,不会在文本框中显示Str = Str.Replace("\0", "\\0");//在信息框中追加接收的数据,其中包括接收时间和接收内容//txtRecvInfo.Text = $"(Str]r\n";AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "<-", txtRecvInfo);AppendTextToInfoBox(Str + " \r\n", txtRecvInfo);// 统计接收字节数UInt32 recvBytes = Convert.ToUInt32(tsslRecvByte.Text, 10); // 定义接收字节数变量,并初始化为已接收字节数recvBytes += (UInt32)Str.Length; // 加 ASCII 码字节数tsslRecvByte.Text = Convert.ToString(recvBytes, 10); // 显示总接收字节数}}catch (Exception ex){//在信息框中追加异常信息AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "<-", txtRecvInfo);AppendTextToInfoBox(ex.Message + " \r\n", txtRecvInfo);AppendTextToInfoBox("ASCII格式接收错误!\r\n", txtRecvInfo);}}else //以 HEX 格式接收{try{byte[] data = new byte[serialPort1.BytesToRead]; // 定义接收缓冲区serialPort1.Read(data, 0, data.Length); // 读取缓冲区中所有的数据string str = ToHexString(data, 0, " "); // 将数据转换为 HEX 字符串格式AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "<-", txtRecvInfo);AppendTextToInfoBox(str + "\r\n", txtRecvInfo);// 统计接收字节数UInt32 recvBytes = Convert.ToUInt32(tsslRecvByte.Text, 10); // 定义接收字节数变量,并初始化为已接收字节数recvBytes += (UInt32)data.Length; // 加 ASCII 码字节数tsslRecvByte.Text = Convert.ToString(recvBytes, 10); // 显示总接收字节数}catch{AppendTextToInfoBox("[" + DateTime.Now.ToString("HH:mm:ss") + "]" + "<- \r\n", txtRecvInfo);AppendTextToInfoBox("HEX格式接收错误!r\n", txtRecvInfo);}}}));}
三、学习心得
一开始学习永远都是跟着老师操作,如何听老师逐步讲解,这是刚学习一门课程的开头要做的。
到了后面我们就要学会慢慢运用自己学到的知识去尝试一些与其相关的项目玩玩,同时巩固知识。正所谓老师带入门修行看个人,学习不只是一个人闭门造车,更多的是要去看看其他人的想法和观念,我们要学会主动去学,主动去找资料,这也是我们以后面对社会要学会的一种能力。
成功没有捷径,只有靠自己的努力和付出才能取得胜利。在学习的路程上,有着许多困难和挫折,有人没有勇气度过,从而浑浑噩噩度过一生,有人则披荆斩棘,尝到了胜利的果实。
学习更要有“勤奋、刻苦、踏实”的精神。“书山有路勤为径,学海无涯苦作舟”,这对联就很好的`诠释了勤奋与刻苦。如果不踏踏实实地学,又找不到适当的学习英语的方法,就会苦恼、郁闷,因而陷入困境因此,在学习上不仅要有刻苦、勤奋的精神,而且要找到恰当、适合自己的学习方法,在学习之路上踏踏实实地走好每一步。
“预则立,不预则废”每次课前都要先预习下,下节课学习的内容,不但能使我们的自主学习能力有很大的提高,还能排除对老师的依赖。如果不课前预习,就会比别人慢一拍,造成课堂学习上“手忙脚乱”的窘相。
“温故而知新”当老师讲完课之后,我们应该做到即时复习,巩固学过的知识,记忆得更牢固。我们也可以试试为自己制定一个学习计划,但不要天马行空,纸上谈兵,否则到头来还是竹篮打水——一场空。
学习方法因人而异,各不相同,愿同学们找到适合自己的学习方法,在学习生涯中绘上精彩的一笔!
每个选择都会导致今后的人生大变样,考虑好自己的将来,规划好自己的人生,所以静下心来好好学习吧,没有谁是一开始就全明白的,都是靠不断的摸索和尝试中掌握的知识。
这篇关于上位机的模板展示和学习上位机的心得体会的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!