MSCOMM32.OCX在C#中的调用

来源:互联网 发布:mysql主从配置 指定表 编辑:程序博客网 时间:2024/05/20 05:06
MSCOMM32.OCX在C#中的调用
调用MSCOMM32.OCX控件可以实现串口通信,最近一直在做这方面的工作,所以把它记下来。
一。注册
第一步:用Windows下的注册工具regsvr32注册该OCX控件,点击“开始”->"运行",输入:
Regsvr32 控件所在路径/Mscomm32.ocx
 
 第二步:在注册表中手工新建一个主键项,找到HKEY_CLASSES_ROOT/Licenses,在其中添加主键
4250E830-6AC2-11cf-8ADB-00AA00C00905 并将内容设置为: kjljvjjjoquqmjjjvpqqkqmqykypoqjquoun
二。添加
将注册好的控件添加到工具箱,可以像使用普通工具一样使用。也就是直接拖入窗体,然后对其编程。
三。代码
这是一个关于ID卡读卡器的源代码,首先打开串口,然后向串口发送一串命令,使其读ID卡数据,再接着接收缓冲中的ID卡数据。
1 using System;
2 using System.Drawing;
3 using System.Collections;
4 using System.ComponentModel;
5 using System.Windows.Forms;
6 using System.Data;
7 using System.Text;
8 using System.Timers;
9 using System.Threading;
10 using Microsoft.VisualBasic;
11 namespace cardtest
12 {
13 ///
14 /// Form1 的摘要说明。
15 ///

16 public class Form1 : System.Windows.Forms.Form
17 {
18 private System.Windows.Forms.TextBox TB_cardnum;
19 private System.Windows.Forms.Button but_open;
20 private System.Windows.Forms.Button but_exit;
21 private System.Windows.Forms.Button but_read;
22 private System.Windows.Forms.Button but_stop;
23 private AxMSCommLib.AxMSComm msc;
24 private System.Windows.Forms.TextBox TB_paut;
25 private System.Windows.Forms.Label lbl_info;
26 private System.ComponentModel.IContainer components=null;
27 private System.Timers.Timer atimer=new System.Timers.Timer();
28 public Form1()
29 {
30 //
31 // Windows 窗体设计器支持所必需的
32 //
33 InitializeComponent();
34
35 //
36 // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
37 //
38 }
39
40 ///
41 /// 清理所有正在使用的资源。
42 ///

43 protected override void Dispose( bool disposing )
44 {
45 if( disposing )
46 {
47 if (components != null)
48 {
49 components.Dispose();
50 }
51 }
52 base.Dispose( disposing );
53 }
54
55 #region Windows 窗体设计器生成的代码
56 ///
57 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
58 /// 此方法的内容。
59 ///

60 private void InitializeComponent()
61 {
62 System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
63 this.TB_paut = new System.Windows.Forms.TextBox();
64 this.TB_cardnum = new System.Windows.Forms.TextBox();
65 this.but_open = new System.Windows.Forms.Button();
66 this.but_exit = new System.Windows.Forms.Button();
67 this.but_read = new System.Windows.Forms.Button();
68 this.but_stop = new System.Windows.Forms.Button();
69 this.msc = new AxMSCommLib.AxMSComm();
70 this.lbl_info = new System.Windows.Forms.Label();
71 ((System.ComponentModel.ISupportInitialize)(this.msc)).BeginInit();
72 this.SuspendLayout();
73 //
74 // TB_paut
75 //
76 this.TB_paut.Location = new System.Drawing.Point(56, 32);
77 this.TB_paut.Name = "TB_paut";
78 this.TB_paut.Size = new System.Drawing.Size(176, 21);
79 this.TB_paut.TabIndex = 0;
80 this.TB_paut.Text = "19200,E,8,1";
81 //
82 // TB_cardnum
83 //
84 this.TB_cardnum.Location = new System.Drawing.Point(8, 240);
85 this.TB_cardnum.Name = "TB_cardnum";
86 this.TB_cardnum.Size = new System.Drawing.Size(392, 21);
87 this.TB_cardnum.TabIndex = 1;
88 this.TB_cardnum.Text = "";
89 //
90 // but_open
91 //
92 this.but_open.Location = new System.Drawing.Point(288, 24);
93 this.but_open.Name = "but_open";
94 this.but_open.TabIndex = 2;
95 this.but_open.Text = "open";
96 this.but_open.Click += new System.EventHandler(this.but_open_Click);
97 //
98 // but_exit
99 //
100 this.but_exit.Location = new System.Drawing.Point(288, 64);
101 this.but_exit.Name = "but_exit";
102 this.but_exit.TabIndex = 2;
103 this.but_exit.Text = "exit";
104 this.but_exit.Click += new System.EventHandler(this.but_exit_Click);
105 //
106 // but_read
107 //
108 this.but_read.Location = new System.Drawing.Point(48, 160);
109 this.but_read.Name = "but_read";
110 this.but_read.TabIndex = 2;
111 this.but_read.Text = "read";
112 this.but_read.Click += new System.EventHandler(this.but_read_Click);
113 //
114 // but_stop
115 //
116 this.but_stop.Location = new System.Drawing.Point(176, 160);
117 this.but_stop.Name = "but_stop";
118 this.but_stop.TabIndex = 2;
119 this.but_stop.Text = "stop";
120 this.but_stop.Click += new System.EventHandler(this.but_stop_Click);
121 //
122 // msc
123 //
124 this.msc.Enabled = true;
125 this.msc.Location = new System.Drawing.Point(304, 176);
126 this.msc.Name = "msc";
127 this.msc.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("msc.OcxState")));
128 this.msc.Size = new System.Drawing.Size(38, 38);
129 this.msc.TabIndex = 3;
130 //
131 // lbl_info
132 //
133 this.lbl_info.Location = new System.Drawing.Point(32, 288);
134 this.lbl_info.Name = "lbl_info";
135 this.lbl_info.Size = new System.Drawing.Size(216, 23);
136 this.lbl_info.TabIndex = 4;
137 //
138 // Form1
139 //
140 this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
141 this.ClientSize = new System.Drawing.Size(408, 333);
142 this.Controls.Add(this.lbl_info);
143 this.Controls.Add(this.msc);
144 this.Controls.Add(this.but_open);
145 this.Controls.Add(this.TB_cardnum);
146 this.Controls.Add(this.TB_paut);
147 this.Controls.Add(this.but_exit);
148 this.Controls.Add(this.but_read);
149 this.Controls.Add(this.but_stop);
150 this.Name = "Form1";
151 this.Text = "Form1";
152 this.Load += new System.EventHandler(this.Form1_Load);
153 ((System.ComponentModel.ISupportInitialize)(this.msc)).EndInit();
154 this.ResumeLayout(false);
155
156 }
157 #endregion
158
159 ///
160 /// 应用程序的主入口点。
161 ///

162 [STAThread]
163 static void Main()
164 {
165 Application.Run(new Form1());
166 }
167
168 private void but_open_Click(object sender, System.EventArgs e)
169 {
170 try
171 {
172
173 msc.CommPort = 1;
174 msc.Settings=this.TB_paut.Text.Trim();
175 msc.InputLen=0;
176 msc.RThreshold=1;
177 msc.InputMode=MSCommLib.InputModeConstants.comInputModeBinary;
178 if(msc.PortOpen==false)
179 {
180 msc.PortOpen=true;
181 this.lbl_info.Text="端口打开成功!";
182 }
183
184 }
185 catch(System.Exception oe)
186 {
187 MessageBox.Show(oe.Message);
188 }
189
190 }
191
192 private void but_exit_Click(object sender, System.EventArgs e)
193 {
194
195 this.Close();
196 }
197
198 private void but_read_Click(object sender, System.EventArgs e)
199 {
200 this.atimer.Enabled=true;
201 this.atimer.Interval=700;
202
203 }
204
205 private void but_stop_Click(object sender, System.EventArgs e)
206 {
207 this.atimer.Stop();
208 msc.PortOpen=false;
209 this.lbl_info.Text="停止读卡,端口关闭!";
210 }
211
212 private void Form1_Load(object sender, System.EventArgs e)
213 {
214 this.atimer.Elapsed +=new ElapsedEventHandler(atimer_Elapsed);
215 }
216
217 private void atimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
218 {
219 byte[] DU=new byte[7];
220
221 DU[0] =0x09;
222
223 DU[1] =0x41;
224 DU[2] =0x31;
225 DU[3] =0x46;
226
227 DU[4] =0x33;
228 DU[5] =0x46;
229 DU[6] =0x0d;
230
231 msc.InBufferCount = 0;
232 msc.OutBufferCount = 0;
233
234 msc.Output=DU;
235
236 Thread.Sleep(200);
237 string strin="";
238 byte[] byin;
239 object obin;
240 obin=msc.Input;
241 byin=(byte[])obin;
242 for(int i=0;i<=byin.Length-1;i++)
243 strin+=" "+byin[i].ToString("x")+"/r/n";
244 Thread.Sleep(200);
245 this.TB_cardnum.Text=strin;
246 Thread.Sleep(200);
247
248 }
249 }
250 }
251

本文转自
http://www.cnblogs.com/randylee/articles/356114.html
原创粉丝点击