資源簡介
完成了對明華URF-R330讀卡器 M1卡的讀寫,官方提供的Demo并沒有C#版本的,我四處收集資料改寫代碼寫出這個C#版的Demo,希望對您有所幫助.
因為時間問題所以并沒有優(yōu)化代碼,不過注釋絕對夠全的.

代碼片段和文件信息
using?System;
using?System.Text;
using?M1Card.Common;
using?System.Windows.Forms;
using?System.Runtime.InteropServices;
namespace?M1Card
{
????/*關于M1卡
?????*?M1卡有15個扇區(qū)可讀寫
?????*?每個扇區(qū)的0-2塊可讀寫第3塊存放控制位所以不可寫
?????*?每次寫一個扇區(qū)里的塊都要先驗證該扇區(qū)的密碼
?????*?寫入一次為一塊
?????*?不知為何寫滿一個塊時讀取的時候末尾會多兩三個亂碼猜測是把控制位給讀出來了時間問題未深究
?????*?總共有5760位可讀寫即720字節(jié)
?????*/
????public?partial?class?MainWindow?:?Form
????{
????????#region?變量
????????///?
????????///?設備ID
????????///?
????????private?int?_icdev?=?-1;
????????///?
????????///?當前IC卡號
????????///?
????????private?int?_cardId?=?0;
????????#endregion
????????public?MainWindow()
????????{
????????????InitializeComponent();
????????}
????????#region?設備操作
????????///?
????????///?寫入指定的字符串到M1卡
????????///?
????????///?要寫入的字符串
????????private?void?Write(string?str)
????????{
????????????try
????????????{
????????????????#region?過濾字符串
????????????????//如果字符串有非法字符
????????????????foreach?(var?c?in?str)
????????????????{
????????????????????if?(c?>?127)?throw?new?ArgumentException(“字符串內(nèi)含有ASCII已外的字符“);
????????????????}
????????????????//不可大于720個字符
????????????????if?(str.Length?>?720)?throw?new?ArgumentException(“字符串的長度已超過卡的最大容量720字節(jié)“);
????????????????#endregion
????????????????int?result;?????????????????????//設備API函數(shù)返回的結果
????????????????#region?初始化設備
????????????????//連接設備并設置波特率
????????????????_icdev?=?URF.rf_init(0?9600);
????????????????if?(_icdev?0)?throw?new?URFException(_icdev);
????????????????//尋卡
????????????????result?=?URF.rf_card((Int16)_icdev?1?ref?_cardId);
????????????????if?(result?!=?0)?throw?new?URFException(result);
????????????????//加載密碼
????????????????result?=?URF.rf_load_key_hex(_icdev?0?0?“FFFFFFFFFFFF“);
????????????????if?(result?!=?0)?throw?new?URFException(result);
????????????????#endregion
????????????????#region?寫入卡內(nèi)
????????????????int?writedBlock?=?0;????????????//標示變量標識已寫入的塊數(shù)
????????????????string[]?data;??????????????????//要寫入卡內(nèi)的字符串數(shù)據(jù)
????????????????var?block?=?str.Length?/?16;????//字符串所占的塊
????????????????var?mod?=?str.Length?%?16;
????????????????if?(mod?!=?0)?block++;??????????//如果有字符占不滿一個塊就多加一個
????????????????var?sector?=?block?/?3;?????????//字符串所占的扇區(qū)
????????????????if?(block?%?3?!=?0)?sector++;???//如果塊占不滿一個扇區(qū)多加一個扇區(qū)
????????????????data?=?new?string[block];
????????????????#region?拆分字符串為16字節(jié)一塊
????????????????for?(int?index?=?0;?index?????????????????{
????????????????????string?tempData;
????????????????????if?(index?==?block?-?1?&&?mod?!=?0)
????????????????????{
????????????????????????tempData?=?str;
????????????????????????str?=?null;
????????????????????}
????????????????????else
????????????????????{
????????????????????????tempData?=?str.Substring(0?16);
????????????????????????str?=?str.Remove(index?16);
????????????????????}
????????????????????data[index]?=?tempD
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????167??2013-07-02?22:51??M1Card\App.config
?????文件????????167??2013-07-02?22:51??M1Card\bin\64\WindowsFormsApplication1.exe.config
?????文件??????22472??2013-07-02?23:02??M1Card\bin\64\WindowsFormsApplication1.vshost.exe
?????文件????????167??2013-07-02?22:51??M1Card\bin\64\WindowsFormsApplication1.vshost.exe.config
?????文件?????114688??2013-07-02?20:28??M1Card\bin\Debug\mwrf32.dll
?????文件??????14336??2013-07-16?23:12??M1Card\bin\Debug\WindowsFormsApplication1.exe
?????文件????????167??2013-07-02?22:51??M1Card\bin\Debug\WindowsFormsApplication1.exe.config
?????文件??????30208??2013-07-16?23:12??M1Card\bin\Debug\WindowsFormsApplication1.pdb
?????文件??????22984??2013-07-16?23:12??M1Card\bin\Debug\WindowsFormsApplication1.vshost.exe
?????文件????????167??2013-07-02?22:51??M1Card\bin\Debug\WindowsFormsApplication1.vshost.exe.config
?????文件?????114688??2013-07-02?20:28??M1Card\bin\Release\mwrf32.dll
?????文件??????11776??2013-07-14?19:53??M1Card\bin\Release\WindowsFormsApplication1.exe
?????文件????????167??2013-07-02?22:51??M1Card\bin\Release\WindowsFormsApplication1.exe.config
?????文件??????30208??2013-07-14?19:53??M1Card\bin\Release\WindowsFormsApplication1.pdb
?????文件??????22472??2013-07-14?19:55??M1Card\bin\Release\WindowsFormsApplication1.vshost.exe
?????文件????????167??2013-07-02?22:51??M1Card\bin\Release\WindowsFormsApplication1.vshost.exe.config
?????文件????????490??2012-06-06?02:06??M1Card\bin\Release\WindowsFormsApplication1.vshost.exe.manifest
?????文件???????2580??2013-07-16?23:09??M1Card\Common\ErrorManager.cs
?????文件???????3970??2013-07-16?23:09??M1Card\Common\URF.cs
?????文件???????5904??2013-07-16?23:09??M1Card\M1Card.csproj
?????文件????????522??2013-07-02?23:35??M1Card\M1Card.csproj.user
?????文件???????9046??2013-07-16?23:12??M1Card\MainWindow.cs
?????文件???????4842??2013-07-16?23:07??M1Card\MainWindow.Designer.cs
?????文件???????5817??2013-07-16?13:59??M1Card\MainWindow.resx
?????文件???????7000??2013-07-02?23:02??M1Card\obj\64\DesignTimeResolveAssemblyReferencesInput.cache
?????文件???????4608??2013-07-02?23:02??M1Card\obj\64\TempPE\Properties.Resources.Designer.cs.dll
?????文件????????120??2013-07-02?23:02??M1Card\obj\64\WindowsFormsApplication1.csproj.FileListAbsolute.txt
?????文件???????1903??2013-07-02?23:29??M1Card\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????6825??2013-07-16?23:08??M1Card\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件????????591??2013-07-16?23:12??M1Card\obj\Debug\M1Card.csproj.FileListAbsolute.txt
............此處省略52個文件信息
- 上一篇:C#透明窗體畫圖電子尺
- 下一篇:C#WEB上傳控件多附件上傳
評論
共有 條評論