-
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
以下为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
栏目列表
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比