首先的引用RC500_232.dll和zlg500B.dll这两个dll功能是一样,只是zlg500B.dll的功能有多一些!
如果是应用程序,将这两个dll放到bin文件夹中,如果是asp.net 程序 则将dll 放到系统盘中的system32文件夹中
(using System.Runtime.InteropServices; 一定要添加这个命名空间!!!!!!)
添加API申明如下:
[DllImp
private static extern byte RC500_232_init(int mport, int mbaud);
[DllImp
private static extern byte RC500_232_config();
[DllImp
private static extern byte RC500_232_exit();
[DllImp
private static extern byte RC500_232_request(byte mmode, ref UInt16 mtagtype);
[DllImp
private static extern byte RC500_232_anticoll(byte mbcnt, ref UInt32 msnr);
[DllImp
private static extern byte RC500_232_select(UInt32 msnr, ref byte msize);
[DllImp
private static extern byte RC500_232_authkey(byte mmode, byte msecnr, byte[] mkey);
[DllImp
private static extern byte RC500_232_read(byte maddr, byte[] mdata);
[DllImp
private static extern byte RC500_232_write(byte maddr, byte[] mdata);
[DllImp
private static extern byte RC500_232_readval(byte maddr, ref Int32 value);
[DllImp
private static extern byte RC500_232_writeval(byte maddr, Int32 value);
[DllImp
private static extern byte zlg500B_increment(byte maddr, ref Int32 value);
[DllImp
private static extern byte zlg500B_decrement(byte maddr, ref Int32 value);
具体功能看代码:(这些函数都有说明的,自己看下吧!)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Da
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="mport">端口</param>
/// <param name="mbaud">平率</param>
/// <returns></returns>
[DllImp
private static extern byte RC500_232_init(int mport, int mbaud);
[DllImp
private static extern byte RC500_232_config();
[DllImp
private static extern byte RC500_232_exit();
[DllImp
private static extern byte RC500_232_request(byte mmode, ref UInt16 mtagtype);
[DllImp
private static extern byte RC500_232_anticoll(byte mbcnt, ref UInt32 msnr);
[DllImp
private static extern byte RC500_232_select(UInt32 msnr, ref byte msize);
[DllImp
private static extern byte RC500_232_authkey(byte mmode, byte msecnr, byte[] mkey);
[DllImp
private static extern byte RC500_232_read(byte maddr, byte[] mdata);
[DllImp
private static extern byte RC500_232_write(byte maddr, byte[] mdata);
[DllImp
private static extern byte RC500_232_readval(byte maddr, ref Int32 value);
[DllImp
private static extern byte RC500_232_writeval(byte maddr, Int32 value);
[DllImp
private static extern byte zlg500B_increment(byte maddr, ref Int32 value);
[DllImp
private static extern byte zlg500B_decrement(byte maddr, ref Int32 value);
ushort tagtype;
UInt32 snr;
byte size;
byte[] key = new byte[6] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
byte[] da
byte byteDataArea; //选择的数据块
byte byteSQ; //选择扇区
private void Form1_Load(object sender, EventArgs e)
{
}
private void toolStripLabel1_Click(object sender, EventArgs e)
{
}
private void but_connect_m_Click(object sender, EventArgs e)
{
if (RC500_232_init(Convert.ToInt32(box_com.Text.Replace("com","")), Convert.ToInt32(box_hz.Text)) != 0)
MessageBox.Show("端口打开失败!");
else if (RC500_232_config() != 0)
MessageBox.Show("初始化失败!");
else txt_log.Text="端口打开成功,读卡器初始化成功!\r\n";http://www.my400800.cn
}
private void but_close_m_Click(object sender, EventArgs e)
{
long i=1;
int j = 0;
while(i!=0){
i = RC500_232_exit();
j++;
if (j > 10)
break;
}
Log("关闭串口成功");
}
public void Log(string mess) {
txt_log.Text += mess+"\r\n";
}
private void but_exti_m_Click(object sender, EventArgs e)
{
but_close_m_Click(null,null);
Application.Exit();
}
Regex rgKey = new Regex(" ");
private void but_readData_Click(object sender, EventArgs e)
{
if (Init())
{
if (RC500_232_read(byteDataArea, da
{
MessageBox.Show("数据读取失败");
}
else
{
txt_co
Log("数据读取成功: " + ReadData(da
}
}
}
public string ReadData(byte[] da
{
string tempt = "";
foreach (byte r in da
{
tempt += Convert.ToString(r,16)+ " ";
}
return tempt;
}
private void but_readValue_Click(object sender, EventArgs e)
{
if (Init())
{
Int32 intValue = 0x500;
if (RC500_232_readval(byteDataArea, ref intValue) != 0)// 将0扇区数据读入byte类型数组da
{
MessageBox.Show("数值读取失败");
}
else
{
txt_co
Log("数值读取成功: " +Convert.ToString(intValue,16));
}
}
}
private void box_SQ_SelectedIndexChanged(object sender, EventArgs e)
{
int intSQ = Convert.ToInt32(box_SQ.Text);
int temp = intSQ * 4;
box_dataArea.Items.Clear();
box_dataArea.Items.Add(temp);
box_dataArea.Items.Add(temp+1);
box_dataArea.Items.Add(temp+2);
box_dataArea.Items.Add(temp+3);
box_dataArea.SelectedIndex = 0;
box_dataArea2.Items.Clear();
if (intSQ != 0)
{
box_dataArea2.Items.Add(temp);
}
box_dataArea2.Items.Add(temp + 1);
box_dataArea2.Items.Add(temp + 2);
box_dataArea2.Items.Add(temp + 3);
box_dataArea2.SelectedIndex = 0;
}
private void but_writeDate_Click(object sender, EventArgs e)
{
string value = txt_writeDate.Text.Trim();
if (!string.IsNullOrEmpty(value))
{
int index = 0;
foreach (string pt in rgKey.Split(value))
{
if (string.IsNullOrEmpty(pt))
{
MessageBox.Show("写入数据时有多余的空格!"); return;
}
int intValue = 0;
try
{
intValue = Convert.ToInt32(pt, 16);
}
catch (Exception)
{
MessageBox.Show("写入数据含有非法字符!");
return;
}
if (intValue > 255 || intValue < 0)
{
MessageBox.Show("写入数据范围错误(00-ff)!");
return;
}
byte bt = (byte)intValue;
da
index++;
}
if (index != 16)
{
MessageBox.Show("写入数据错误,长度不为16个!"); return;
}
if (Init())
{
if (RC500_232_write(byteDataArea, da
{
MessageBox.Show("数据写入失败");
}
else
{
txt_co
Log("数据写入成功: " + ReadData(da
}
}
}
else {
MessageBox.Show("写入数据不能为空!");
}
}
public bool Init()
{
byteDataArea = (byte)Convert.ToInt32(box_dataArea.Text); //选择的数据块
byteSQ = (byte)Convert.ToInt32(box_SQ.Text); //选择扇区
string strKey = txt_key.Text;
if (!string.IsNullOrEmpty(strKey))
{
int index = 0;
foreach (string pt in rgKey.Split(strKey))
{
byte bt = (byte)Convert.ToInt32(pt, 16);
key[index] = bt;
index++;
}
}
else
{
key = new byte[6] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
}
bool flag = false;
if (RC500_232_request(0, ref tagtype) != 0) //检查有效范围是否有卡
{
if (RC500_232_request(0, ref tagtype) != 0) //检查有效范围是否有卡
{
MessageBox.Show("没有卡!"); return false;
}
else
flag = true;
}
else
flag = true;
if (flag)
{
if (RC500_232_anticoll(0, ref snr) != 0) //防碰撞控制,snr返回卡的序列号
{
MessageBox.Show("防碰撞错误!"); return false;
}
else if (RC500_232_select(snr, ref size) != 0) //选择某一序号的卡,//size返回卡的容量大小
{
MessageBox.Show("选择错误!"); return false;
}
else if (RC500_232_authkey((byte)box_keyMode.SelectedIndex, byteSQ, key) != 0) //密码验证,密码存放在 key数组中
{
MessageBox.Show("密码验证错误!"); return false;
}
}
return true;
}
private void but_writeValue_Click(object sender, EventArgs e)
{
if (Init())
{
string value = txt_writeValue.Text.Trim();
if(string.IsNullOrEmpty(value)){
MessageBox.Show("写入数值不能为空");
return;
}
Int32 intValue = Convert.ToInt32(value,16);
if (RC500_232_writeval(byteDataArea, intValue) != 0)// 将0扇区数据读入byte类型数组da
{
MessageBox.Show("数值写入失败");
}
else
{
txt_co
Log("数值写入成功: " + value);
}
}
}
private void but_add_Click(object sender, EventArgs e)
{
if (Init())
{
string value = txt_addValue.Text.Trim();
if(string.IsNullOrEmpty(value)){
MessageBox.Show("写入数值不能为空");
return;
}
Int32 temp = 1; //Convert.ToInt32(txt_addValue.Text.Trim(),16);
if (zlg500B_increment(byteDataArea, ref temp) != 0)// 将0扇区数据读入byte类型数组da
{
MessageBox.Show("数值写入失败");
}
else
{
txt_co
Log("数值写入成功: " + value);
}
}
}
private void but_decrement_Click(object sender, EventArgs e)
{
if (Init())
{
string value = txt_dec.Text.Trim();
if (string.IsNullOrEmpty(value))
{
MessageBox.Show("写入数值不能为空");
return;
}
Int32 temp = 1; //Convert.ToInt32(txt_addValue.Text.Trim(), 16);
if (zlg500B_decrement(byteDataArea, ref temp) != 0)// 将0扇区数据读入byte类型数组da
{
MessageBox.Show("数值写入失败");
}
else
{
txt_co
Log("数值写入成功: " + value);
}
}
}