潘小東 王福明
(中北大學 太原 030051)
以往的上、下位機之間的通信方式大都采用串口通訊[1]方式,串口通信是“點對點”連接,如果是“一機多設備”共同工作,那么還需要多線程的通信協(xié)議編程;而且串口通信的數(shù)據(jù)傳輸速率會受到距離的客觀因素影響,傳輸效率不高。隨著IC 卡技術的日益成熟,IC 卡技術[2]在當今社會的各個領域發(fā)揮著舉足輕重的作用;以其攜帶方便、保密性好、存儲量大的特性深受廣大消費者青睞。因此,為了提高工作的科學化和規(guī)范化,運用IC 卡技術開發(fā)此系統(tǒng)是非常有必要的。
該系統(tǒng)專門為澳瑞特健康產(chǎn)業(yè)股份有限公司所生產(chǎn)的康復器械所設計。主要的功能如下圖1 所示。
圖1 康復器械主要的功能
該系統(tǒng)所用的接口函數(shù)皆封裝在32 位DLL(Mwic_32.dll)中,IC 卡為德國西門子生產(chǎn)的SLE4442 卡[3],支持Windows xp/Vista/7 系統(tǒng)。可在VB、VC、DELPHI 等32 位開發(fā)環(huán)境中使用。
(1)int chk_4442(int icdev)
icdev:通用設備標示符,若返回值<0,錯誤;返回值=0,正確(下同);
功能:檢測卡片是否存在;
舉例:st=chk_4442(icdev);
(2)int csc_4442(int icdev ,int len ,unsigned char *databuff)
功能:核對卡片密碼;
舉例:unsigned char databuff[3]={0xFF,0xFF,0xFF}//假定密碼為出廠密碼:FFFFFF;
st=csc_4442(icdev,3,databuff);
(3)int asc_hex(unsigned char * asc ,unsigned char *hex ,unsigned long length)
功能:ASCII 碼轉(zhuǎn)換成十六進制數(shù)據(jù);
舉例:unsigned char databuff[6];
st=asc_hex(“a1a2a3a4a5”,databuff,5);
(4)int asc_hex(unsigned char * hex ,unsigned char *asc ,unsigned long length)
功能:十六進制轉(zhuǎn)換成ASCII 碼;
舉例:unsigned char databuff1[5]={0xa1,0xa2,0xa3,0xa4,0xa5};
unsigned char databuff2[10];
st=hex_asc(databuff1,databuff2,5);
* 注:轉(zhuǎn)換后的字符長度為2* length。
(5)int swr_4442(int icdev ,int offset ,int len ,unsigned cahr * databuff);
功能:向卡內(nèi)制定地址寫入數(shù)據(jù);
舉例:unsigned char databuff[5]={‘s’,’o’,’n’,’n’,’y’};
st=swr_4442(icdev,32,5,databuff);
(6)int srd_4442(int icdev ,int offset ,int len ,unsigned char * databuff);
功能:讀取卡內(nèi)制定地址的數(shù)據(jù);
舉例:unsigned char databuff[5];
st=srd_4442(icdev,32,5,databuff);
2.2.1 函數(shù)聲明[4]
Declare Function srd_4442 Lib "mwic_32.dll" (ByVal icdev As Long,ByVal offset As Integer,ByVal le As Integer,ByVal data_buffer)As Integer
Declare Function srd_4442_hex Lib "mwic_32.dll" Alias"srd_4442" (ByVal icdev As Long,ByVal offset As Integer,ByVal le As Integer,ByRef data_buff As Byte)As Integer
Declare Function swr_4442 Lib "mwic_32.dll"(ByVal icdev As Long,ByVal offset As Integer,ByVal le As Integer,ByVal data_buffer)As Integer
Declare Function swr_4442_hex Lib"mwic_32.dll" Alias"swr_4442" (ByVal icdev As Long,ByVal offset As Integer,ByVal le As Integer,ByRef data_buffer As Byte)As Integer
Declare Function chk_4442 Lib "mwic_32.dll" (ByVal icdev As Long)As Integer
Declare Function csc_4442 Lib "mwic_32.dll" (ByVal icdev As Long,ByVal le As Integer,ByRef data_buffer As Byte)As Integer
Declare Function asc_hex Lib "mwic_32.dll" (ByVal asc,ByRef hex As Byte,ByVal le&)As Integer
Declare Function hex_asc Lib "mwic_32.dll" (ByRef hex As Byte,ByVal asc,ByVal le&)As Integer
2.2.2 代碼節(jié)選
<1>數(shù)據(jù)存入數(shù)據(jù)庫[5](采用ADO 技術連接數(shù)據(jù)庫)
Dim DT As String
DT=CStr(Month(Date))+"/"+CStr(Day(Date))+ "/"+CStr(Year(Date))+" "+CStr(time())
ExeCutesql " insert into 客戶資料 values(CSTR(DATE())+ ' '+CSTR(TIME()),'" & Label2.Caption & "','" & Text1(1).Text & "','" & Text1(2).Text & "','" _
& Text1(3).Text & "','" & Text1(4).Text & "','" & Text1(5).Text & "','" & Text1(6).Text & "','" _
& Text1(7).Text & "','" & Text1(8).Text & "','" & Label3.Caption & "')",Str_text
<2>十六進制寫卡操作(包括對數(shù)據(jù)的處理)
就此系統(tǒng)而言,為了讓軟件往卡里寫入的數(shù)據(jù)能夠方便的由下位機成功讀取,我們必須對寫入的數(shù)據(jù)進行一些轉(zhuǎn)化,這里就要求數(shù)據(jù)轉(zhuǎn)換為十六進制。我們規(guī)定一個數(shù)據(jù)占卡中的5 個地址,同時還要為小數(shù)與整數(shù)的不同寫入形式(這里主要是輸入數(shù)據(jù)的指長度)做周全考慮!該系統(tǒng)主要用到的數(shù)據(jù)處理函數(shù)是Mid(string,start,leng),它的作用就是在字符串string 中從start 開始截取leng 長度的字符;具體用法參看如下代碼:
‘寫數(shù)據(jù)前必須核對卡密碼,否則不能寫入數(shù)據(jù)到卡中
st=asc_hex("ffffff",databuff(6),3)
st=csc_4442(icdev,3,databuff(6))
date1(1)=Trim(Text1(1).Text)
x(1)=Len(Trim(Text1(1).Text))
If InStr(1,date1(1),".")<>0 Then
Select Case x(1)
Case 4
date11(1)="00000"+Mid(date1(1),1,1)+ "0"+ Mid(date1(1),2,1)+ "0"+Mid(date1(1),4,1)’將數(shù)據(jù)組合為長度為10 的字符串形式以便于后面轉(zhuǎn)換
st=asc_hex(date11(1),datebuff1(1),5)
st=swr_4442_hex(icdev,100,5,datebuff1(1))
Case 3
date11(1)="0000000"+Mid(date1(1),1,1)+ "0"+Mid(date1(1),3,1)
st=asc_hex(date11(1),datebuff1(1),5)
st=swr_4442_hex(icdev,100,5,datebuff1(1))
End Select
Else
Select Case x(1)
Case 1
date11(1)="0000000"+Mid(date1(1),1,1)+ "0"+"0"
st=asc_hex(date11(1),datebuff1(1),5)
st=swr_4442_hex(icdev,100,5,datebuff1(1))
Case 2
date11(1)="00000"+Mid(date1(1),1,1)+ "0"+ Mid(date1(1),2,1)+ "00"
st=asc_hex(date11(1),datebuff1(1),5)
st=swr_4442_hex(icdev,100,5,datebuff1(1))
End Select
End If
………
<3>十六進制讀卡操作
每次鍛煉結束后,需要將本次的鍛煉數(shù)據(jù)在該軟件上顯示出來以便于對客戶的身體狀況做出一個客觀的評價,讀卡操作理所當然的就成為該系統(tǒng)的一個重要環(huán)節(jié)。而下位機寫數(shù)據(jù)到卡中都是以十六進制形式寫入,因此,此軟件系統(tǒng)讀取卡中數(shù)據(jù)后也要進行數(shù)制轉(zhuǎn)換截取等處理再在窗體上顯示出來,此處用到一個Val(string)函數(shù),它返回一個包含于字符串string 內(nèi)的數(shù)字,更直白一點的解釋就是過濾字符,輸出數(shù)字,具體的應用參看代碼如下:
st=srd_4442_hex(icdev,55,5,databuff(2))
st=hex_asc(databuff(2),data61,5)
data611=Val(data61)
If data611=0 Then
Label2(3).Caption="0"
End If
x4=Len(data411)
If x4=9Then
data6111=Mid(data611,1,1)+Mid(data611,3,1)+ Mid(data611,5,1)+Mid(data611,7,1)+Mid(data611,9,1)
Else
If x4=7 Then
data6111=Mid(data611,1,1)+Mid(data611,3,1)+ Mid(data611,5,1)+ Mid(data611,7,1)
Else
If x4=5 Then
data6111=Mid(data611,1,1)+Mid(data611,3,1)+ Mid(data611,5,1)
Else
If x4=3 Then
data6111=Mid(data611,1,1)+ Mid(data611,3,1)
Else
If x4=1 Then
data6111=Mid(data611,1,1)
End If
End If
End If
End If
End If
………
<4>三次參數(shù)比較[6]
此系統(tǒng)中運用MSChart 控件來作為參數(shù)比較的工具,MSChart 控件是以圖形方式顯示數(shù)據(jù)的圖表,可以按照一定的規(guī)范將數(shù)據(jù)以圖表的形式顯示出來。可以通過在控件屬性頁中設置數(shù)據(jù)來創(chuàng)建圖表,或者可以從其他數(shù)據(jù)源比如Excel 表格、數(shù)據(jù)庫中檢索并繪制數(shù)據(jù)。
‘運用VB 自帶的MSChart 控件,非常直觀的顯示出來
Dim arrValues(1 To 6,1 To 3)
Dim a As Integer
For a=1 To 6
arrValues(a,1)=Label2(a-1).Caption
arrValues(a,2)=Label3(a-1).Caption
arrValues(a,3)=Label4(a-1).Caption
Next a
MSChart1.ChartData=arrValues
<5>打印鍛煉參數(shù)[7](用于醫(yī)學分析)
‘通過VB 操作Excel 將鍛煉參數(shù)以表格形式打印
Dim XlApp As New excel.Application’Excel 對象
Dim xlBook As New excel.Workbook’工作簿
Dim xlSheet As New excel.Worksheet’工作表
XlApp.Visible=True’設置Excel 對象可見
Set xlBook=XlApp.Workbooks.Add’建立新的工作簿
Set xlSheet=xlBook.Worksheets(1)’設置活動工作表
xlSheet.Select
excel
Sub excel()
Dim j As Integer
Cells(1,1)="載荷"
Cells(1,2)="功率"
Cells(1,3)="時間"
Cells(1,4)="次數(shù)"
Cells(1,5)="心率"
Cells(1,6)="熱量"
For j=2 To i+1
Cells(j,1)=a1(i)
Cells(j,2)=b1(i)
Cells(j,3)=c1(i)
Cells(j,4)=d1(i)
Cells(j,5)=e1(i)
Cells(j,6)=f1(i)
Next j
End Sub
<6>窗體的美化處理
為了讓軟件系統(tǒng)界面更美觀,我們在啟動界面以及按鈕控件上可以加載.bmp 格式的圖片,效果如圖2:
圖2
以上代碼摘自ORIENT 鍛煉管理系統(tǒng),系統(tǒng)投入使用后,運行穩(wěn)定正常。
該軟件系統(tǒng)已被正式運用,它改變了以往軟件系統(tǒng)的不足,經(jīng)過測試,本系統(tǒng)功能較齊全、穩(wěn)定可靠,并且能運用到該廠的其他類型的各種設備(氣動設備、無線設備等等),具有普遍適用性。操作方便,提高了康復鍛煉的效率,從而更好的服務于廣大客戶。
[1]張玉強,白少民.串口通信編程分析及其VB 編程實例[J].延邊大學學報:自然科學版,2003,22(3).
[2]江斌.淺談IC 卡技術與應用[R].第七屆全國建設事業(yè)IC 卡應用和技術發(fā)展研討會,2008.
[3]馮馳,徐旭東.基于SLE4442 卡的身份認證系統(tǒng)[J].自動化技術與應用,2004,23(4).
[4]黃悅.SLE4442 智能卡VB 接口程序設計[J].電腦編程技巧與維護,2003(7).
[5]廖金輝,向建國,唐日成.VB 中基于自動化功能的Access 數(shù)據(jù)庫應用[J].計算機與現(xiàn)代化,2007(8).
[6]李旭東.VB 環(huán)境下MSChart 控件的應用與研究[J].內(nèi)江科技,2008,29(9).
[7]黃世芹,王珺.VB 編程環(huán)境下如何調(diào)用和生成EXCEL 報表文檔[J].貴州氣象,2010,34(2).