VB写的串口和单片机读卡程序 有需要的下
所有资料51hei提供下载:
自己写的通过串口和单片机通讯的ic卡读卡程序.rar
(55.17 KB, 下载次数: 73)
vb源程序如下:
- Attribute VB_Name = "IC卡管理自动记录系统"
- Attribute VB_GlobalNameSpace = False
- Attribute VB_Creatable = False
- Attribute VB_PredeclaredId = True
- Attribute VB_Exposed = False
- Option Explicit
- Public inData As String '串口中断时读入的字符串
- Dim TEXT_OK As Boolean
- Dim text1_temp As String
- Dim t7 As String
- Private Sub Command2_Click()
- data1.Recordset.MovePrevious
- If data1.Recordset.BOF Then data1.Recordset.MoveFirst
- Text1.Text = data1.Recordset("卡号")
- Text2.Text = data1.Recordset("姓名")
- Text3.Text = data1.Recordset("班级")
- Text4.Text = data1.Recordset("余额")
- Text5.Text = data1.Recordset("日期")
- Text6.Text = data1.Recordset("时间")
- Text9.Text = Format(CStr(CStr((CSng(Text4.Text) * 100) \ 60)), "#00") & "小时" & Format(CStr(CStr((CSng(Text4.Text) * 100) Mod 60)), "#00") & " 分钟"
- data1.UpdateRecord
- End Sub
- Private Sub Command3_Click()
- If data1.Recordset.EOF = False Then data1.Recordset.MoveNext
- If data1.Recordset.EOF Then data1.Recordset.MoveLast
- Text1.Text = data1.Recordset("卡号")
- Text2.Text = data1.Recordset("姓名")
- Text3.Text = data1.Recordset("班级")
- Text4.Text = data1.Recordset("余额")
- Text5.Text = data1.Recordset("日期")
- Text6.Text = data1.Recordset("时间")
- data1.UpdateRecord
- Text9.Text = Format(CStr(CStr((CSng(Text4.Text) * 100) \ 60)), "#00") & "小时" & Format(CStr(CStr((CSng(Text4.Text) * 100) Mod 60)), "#00") & " 分钟"
- End Sub
- Sub 找卡号()
- Dim n, m As Integer
- data1.Refresh
- data1.Recordset.MoveLast
- m = data1.Recordset.RecordCount
- data1.Refresh
- data1.Recordset.MoveFirst
- For n = 1 To m Step 1
- If Text1.Text = data1.Recordset("卡号") Then
- Text1.Text = data1.Recordset("卡号")
- 'Text2.Text = data1.Recordset("姓名")
- 'Text3.Text = data1.Recordset("班级")
- End If
- data1.Recordset.MoveNext
- Next n
- End Sub
- Private Sub Command6_Click()
- Timer2.Enabled = True
- Command9.Enabled = True
- Command6.Enabled = False
- End Sub
- Private Sub Command7_Click()
- Text1.Text = ""
- Text2.Text = ""
- Text3.Text = ""
- Text4.Text = ""
- MSComm1.Output = "rr12345678" '把查询命令发出到串口
- End Sub
- Private Sub Command8_Click()
- Dim NUM, n, m As Integer
- Call CACK_TEXT
- If TEXT_OK = True Then
- NUM = Val(Text4.Text)
- n = NUM \ 256
- m = NUM - n * 256
- 'Call 写入单片机
-
- Call 写入数据库
- text1_temp = Text1.Text
- Text1.Text = ""
- Else
- n = MsgBox("请正确输入", 5 + vbExclamation, "错误")
- End If
- End Sub
- Sub 写入单片机()
- Dim adsafdds As String
- Dim yuer1, yuer2 As String
- yuer1 = Format(CStr(CStr((CSng(Text4.Text) * 100) \ 60)), "#00")
- yuer2 = Format(CStr(CStr((CSng(Text4.Text) * 100) Mod 60)), "#00")
- adsafdds = "ss" & Text1.Text & yuer1 & yuer2
- MSComm1.Output = adsafdds
- End Sub
- Private Sub Command9_Click()
- Timer2.Enabled = False
- Command6.Enabled = True
- Command9.Enabled = False
- End Sub
- Private Sub Form_Load()
- MSComm1.PortOpen = True
- MSComm1.RThreshold = 1
- Command8.Enabled = False
- Command6.Enabled = True
- Command9.Enabled = False
- End Sub
- Sub CACK_TEXT()
- TEXT_OK = False
- If (Len(Text1.Text) = 4 And Len(Text4.Text) < 6) Then TEXT_OK = True
- End Sub
- Sub 写入数据库()
- data1.Refresh
- data1.Recordset.MoveLast
- data1.Recordset.AddNew
- data1.Recordset("卡号") = Text1.Text
- 'data1.Recordset("姓名") = Text2.Text
- 'data1.Recordset("班级") = Text3.Text
- data1.Recordset("余额") = Text4.Text
- data1.Recordset("日期") = Date
- data1.Recordset("时间") = Time()
- data1.UpdateRecord
- End Sub
- Private Sub MSComm1_OnComm() '串口中断
- 'On Error Resume Next
- Static bFlag As Boolean
- Static Xbyte As Long
- Select Case MSComm1.CommEvent '选择事件
- Case comEvReceive '接收到字符
- Dim I As Integer
- Dim InByte() As Byte '定义一个二进制指针放接收到的数据
- InByte = MSComm1.Input '数据转移到指针
- Dim j As Long
- For j = 0 To UBound(InByte) '循环到指针上标
-
- inData = inData & Chr(InByte(j)) '取出一个字节换为16进制显示用
-
- Next j
- DoEvents
- Text1.SelText = inData '将刚收到的字符串显示出来
- inData = ""
- Text1.SelStart = Len(Text1.Text) '光标置后
- If Len(Text1.Text) = 10 Then
- Call 计算
- 'Call 找卡号
- End If
- Case comEventRxOver '接收缓冲区满的处理
- MsgBox "接收缓冲区满了!" '发出警告
- End Select
- End Sub
- Sub 计算()
- Dim temp As String
- Dim 标志 As String
- Dim k As Integer
- Dim yuer1 As String
- Dim 余额 As Integer
- temp = Text1.Text
- 标志 = Left(temp, 1)
- temp = Right(temp, Len(temp) - 2)
- Select Case 标志
- Case "s"
- Text1.Text = Left(temp, 4)
-
- Case "r"
- Text1.Text = ""
- MsgBox "写卡成功!!!!!"
- GoTo 计算end
- Case Else
- Text1.Text = ""
- MsgBox "通讯有误??????"
- GoTo 计算end
- End Select
- temp = Right(temp, 4)
- Text9.Text = Left(temp, 2) & "小时" & Right(temp, 2) & "分钟"
- Text4.Text = Format(CStr((CSng(Left(temp, 2) * 60 + CSng(Right(temp, 2)))) / 100), "#00.00")
- 计算end:
-
- End Sub
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
|