VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • vb.net教程之VB。Net串行通信DataControl的实例

代码如下:
以下为Module1.vb代码
=============================================
Module Module1
  Declare Function WriteByte Lib "ACALLPORTIO.dll" Alias "ACALLPORTIO_WriteByte" (ByVal address As Int16, ByVal DataOut As Byte) As Int16
  Declare Function WriteWord Lib "ACALLPORTIO.dll" Alias "ACALLPORTIO_WriteWord" (ByVal address As Int16, ByVal DataOut As Int16) As Int16
  Declare Function ReadByte Lib "ACALLPORTIO.dll" Alias "ACALLPORTIO_ReadByte" (ByVal address As Int16, ByRef DataIn As Byte) As Int16
  Declare Function ReadWord Lib "ACALLPORTIO.dll" Alias "ACALLPORTIO_ReadWord" (ByVal address As Int16, ByRef DataIn As Int16) As Int16

  Declare Function DInit Lib "ACALLPORTIO.dll" Alias "ACALLPORTIO_Init" () As Int16
  Declare Function DClose Lib "ACALLPORTIO.dll" Alias "ACALLPORTIO_Close" () As Int16
  Declare Function GetDllVersion Lib "ACALLPORTIO.dll" Alias "ACALLPORTIO_GetDllVersion" (ByRef wDLLVersion As Integer) As Int16
  Declare Function GetDriverVersion Lib "ACALLPORTIO.dll" Alias "ACALLPORTIO_GetDriverVersion" (ByRef wDriverVersion As Integer) As Int16
  Public Declare Sub Sleep Lib "kernel32" (ByVal time As Integer)
End Module
=============================================
以下为Module1.vb代码
=============================================
Public Class Form1
    Dim DataAddress, CtlAddress, StatusAddress As Short  '印表机地址
    Dim fInit As Boolean  '初始化旗标
    '**************************************************************
    '初始化按钮的Click事件
    '必须先将驱动程序初始化,才能顺利进行控制
    '**************************************************************
    Private Sub btnInit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInit.Click
        If DInit() <> 0 Then
            MsgBox("~初始化错误~", MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly, "驱动程序错误")
        Else
            gbData.Enabled = True
            gbControl.Enabled = True
            gbStatus.Enabled = True
            fInit = True
        End If
    End Sub
    '**************************************************************
    '结束按钮的Click事件
    '将驱动程序关闭后结束程序
    '**************************************************************
    Private Sub btnEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnd.Click
        If DClose() <> 0 Then
            MsgBox("~关闭驱动程序发生错误~", MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly, "驱动程序错误")
        Else
            End
        End If
    End Sub
    '**************************************************************
    '表单的Load事件
    '定义地址
    '**************************************************************
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataAddress = &H378              '资料端口地址
        CtlAddress = DataAddress + 2   '控制端口地址 
        StatusAddress = DataAddress + 1 '状态端口地址
        fInit = False
    End Sub
    '**************************************************************
    '资料输出埠的CheckedChanged事件
    '均呼叫DataOutput子程序
    '**************************************************************
    Private Sub chkD0_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkD0.CheckedChanged, chkD1.CheckedChanged, _
     chkD2.CheckedChanged, chkD3.CheckedChanged, chkD4.CheckedChanged, chkD5.CheckedChanged, chkD6.CheckedChanged, chkD7.CheckedChanged
        DataOutput()
    End Sub
    '**************************************************************
    '处理资料输出埠的输出动作
    '每一个Data端口控制的CheckBox组件都呼此子程序
    '**************************************************************
    Sub DataOutput()
        Dim Ldata As Short
        If chkD0.Checked Then
            Ldata = 1
        Else
            ldata = 0
        End If
        If chkD1.Checked Then Ldata += 2 ^ 1
        If chkD2.Checked Then Ldata += 2 ^ 2
        If chkD3.Checked Then Ldata += 2 ^ 3
        If chkD4.Checked Then Ldata += 2 ^ 4
        If chkD5.Checked Then Ldata += 2 ^ 5
        If chkD6.Checked Then Ldata += 2 ^ 6
        If chkD7.Checked Then Ldata += 2 ^ 7
        WriteByte(DataAddress, Ldata) '输出到资料埠
    End Sub
    Private Sub chkC0_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkC0.CheckedChanged, _
    chkC1.CheckedChanged, chkC2.CheckedChanged, chkC3.CheckedChanged
        ControlOutput()
    End Sub
    '**************************************************************
    '处理资料输出埠的输出动作
    '每一个Control端口控制的CheckBox组件都呼此子程序
    '**************************************************************
    Sub ControlOutput()
        Dim Ctldata As Short
        If chkC0.Checked Then
            Ctldata = 1
        Else
            Ctldata = 0
        End If
        If chkC1.Checked Then Ctldata += 2 ^ 1
        If chkC2.Checked Then Ctldata += 2 ^ 2
        If chkC3.Checked Then Ctldata += 2 ^ 3
        WriteByte(CtlAddress, Ctldata) '输出到控制埠
    End Sub
    '**************************************************************
    '计时器的Tick事件
    '我们将状态值的侦测放在这个里面
    '透过各bit的检查确定状态
    '**************************************************************
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim i As Short, BB As Byte
        If Not fInit Then Exit Sub
        i = ReadByte(StatusAddress, BB)         '读取状态埠资料
        If (BB And &H8) Then                          '检查传回值中的第三位是否为1
            picErr.Image = picON.Image   '改变灯号为红色
        Else
            picErr.Image = picOFF.Image      '改变灯号为黑色
        End If
        If (BB And &H10) Then                     '检查传回值中的第四位是否为1
            picOL.Image = picON.Image
        Else
            picOL.Image = picOFF.Image
        End If
        If (BB And &H20) Then                    '检查传回值中的第五位是否为1
            picEmpty.Image = picON.Image
        Else
            picEmpty.Image = picOFF.Image
        End If
        If (BB And &H40) Then                   '检查传回值中的第六位是否为1
            picACK.Image = picON.Image
        Else
            picACK.Image = picOFF.Image
        End If
        If (BB And &H80) Then                   '检查传回值中的第七位是否为1
            picBuzy.Image = picON.Image
        Else
            picBuzy.Image = picOFF.Image
        End If
    End Sub
End Class
 

相关教程