📄 z_comm_ex.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using Microsoft.Win32;
namespace Z_COMM_EX
{
/// <summary>
/// UserControl1 的摘要说明。
/// </summary>
///
public delegate void SecManageHandle(object sender,EventArgs e);
public class Z_COMM_EX : System.Windows.Forms.UserControl
{
private System.Windows.Forms.GroupBox qqq;
private System.Windows.Forms.ComboBox cc2;
private System.Windows.Forms.ComboBox cc1;
private System.Windows.Forms.Label l2;
private System.Windows.Forms.Label l1;
private bool ReceiveFlag=false;//是否接收数据的标志
private ArrayList receivedatabyte=new ArrayList();//十进制数
private ArrayList receivedataorigin=new ArrayList();//原始数据
private int receivenum=0;//
private byte databegin; //数据包开始字符
private byte dataend; //数据包结束字符
public AxMSCommLib.AxMSComm axMSComm1;
[Description("接收完数据事件。当将串口接收到的数据保存至数组receivedataorigin中后产生"),Category("数据处理")]
public event SecManageHandle SecManage;
protected virtual void OnSecManage(EventArgs e)
{
if(SecManage!=null)
SecManage(this,e);
}
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Z_COMM_EX()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();
this.ReadSet();
this.Resize+=new EventHandler(Z_COMM_EX_Resize);
SetStyle(System.Windows.Forms.ControlStyles.SupportsTransparentBackColor,true);
// TODO: 在 InitComponent 调用后添加任何初始化
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Z_COMM_EX));
this.qqq = new System.Windows.Forms.GroupBox();
this.axMSComm1 = new AxMSCommLib.AxMSComm();
this.cc2 = new System.Windows.Forms.ComboBox();
this.cc1 = new System.Windows.Forms.ComboBox();
this.l2 = new System.Windows.Forms.Label();
this.l1 = new System.Windows.Forms.Label();
this.qqq.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.axMSComm1)).BeginInit();
this.SuspendLayout();
//
// qqq
//
this.qqq.Controls.Add(this.axMSComm1);
this.qqq.Controls.Add(this.cc2);
this.qqq.Controls.Add(this.cc1);
this.qqq.Controls.Add(this.l2);
this.qqq.Controls.Add(this.l1);
this.qqq.Location = new System.Drawing.Point(0, 0);
this.qqq.Name = "qqq";
this.qqq.Size = new System.Drawing.Size(184, 88);
this.qqq.TabIndex = 0;
this.qqq.TabStop = false;
this.qqq.Text = "串口设置";
//
// axMSComm1
//
this.axMSComm1.ContainingControl = this;
this.axMSComm1.Enabled = true;
this.axMSComm1.Location = new System.Drawing.Point(16, 40);
this.axMSComm1.Name = "axMSComm1";
this.axMSComm1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMSComm1.OcxState")));
this.axMSComm1.Size = new System.Drawing.Size(38, 38);
this.axMSComm1.TabIndex = 5;
this.axMSComm1.OnComm += new System.EventHandler(this.axMSComm1_OnComm);
//
// cc2
//
this.cc2.Items.AddRange(new object[] {
"1200",
"2400",
"9600",
"14400",
"19200",
"28800",
"38400",
"115200"});
this.cc2.Location = new System.Drawing.Point(80, 56);
this.cc2.Name = "cc2";
this.cc2.Size = new System.Drawing.Size(88, 20);
this.cc2.TabIndex = 4;
this.cc2.Text = "9600";
//
// cc1
//
this.cc1.Items.AddRange(new object[] {
"COM1",
"COM2",
"COM3",
"COM4"});
this.cc1.Location = new System.Drawing.Point(80, 24);
this.cc1.Name = "cc1";
this.cc1.Size = new System.Drawing.Size(88, 20);
this.cc1.TabIndex = 3;
this.cc1.Text = "COM1";
//
// l2
//
this.l2.AutoSize = true;
this.l2.Location = new System.Drawing.Point(8, 56);
this.l2.Name = "l2";
this.l2.Size = new System.Drawing.Size(66, 17);
this.l2.TabIndex = 2;
this.l2.Text = " 波特率:";
this.l2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// l1
//
this.l1.AutoSize = true;
this.l1.Location = new System.Drawing.Point(8, 24);
this.l1.Name = "l1";
this.l1.Size = new System.Drawing.Size(66, 17);
this.l1.TabIndex = 1;
this.l1.Text = "通信端口:";
this.l1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// Z_COMM_EX
//
this.Controls.Add(this.qqq);
this.Name = "Z_COMM_EX";
this.Size = new System.Drawing.Size(192, 96);
this.qqq.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.axMSComm1)).EndInit();
this.ResumeLayout(false);
}
#endregion
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
this.DrawControl();
}
private void DrawControl()
{
this.qqq.Width=this.Width;
this.qqq.Height=this.Height;
this.l1.Location=new Point(10,this.cc1.Height+5);
this.l2.Location=new Point(10,(int)(this.cc1.Height*2.5)+5);
this.cc1.Location=new Point(this.l1.Width+10,this.cc1.Height);
this.cc2.Location=new Point(this.l2.Width+10,(int)(this.cc1.Height*2.5));
this.cc1.Width=this.Width-this.l1.Width-20;
this.cc2.Width=this.Width-this.l2.Width-20;
this.Height=this.cc2.Location.Y+(int)(1.5*this.cc2.Height);
}
private void Z_COMM_EX_Resize(object sender, EventArgs e)
{
this.Invalidate();
}
[Description("字体"),Category("设置")]
public Font Z_COMMEXFont
{
get
{
return this.qqq.Font;
}
set
{
this.qqq.Font=value;
this.Invalidate();
}
}
[Description("颜色"),Category("设置")]
public Color Z_COMMForeColor
{
get
{
return this.qqq.ForeColor;
}
set
{
this.qqq.ForeColor=value;
this.Invalidate();
}
}
[Description("下拉列表框的颜色"),Category("设置")]
public Color Z_COMMColor
{
get
{
return this.cc1.ForeColor;
}
set
{
this.cc1.ForeColor=value;
this.cc2.ForeColor=value;
this.Invalidate();
}
}
[Description("数据包起始字符"),Category("数据包格式")]
public byte DataBegin
{
get
{
return databegin;
}
set
{
databegin=value;
}
}
[Description("数据包结束字符"),Category("数据包格式")]
public byte DataEnd
{
get
{
return dataend;
}
set
{
dataend=value;
}
}
[Description("接收到完整数据包的次数"),Category("数据包格式")]
public int ReceiveNum
{
get
{
return receivenum;
}
set
{
receivenum=value;
}
}
[Description("用于接收数据的可变数组")]
public ArrayList ReceiveDataOrigin
{
get
{
return receivedataorigin;
}
}
[Description("转化后的数据接收数组")]
public ArrayList ReceiveDataByte
{
get
{
return receivedatabyte;
}
}
/// <summary>
/// 读注册表中串口信息
/// </summary>
public void ReadSet()
{
//读注册表中串口信息
RegistryKey r=Registry.LocalMachine.CreateSubKey("software\\Z_COMM_EX");
RegistryKey r1=r.CreateSubKey("mycomm");
this.cc1.Text=(string)r1.GetValue("port","COM2");
this.cc2.Text=(string)r1.GetValue("setting","9600");
r1.Close();
r.Close();
}
/// <summary>
/// 写注册表中串口信息
/// </summary>
public void WriteSet()
{
//写注册表中串口信息
RegistryKey r=Registry.LocalMachine.CreateSubKey("software\\Z_COMM_EX");
RegistryKey r1=r.CreateSubKey("mycomm");
r1.SetValue("port",this.cc1.Text);
r1.SetValue("setting",this.cc2.Text);
r1.Close();
r.Close();
}
/// <summary>
/// 串口初始化
/// </summary>
public void InitCommPort()
{
//串口初始化
try
{
this.WriteSet();
if (this.axMSComm1.PortOpen==true)
this.axMSComm1.PortOpen=false;
//设置通信端口
if(this.cc1.Text=="COM1")
this.axMSComm1.CommPort=1;
else if(this.cc1.Text=="COM2")
this.axMSComm1.CommPort=2;
else if(this.cc1.Text=="COM3")
this.axMSComm1.CommPort=3;
else
this.axMSComm1.CommPort=4;
//设置波特率
this.axMSComm1.Settings=this.cc2.Text+",N,8,1";
// this.axMSComm1.InputLen=1;
this.axMSComm1.RThreshold=1;
this.axMSComm1.PortOpen=true; //打开串口
}
catch(Exception e)
{
DialogResult result;
result=MessageBox.Show(e.Message+"\r请重新配置串口!","消息提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
private void axMSComm1_OnComm(object sender, System.EventArgs e)
{
//"F0"起始,"FF"结束
object av;
byte[] a;
if(this.axMSComm1.CommEvent==2)
{
av=this.axMSComm1.Input;
a=(byte[])av;
if(!ReceiveFlag) //检验是否收到数据包开始标志
{
for(int i=0;i<a.Length;i++)
{
if(a[i]==this.DataBegin)
{
ReceiveFlag=true;
ReceiveDataOrigin.Clear();
}
if(ReceiveFlag)//收到数据包开始标志,但此次读取的数据还没有转化完时,将包中的数据转存入ReceiveDataOrigin,包括开始标志“F0”
{
ReceiveDataOrigin.Add(a[i]);
}
if(a[i]==dataend)
{
ReceiveFlag=false;
PreManage();
}
}
}
else
{
for(int i=0;i<a.Length;i++)
{
//收到数据包结束标志,将ReceiveFlag付值为false,将包中的数据转存入ReceiveDataOrigin,包括结束标志“FF”
ReceiveDataOrigin.Add(a[i]);
if(a[i]==dataend)
{
ReceiveFlag=false;
PreManage();
}
}
}
}
}
/// <summary>
/// 预处理。当接收本数据包数据完成后,将调用此函数完成初级的处理
/// </summary>
private void PreManage()
{
//预处理。当接收本数据包数据完成后,将调用此函数完成初级的处理。包括:1、把bcd码转化为二进制 2、引发SecManage事件,把控制权交给其他相关窗体
this.ReceiveNum++;//接收到完整数据包的次数加1
OnSecManage(EventArgs.Empty);//引发SecMange事件,在相关窗体中进行深度处理。
}
/// <summary>
/// 把接收到的数据去掉开始、结束标志,并转化为十进制数
/// </summary>
public void ChangeBcd()
{
byte a;
byte b;
this.ReceiveDataByte.Clear();
for(int i=1;i<this.ReceiveDataOrigin.Count-1;i++)
{
a=(byte)this.ReceiveDataOrigin[i];
// this.ReceiveDataByte.Add(Convert.ToByte(a.ToString("X"),10));
b=(byte)(a-a/16*6);
this.receivedatabyte.Add(b);
}
}
public void SendArray(byte[] b)
{
this.axMSComm1.Output=b;
}
public void ClosePort()
{
this.axMSComm1.PortOpen=false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -