/***************************************************** * ProjectName: BXHSerialPort * Description: * ClassName: Class1 * CLRVersion: 4.0.30319.42000 * Author: JiYF * NameSpace: BXHSerialPort * MachineName: JIYF_PC * CreateTime: 2017/3/25 16:46:14 * UpdatedTime: 2017/3/25 16:46:14*****************************************************/using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO.Ports;//串口调用对应的引用using System.Linq;using System.Text;using System.Windows.Forms;namespace BXHSerialPort{ public partial class frmSerialPort : Form { private SerialPort ComDevice = new SerialPort(); public frmSerialPort() { InitializeComponent(); this.init(); } public void init() //串口界面初始化 { btnSend.Enabled = false; cbbComList.Items.AddRange(SerialPort.GetPortNames());//自动获取RS232串口号 if (cbbComList.Items.Count > 0) { cbbComList.SelectedIndex = 0; } cbbBaudRate.SelectedIndex = 5; cbbDataBits.SelectedIndex = 0; cbbParity.SelectedIndex = 0; cbbStopBits.SelectedIndex = 0; pictureBox1.BackgroundImage = Properties.Resources.red; //串口打开的背景图片显示 ComDevice.DataReceived = new SerialDataReceivedEventHandler(Com_DataReceived);//绑定事件 } /// <summary> /// 打开串口 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOpen_Click(object sender, EventArgs e) { if (cbbComList.Items.Count <= 0) { MessageBox.Show("没有发现串口,请检查线路!"); return; } if (ComDevice.IsOpen == false) { ComDevice.PortName = cbbComList.SelectedItem.ToString(); ComDevice.BaudRate = Convert.ToInt32(cbbBaudRate.SelectedItem.ToString()); ComDevice.Parity = (Parity)Convert.ToInt32(cbbParity.SelectedIndex.ToString()); ComDevice.DataBits = Convert.ToInt32(cbbDataBits.SelectedItem.ToString()); ComDevice.StopBits = (StopBits)Convert.ToInt32(cbbStopBits.SelectedItem.ToString()); try { ComDevice.Open(); btnSend.Enabled = true; } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } btnOpen.Text = "关闭串口"; pictureBox1.BackgroundImage = Properties.Resources.green; } else { try { ComDevice.Close(); btnSend.Enabled = false; } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } btnOpen.Text = "打开串口"; pictureBox1.BackgroundImage = Properties.Resources.red; } cbbComList.Enabled = !ComDevice.IsOpen; cbbBaudRate.Enabled = !ComDevice.IsOpen; cbbParity.Enabled = !ComDevice.IsOpen; cbbDataBits.Enabled = !ComDevice.IsOpen; cbbStopBits.Enabled = !ComDevice.IsOpen; } /// <summary> /// 关闭串口 /// </summary> public void ClearSelf() { if (ComDevice.IsOpen) { ComDevice.Close(); } } /// <summary> /// 发送数据 /// </summary> /// <param name="sender"></param> /// <param name="data"></param> public bool SendData(byte[] data) { if (ComDevice.IsOpen) { try { ComDevice.Write(data, 0, data.Length);//发送数据 return true; } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("串口未打开", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } return false; } /// <summary> /// 发送数据button事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSend_Click(object sender, EventArgs e) { byte[] sendData = null; if (rbtnSendHex.Checked) { sendData = strToHexByte(txtSendData.Text.Trim()); } else if (rbtnSendASCII.Checked) { sendData = Encoding.ASCII.GetBytes(txtSendData.Text.Trim()); } else if (rbtnSendUTF8.Checked) { sendData = Encoding.UTF8.GetBytes(txtSendData.Text.Trim()); } else if (rbtnSendUnicode.Checked) { sendData = Encoding.Unicode.GetBytes(txtSendData.Text.Trim()); } else { sendData = Encoding.ASCII.GetBytes(txtSendData.Text.Trim()); } if (this.SendData(sendData))//发送数据成功计数 { lblSendCount.Invoke(new MethodInvoker(delegate { lblSendCount.Text = (int.Parse(lblSendCount.Text) txtSendData.Text.Length).ToString(); })); } else { } } /// <summary> /// 字符串转换16进制字节数组 /// </summary> /// <param name="hexString"></param> /// <returns></returns> private byte[] strToHexByte(string hexString) { hexString = hexString.Replace(" ", ""); if ((hexString.Length % 2) != 0) hexString = " "; byte[] returnBytes = new byte[hexString.Length / 2]; for (int i = 0; i < returnBytes.Length; i ) returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Replace(" ",""), 16); return returnBytes; } /// <summary> /// 接收数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Com_DataReceived(object sender, SerialDataReceivedEventArgs e) { byte[] ReDatas = new byte[ComDevice.BytesToRead]; ComDevice.Read(ReDatas, 0, ReDatas.Length);//读取数据 this.AddData(ReDatas);//输出数据 } /// <summary> /// 添加数据 /// </summary> /// <param name="data">字节数组</param> public void AddData(byte[] data) { if (rbtnHex.Checked) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < data.Length; i ) { sb.AppendFormat("{0:x2}" " ", data[i]); } AddContent(sb.ToString().ToUpper()); } else if (rbtnASCII.Checked) { AddContent(new ASCIIEncoding().GetString(data)); } else if (rbtnUTF8.Checked) { AddContent(new UTF8Encoding().GetString(data)); } else if (rbtnUnicode.Checked) { AddContent(new UnicodeEncoding().GetString(data)); } else {} lblRevCount.Invoke(new MethodInvoker(delegate { lblRevCount.Text = (int.Parse(lblRevCount.Text) data.Length).ToString(); })); } /// <summary> /// 输入到显示区域 /// </summary> /// <param name="content"></param> private void AddContent(string content) { this.BeginInvoke(new MethodInvoker(delegate { if(chkAutoLine.Checked && txtShowData.Text.Length>0) { txtShowData.AppendText("\r\n"); } txtShowData.AppendText(content); })); } /// <summary> /// 清空接收区 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnClearRev_Click(object sender, EventArgs e) { txtShowData.Clear(); } /// <summary> /// 清空发送区 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnClearSend_Click(object sender, EventArgs e) { txtSendData.Clear(); } }}
下载RS232串口调试助手用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论