ImageVerifierCode 换一换
格式:DOCX , 页数:28 ,大小:56.04KB ,
资源ID:394344      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-394344.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(c#上位机串口通信助手源代码详解Word文档格式.docx)为本站会员(b****1)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

c#上位机串口通信助手源代码详解Word文档格式.docx

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

2、neric;using System.IO.Ports;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 串口助手sdd class Methods /获取有效的COM口 public static string ActivePorts() ArrayList activePorts = new ArrayList(); foreach (string pname in SerialPort.GetPortNames() activePorts.Add(Convert.ToInt32(pname

3、.Substring(3); activePorts.Sort(); string mystr = new 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 = n

4、ew BytehexValuesSplit.Length; Console.WriteLine(hexValuesSplit.Length); for (int i = 0; i hexValuesSplit.Length; i+) hexValuesi = Convert.ToByte(hexValuesSpliti, 16); return hexValues; /byte数组以16进制形式转字符串 public static string ByteTo16Str(byte bytes) string recData = null;/创建接收数据的字符串 foreach (byte out

5、Byte in bytes)/将字节数组以16进制形式遍历到一个字符串内 recData += outByte.ToString(X2) + ; return recData; /16进制字符串转换字符串 public static string _16strToStr(string _16str) string outStr = null; byte streamByte = _16strToHex(_16str); outStr = Encoding.Default.GetString(streamByte); return outStr; 2 Form1.cs的代码为:using Sys

6、tem.ComponentModel;using System.Data;using System.Drawing;using System.Text.RegularExpressions;using System.Windows.Forms;using System.IO; public partial class Form1 : Form /声明变量 SerialPort sp = new SerialPort(); bool isSetProperty = false;/串口属性设置标志位 private enum PortState/声明接口显示状态,枚举型 打开, 关闭 string

7、 path = AppDomain.CurrentDomain.BaseDirectory + confing.ini/声明配置文件路径 string tbSendDataStr = /发送窗口字符串存储 string tbSendData16 = /发送窗口16进制存储 List receivedDatas = new List();/接收数据泛型数组 /接收串口数据 private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e) byte ReceivedData = new bytesp.BytesTo

8、Read;/创建接收字节数组 sp.Read(ReceivedData, 0, ReceivedData.Length);/读取所接收到的数据 receivedDatas.AddRange(ReceivedData); tbReceivedCount.Text = (Convert.ToInt32(tbReceivedCount.Text) + ReceivedData.Length).ToString(); if (cb16Display.Checked) tbReceivedData.Text = Methods.ByteTo16Str(receivedDatas.ToArray(); e

9、lse tbReceivedData.Text = Encoding.Default.GetString(receivedDatas.ToArray(); sp.DiscardInBuffer();/丢弃接收缓冲区数据 /发送串口数据 private void DataSend() try if (cb16Send.Checked) byte hexBytes = Methods._16strToHex(tbSendData16); sp.Write(hexBytes, 0, hexBytes.Length); tbSendCount.Text = (Convert.ToInt32(tbSen

10、dCount.Text) + hexBytes.Length).ToString(); sp.WriteLine(tbSendDataStr); tbSendCount.Text = (Convert.ToInt32(tbSendCount.Text) + tbSendDataStr.Length).ToString(); catch (Exception ex) MessageBox.Show(ex.Message.ToString(); return; /设置串口属性 private void SetPortProperty() sp.PortName = cbxCOMPort.Text.

11、Trim();/设置串口名 sp.BaudRate = Convert.ToInt32(cbxBaudRate.Text.Trim();/设置波特率 switch (cbxStopBits.Text.Trim()/设置停止位 case 1: sp.StopBits = StopBits.One; break;1.5 sp.StopBits = StopBits.OnePointFive;2 sp.StopBits = StopBits.Two; default: sp.StopBits = StopBits.None; sp.DataBits = Convert.ToInt32(cbxData

12、Bits.Text.Trim();/设置数据位 switch (cbxParity.Text.Trim()/设置奇偶校验位 无 sp.Parity = Parity.None;奇校验 sp.Parity = Parity.Odd;偶校验 sp.Parity = Parity.Even; sp.ReadTimeout = 5000;/设置超时时间为5s Control.CheckForIllegalCrossThreadCalls = false;/这个类中我们不检查跨线程的调用是否合法(因为.net 2.0以后加强了安全机制,,不允许在winform中直接跨线程访问控件的属性) /定义Data

13、Received事件的委托,当串口收到数据后出发事件 sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived); /设置端口显示状态 private void DisplayPortState(PortState portState) toolStripStatusLabel1.Text = cbxCOMPort.Text + 端口 处于 + portState + 状态 + cbxBaudRate.Text + + cbxDataBits.Text + + cbxStopBits.Text + + cbxPa

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

15、() InitializeComponent(); /软件启动时加载事件 private void Form1_Load(object sender, EventArgs e) #region 加载配置文件 Hashtable ht = new Hashtable(); if (File.Exists(path) string myline = string str = new string2; using (StreamReader sr = new StreamReader(path) myline = sr.ReadLine(); while (myline != null) str =

16、 myline.Split(= ht.Add(str0, str1); catch(Exception ex) #endregion #region 设置窗口为固定大小且不可最大化 this.MaximumSize = this.Size; this.MinimumSize = this.Size; this.MaximizeBox = false; #region 列出常用的波特率 cbxBaudRate.Items.Add(12002400480096001920038400430005600057600115200 if (ht.ContainsKey(cbxBaudRate) cbxB

17、audRate.SelectedIndex = cbxBaudRate.Items.IndexOf(ht.ToString(); cbxBaudRate.SelectedIndex = 3; cbxBaudRate.DropDownStyle = ComboBoxStyle.DropDownList; #region 列出停止位 cbxStopBits.Items.Add(cbxStopBits cbxStopBits.SelectedIndex = cbxStopBits.Items.IndexOf(ht cbxStopBits.SelectedIndex = 0; cbxStopBits.

18、DropDownStyle = ComboBoxStyle.DropDownList; #region 列出数据位 cbxDataBits.Items.Add(8765cbxDataBits cbxDataBits.SelectedIndex = cbxDataBits.Items.IndexOf(ht cbxDataBits.SelectedIndex = 0; cbxDataBits.DropDownStyle = ComboBoxStyle.DropDownList; #region 列出奇偶校验位 cbxParity.Items.Add(cbxParity cbxParity.Sele

19、ctedIndex = cbxParity.Items.IndexOf(ht cbxParity.SelectedIndex = 0; cbxParity.DropDownStyle = ComboBoxStyle.DropDownList; #region COM口重新加载 cbxCOMPort.Items.Clear();/清除当前串口号中的所有串口名称 cbxCOMPort.Items.AddRange(Methods.ActivePorts();cbxCOMPort) & cbxCOMPort.Items.Contains(ht.ToString() cbxCOMPort.Select

20、edIndex = cbxCOMPort.Items.IndexOf(ht cbxCOMPort.SelectedIndex = 0; cbxCOMPort.DropDownStyle = ComboBoxStyle.DropDownList; #region 初始化计数器 tbSendCount.Text = 0 tbSendCount.ReadOnly = true; tbReceivedCount.Text = tbReceivedCount.ReadOnly = true; #region 初始化当前时间 toolStripStatusLabel3.Text = DateTime.No

21、w.ToString(); #region 初始化串口状态 toolStripStatusLabel1.ForeColor = Color.Blue; if (!isSetProperty)/串口未设置则设置串口属性关闭串口 /串口打开失败后,串口属性设置标志位设为false #region 初始化间隔时间tbSpaceTime tbSpaceTime.Text = ht.ToString(); tbSpaceTime.Text = 1000 #region 初始化按16进制显示状态cb16Display ht.ToString() = True) cb16Display.Checked = true; cb16Display.Checked = false; #region 初始化按16进制发送状态cb16Send cb16Send.Checked = true; cb16Send.Checked = false; #region 初始化发送区文本 if(ht.ContainsKey(tbSendData16 ht.ContainsKey(tbSendDataStr tbSendData16 = ht tbSendDataStr = ht tbSendData.Text = ht tbSendData.Focus(); /显

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2