实例介绍
【实例简介】
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using cn.zhm.common;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
System.Timers.Timer t;
public Form1()
{
InitializeComponent();
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}
[DllImport("Sdtapi.dll")]
private static extern int InitComm(int iPort);
[DllImport("Sdtapi.dll")]
private static extern int Authenticate();
[DllImport("Sdtapi.dll")]
private static extern int ReadBaseInfos(StringBuilder Name, StringBuilder Gender, StringBuilder Folk,
StringBuilder BirthDay, StringBuilder Code, StringBuilder Address,
StringBuilder Agency, StringBuilder ExpireStart, StringBuilder ExpireEnd);
[DllImport("Sdtapi.dll")]
private static extern int CloseComm();
[DllImport("Sdtapi.dll")]
private static extern int ReadBaseMsg(byte[] pMsg, ref int len);
[DllImport("Sdtapi.dll")]
private static extern int ReadBaseMsgW(byte[] pMsg, ref int len);
[DllImport("Sdtapi.dll")]
private static extern int ReadIINSNDN(byte[] pMsg);
[DllImport("Sdtapi.dll")]
private static extern int Routon_ReadIINSNDN(byte[] pMsg);
bool flag = true;
private void button1_Click(object sender, EventArgs e)
{
StringBuilder Name = new StringBuilder(31);
StringBuilder Gender = new StringBuilder(3);
StringBuilder Folk = new StringBuilder(10);
StringBuilder BirthDay = new StringBuilder(9);
StringBuilder Code = new StringBuilder(19);
StringBuilder Address = new StringBuilder(71);
StringBuilder Agency = new StringBuilder(31);
StringBuilder ExpireStart = new StringBuilder(9);
StringBuilder ExpireEnd = new StringBuilder(9);
byte[] pMsg = new byte[256];
int len = 0;
string[] temp;
string[] baseinfo = new string[9];
char[] param = { '\0' };
//ConfigClass config = new ConfigClass();
string[] arr= ConfigClass.Readconfig("config").Split('|');
int COM = string.IsNullOrEmpty(arr[0]) ? 1001 : int.Parse(arr[0]);
if (arr.Length > 1)
{
label1.Text = arr[1] "欢迎您!";
}
//打开端口
int intOpenRet = InitComm(COM);
if (intOpenRet != 1)
{
if (flag)
{
MessageBox.Show("阅读机具未连接");
}
flag = false;
return;
}
flag = true;
////卡认证
int intReadRet = Authenticate();
//if (intReadRet != 1)
//{
// //MessageBox.Show("卡认证失败");
// CloseComm();
// return;
//}
//三种方式读取基本信息
//ReadBaseInfos(推荐使用)
int intReadBaseInfosRet = ReadBaseInfos(Name, Gender, Folk, BirthDay, Code, Address, Agency, ExpireStart, ExpireEnd);
if (intReadBaseInfosRet != 1)
{
//MessageBox.Show("读卡失败");
int intCloseRet = CloseComm();
return;
}
label4.Text = Code.ToString();
label6.Text = Name.ToString();
label8.Text = Gender.ToString();
label10.Text = Folk.ToString();
label12.Text = BirthDay.ToString();
label14.Text = Address.ToString();
label16.Text = Agency.ToString();
label18.Text = ExpireStart.ToString();
label20.Text = ExpireEnd.ToString();
pictureBox1.Load("photo.bmp");
pictureBox1.Refresh();
byte[] pMsg1 = new byte[256];
// int aa = ReadIINSNDN(pMsg1);
// //int aa = ReadIINSNDN(pMsg1);
////ReadBaseMsg(不推荐使用)
// //intReadBaseInfosRet = ReadBaseMsg(pMsg, ref len);
// //if (intReadBaseInfosRet != 1)
// if (aa != 1)
// {
// MessageBox.Show("读卡失败");
// CloseComm();
// return;
// }
//temp = System.Text.Encoding.Default.GetString(pMsg1).Split(param);
//listBox1.Items.Add(temp);
//listBox1.Items.Add("-------------");
// int i = 0;
// foreach (string s in temp)
// {
// if (s.Length == 0)
// continue;
// baseinfo[i ] = s;
// }
// listBox1.Items.Add(temp.ToString());
// listBox1.Items.Add(baseinfo[0]);
/* label4.Text = baseinfo[5];
label6.Text = baseinfo[0];
label8.Text = baseinfo[1];
label10.Text = baseinfo[2];
label12.Text = baseinfo[3];
label14.Text = baseinfo[4];
label16.Text = baseinfo[6];
label18.Text = baseinfo[7];
label20.Text = baseinfo[8];
pictureBox1.Load("photo.bmp");
*/
//ReadBaseMsgW
//intReadBaseInfosRet = ReadBaseMsgW(pMsg, ref len);
//if (intReadBaseInfosRet != 1)
//{
// MessageBox.Show("读卡失败");
// CloseComm();
// return;
//}
//string temp1 = System.Text.Encoding.Unicode.GetString(pMsg);
//listBox1.Items.Add(temp1);
//listBox1.Items.Add("-------------");
/* label4.Text = temp1.Substring(61, 18);
label6.Text = temp1.Substring(0, 15);
label8.Text = temp1.Substring(15, 1);
label10.Text = temp1.Substring(16, 2);
label12.Text = temp1.Substring(18, 8);
label14.Text = temp1.Substring(26, 35);
label16.Text = temp1.Substring(79, 15);
label18.Text = temp1.Substring(94, 8);
label20.Text = temp1.Substring(102, 8);
pictureBox1.Load("photo.bmp");
*/
//关闭端口
int intCloseRet1 = CloseComm();
}
private void button2_Click(object sender, EventArgs e)
{
Form11 objfrm = new Form11();
objfrm.Show();
}
private void button3_Click(object sender, EventArgs e)
{
Form3 objfrm1 = new Form3();
objfrm1.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
button1.Visible = false;
button2.Visible = false;
//button3.Visible = false;
//ConfigClass config = new ConfigClass();
string[] arr = ConfigClass.Readconfig("config").Split('|');
if (arr.Length > 1)
{
label1.Text = arr[1] "欢迎您!";
}
t = new System.Timers.Timer(5000);
t.Elapsed = new System.Timers.ElapsedEventHandler(theout1);//到达时间的时候执行事件;
t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
//如果不写下面这句会有一个异常。
//异常:线程间操作无效: 从不是创建控件"richtextbox"的线程访问它
//但这不是最好的方法。如果只有一个进程调用richtextbox而已。就可以用下面这句
//如果有多个线程调用richtextbox等控件。就要用委托。见thread第二种方法
Control.CheckForIllegalCrossThreadCalls = false;
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = Application.StartupPath "//aa.wav";
player.LoadAsync();
player.PlaySync();
timer1.Enabled = true;//开始滚动
}
private void theout1(object sender, EventArgs e)
{
button1_Click(sender, e);
}
private void theout(object sender, EventArgs e)
{
try
{
StringBuilder Name = new StringBuilder(31);
StringBuilder Gender = new StringBuilder(3);
StringBuilder Folk = new StringBuilder(10);
StringBuilder BirthDay = new StringBuilder(9);
StringBuilder Code = new StringBuilder(19);
StringBuilder Address = new StringBuilder(71);
StringBuilder Agency = new StringBuilder(31);
StringBuilder ExpireStart = new StringBuilder(9);
StringBuilder ExpireEnd = new StringBuilder(9);
byte[] pMsg = new byte[256];
int len = 0;
string[] temp;
string[] baseinfo = new string[9];
char[] param = { '\0' };
//打开端口
int intOpenRet = InitComm(1001);
if (intOpenRet != 1)
{
MessageBox.Show("阅读机具未连接");
return;
}
//卡认证
int intReadRet = Authenticate();
if (intReadRet != 1)
{
//MessageBox.Show("卡认证失败");
CloseComm();
return;
}
//三种方式读取基本信息
//ReadBaseInfos(推荐使用)
int intReadBaseInfosRet = ReadBaseInfos(Name, Gender, Folk, BirthDay, Code, Address, Agency, ExpireStart, ExpireEnd);
if (intReadBaseInfosRet != 1)
{
//MessageBox.Show("读卡失败");
CloseComm();
return;
}
label4.Text = Code.ToString();
label6.Text = Name.ToString();
label8.Text = Gender.ToString();
label10.Text = Folk.ToString();
label12.Text = BirthDay.ToString();
label14.Text = Address.ToString();
label16.Text = Agency.ToString();
label18.Text = ExpireStart.ToString();
label20.Text = ExpireEnd.ToString();
pictureBox1.Load("photo.bmp");
pictureBox1.Refresh();
byte[] pMsg1 = new byte[256];
int aa = ReadIINSNDN(pMsg1);
//int aa = ReadIINSNDN(pMsg1);
//ReadBaseMsg(不推荐使用)
//intReadBaseInfosRet = ReadBaseMsg(pMsg, ref len);
//if (intReadBaseInfosRet != 1)
if (aa != 1)
{
//MessageBox.Show("读卡失败");
CloseComm();
return;
}
temp = System.Text.Encoding.Default.GetString(pMsg1).Split(param);
listBox1.Items.Add(temp);
listBox1.Items.Add("-------------");
int i = 0;
foreach (string s in temp)
{
if (s.Length == 0)
continue;
baseinfo[i ] = s;
}
listBox1.Items.Add(temp.ToString());
listBox1.Items.Add(baseinfo[0]);
//关闭端口
//int intCloseRet = CloseComm();
}
catch (Exception ex)
{
int intCloseRet = CloseComm();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.Left -= 2;//设置label1左边缘与其容器的工作区左边缘之间的距离
if (label1.Right < 0)//当label1右边缘与其容器的工作区左边缘之间的距离小于0时
{
label1.Left = this.Width;//设置label1左边缘与其容器的工作区左边缘之间的距离为该窗体的宽度
}
}
}
}
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论