c#上位机串口通信助手源代码详解

上传人:繁星 文档编号:42914323 上传时间:2018-06-04 格式:DOCX 页数:16 大小:67.38KB
返回 下载 相关 举报
c#上位机串口通信助手源代码详解_第1页
第1页 / 共16页
c#上位机串口通信助手源代码详解_第2页
第2页 / 共16页
c#上位机串口通信助手源代码详解_第3页
第3页 / 共16页
c#上位机串口通信助手源代码详解_第4页
第4页 / 共16页
c#上位机串口通信助手源代码详解_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《c#上位机串口通信助手源代码详解》由会员分享,可在线阅读,更多相关《c#上位机串口通信助手源代码详解(16页珍藏版)》请在金锄头文库上搜索。

1、c#上位机串口通信助手源代码实例详解一、功能1 软件打开时,自动检测有效 COM 端口2 软件打开时,自动复原到上次关闭时的状态3 不必关闭串口,即可直接进行更改初始化设置内容(串口号、波特率、数据位、停止位、校验位),可按更改后的信息自动将串口重新打开4 可统计接收字节和发送字节的个数5 接收数据可按 16 进制数据和非 16 进制数据进行整体转换6 可将接收到数据进行保存7 可设置自动发送,发送时间可进行实时更改8 可按字符串、16 进制字节、文件方式进行发送,字符串和 16 进制字节可分别进行存储,内容互不干扰9 按 16 进制发送时,可自动校验格式,不会输错10 可清空发送或接收区域的

2、数据二、使用工具Visual Studio2015三、程序详解1 界面创建图 1用 winform 创建如图 1 所示界面,控件名字分别为:端口号:cbxCOMPort 波特率:cbxBaudRate数据位:cbxDataBits 停止位:cbxStopBits校验位:label5 打开串口按钮:btnOpenCom发送(byte):tbSendCount 接收(byte):tbReceivedCount清空计数按钮:btnClearCount 按 16 进制显示:cb16Display接收区清空内容按钮:btnClearReceived 保存数据按钮:btnSaveFile接收数据框:tbR

3、eceivedData 发送数据框:tbSendData自动发送:cbAutomaticSend 间隔时间:tbSpaceTime按 16 进制发送:cb16Send 发送区清空内容按钮:btnClearSend读入文件按钮:btnReadFile 发送按钮:btnSend2 创建一个方法类按 Ctrl+shift+A 快捷键创建一个类,名字叫 Methods,代码为:using System; using System.Collections; using System.Collections.Generic; using System.IO.Ports; using System.Linq

4、; using System.Text; using System.Threading.Tasks;namespace 串口助手sddclass Methods/获取有效的COM口public static string ActivePorts()ArrayList activePorts = new ArrayList();foreach (string pname in SerialPort.GetPortNames()activePorts.Add(Convert.ToInt32(pname.Substring(3);activePorts.Sort();string mystr = n

5、ew stringactivePorts.Count;int i = 0;foreach (int num in activePorts)mystri+ = “COM“ + num.ToString();return mystr;/16进制字符串转换为byte字符数组public static Byte _16strToHex(string strValues)string hexValuesSplit = strValues.Split( );Byte hexValues = new BytehexValuesSplit.Length;Console.WriteLine(hexValuesS

6、plit.Length);for (int i = 0; i receivedDatas = new List();/接收数据泛型数组/接收串口数据private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)byte ReceivedData = new bytesp.BytesToRead;/创建接收字节数组sp.Read(ReceivedData, 0, ReceivedData.Length);/读取所接收到的数据receivedDatas.AddRange(ReceivedData);tbRecei

7、vedCount.Text = (Convert.ToInt32(tbReceivedCount.Text) + ReceivedData.Length).ToString();if (cb16Display.Checked)tbReceivedData.Text = Methods.ByteTo16Str(receivedDatas.ToArray();elsetbReceivedData.Text = Encoding.Default.GetString(receivedDatas.ToArray();sp.DiscardInBuffer();/丢弃接收缓冲区数据/发送串口数据privat

8、e void DataSend()tryif (cb16Send.Checked)byte hexBytes = Methods._16strToHex(tbSendData16);sp.Write(hexBytes, 0, hexBytes.Length);tbSendCount.Text = (Convert.ToInt32(tbSendCount.Text) + hexBytes.Length).ToString();elsesp.WriteLine(tbSendDataStr);tbSendCount.Text = (Convert.ToInt32(tbSendCount.Text)

9、+ tbSendDataStr.Length).ToString();catch (Exception ex)MessageBox.Show(ex.Message.ToString();return;/设置串口属性private void SetPortProperty()sp.PortName = cbxCOMPort.Text.Trim();/设置串口名sp.BaudRate = Convert.ToInt32(cbxBaudRate.Text.Trim();/设置波特率switch (cbxStopBits.Text.Trim()/设置停止位case “1“: sp.StopBits =

10、 StopBits.One; break;case “1.5“: sp.StopBits = StopBits.OnePointFive; break;case “2“: sp.StopBits = StopBits.Two; break;default: sp.StopBits = StopBits.None; break;sp.DataBits = Convert.ToInt32(cbxDataBits.Text.Trim();/设置数据位switch (cbxParity.Text.Trim()/设置奇偶校验位 case “无“: sp.Parity = Parity.None; bre

11、ak;case “奇校验“: sp.Parity = Parity.Odd; break;case “偶校验“: sp.Parity = Parity.Even; break;default: sp.Parity = Parity.None; break;sp.ReadTimeout = 5000;/设置超时时间为5sControl.CheckForIllegalCrossThreadCalls = false;/这个类中我们不检查跨线程的调用是否合法(因为.net 2.0以后加强了安全机制,,不允许在winform中直接跨线程访问控件的属性)/定义DataReceived事件的委托,当串口收

12、到数据后出发事件sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);/设置端口显示状态private void DisplayPortState(PortState portState)toolStripStatusLabel1.Text = cbxCOMPort.Text + “端口 处于“ + portState + “状态 “ + cbxBaudRate.Text + “ “ + cbxDataBits.Text + “ “ + cbxStopBits.Text + “ “ + cbxParity.

13、Text;/重新打开串口private void AgainOpenPort()if (sp.IsOpen)trysp.Close();SetPortProperty();isSetProperty = true;sp.Open();catch (Exception)isSetProperty = false;btnOpenCom.Text = “打开串口“;DisplayPortState(PortState.关闭);MessageBox.Show(“串口无效或已被占用!“, “错误提示“);return;DisplayPortState(PortState.打开);elseDisplayP

14、ortState(PortState.关闭);public Form1()InitializeComponent();/软件启动时加载事件private void Form1_Load(object sender, EventArgs e)#region 加载配置文件Hashtable ht = new Hashtable();if (File.Exists(path)trystring myline = “;string str = new string2;using (StreamReader sr = new StreamReader(path)myline = sr.ReadLine(

15、);while (myline != null)str = myline.Split(=);ht.Add(str0, str1);myline = sr.ReadLine();catch(Exception ex)MessageBox.Show(ex.Message.ToString();#endregion#region 设置窗口为固定大小且不可最大化this.MaximumSize = this.Size;this.MinimumSize = this.Size;this.MaximizeBox = false;#endregion#region 列出常用的波特率cbxBaudRate.Items.Add(“1200“);cbxBaudRate.Items.Add(

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 总结/报告

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号