vb编程实例讲解、告诉我一些简单的vb编程代码?( 八 )
=
Val(Text1.Text)
-
Val(Text2.Text)
'(-)被点时运行这里
End
Sub
Private
Sub
Command3_Click()
Text3.Text
=
Val(Text1.Text)
*
Val(Text2.Text)
'command3("*")被点时运行这里
End
Sub
Private
Sub
Command4_Click()
Text3.Text
=
Val(Text1.Text)
/
Val(Text2.Text)
'command4("/")被点时运行这里
End
Sub
Q5:vb串口通信程序实例
我也是自己摸索开发出来的每一款VBtoCOM通讯,有具体的思路如下:
给你推荐一个工具“com串口测试工具 ComTone V1.0 中文绿色版”
1、打开你的噪音计的测试软件,调整好串口号、通讯频率等等,我用温度计举例说明 。
开始查询后有返回数值,这个Receive:就是返回的数值000304012200004b05,
打开串口监视精灵,监视软件的com口事件:
这里面的Write是测试软件发出的查询指令,read是设备返回的数据指令
Private Sub Command1_Click() '发送指令
If Not MSComm1.PortOpen Then
MSComm1.CommPort = 7 '串口为7
MSComm1.Settings = "9600,n,8,1"
MSComm1.InBufferCount = 0 '清除接收缓冲区
MSComm1.OutBufferCount = 0 '清除发送缓冲区
MSComm1.InputMode = comInputModeBinary '二进制接收
MSComm1.InputLen = 0 '读取接收缓冲区的所有字符
MSComm1.PortOpen = True'打开串口
MSComm1.RTSEnable = False '置为发送状态
End If
Dim pu() As Byte
Dim strdataAs String
Dim crc_js() As Byte
ReDim pu(7)'这个数组是8位的查询指令
pu(0) = "&H00" '温度计地址
pu(1) = "&H03" '查询指令
pu(2) = "&H00" '2、3为温度计地址
pu(3) = "&H00" '2、3为温度计地址
pu(4) = "&H00" '4、5为读取寄存器长度
pu(5) = "&H02" '4、5为读取寄存器长度
pu(6) = "&HC5" '6、7为CRC校验码 因为我们不知道设备的CRC校验规则所以用测试软件产生的校验码
pu(7) = "&HDA" '6、7为CRC校验码
MSComm1.Output = pu
'不做任何事情,仅仅允许其它应用程序处理它们的事件 。
DoEvents
MSComm1.InBufferCount = 0 '清除接收缓冲区
MSComm1.RThreshold = 9 '所要接收的数据长度,我们通过COM检测精灵看到了回传数据一共是9字节
MSComm1.RTSEnable = True '转为接收状态
End Sub
Private Sub Command2_Click()
Timer1.Enabled = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
If MSComm1.PortOpen Then
MSComm1.PortOpen = False'关闭串口
End If
Timer1.Enabled = False
End Sub
Private Sub MSComm1_OnComm() 'COM事件
Dim PA() As Byte
Dim PB As String
Select Case MSComm1.CommEvent
Case comEvReceive
MSComm1.InputLen = 0 '读取接收缓冲区的所有字符
PB = MSComm1.Input
PA() = PB
For i = 0 To UBound(PA())
'Print "PA(" & i & ")"; PA(i)
If Len(Hex(PA(i))) = 1 Then
strdata = https://www.08ts.cn/strdata &"0" & Hex(PA(i))
Else
strdata = https://www.08ts.cn/strdata & Hex(PA(i))
End If
Next
'回传的数据串:000304012600000AC4,这9字节根据通讯协议我们进行拆分
'00为协议内回传机号,03为读命令,04为返回的数据长度 0126 为我要的温度数据为十六进制表达,下面我进行数据处理
wd = CLng("&H" & Left(Right(strdata, 12), 4)) / 10 & "℃"'根据通讯协议换算成温度
Text1 = Text1 & vbCrLf & strdata & "" & wd
strdatahttps://www.08ts.cn/= ""
MSComm1.PortOpen = False'关闭串口
End Select
End Sub
Private Sub Timer1_Timer()
Call Command1_Click
End Sub
这是我的测试结果 。
下面是通讯协议
这个是我的软件用检测精灵检测的结果
Q6:告诉我一些简单的vb编程代码
给几个写的模块给你看看
Public Function LoadData(strSQL As String)
Dim rst As DAO.Recordset
Dim ctl As Control
Dim fld As Object
Set rst = CurrentDb.OpenRecordset(strSQL, , dbReadOnly)
If Not rst.EOF Then
For Each ctl In ctlFormName
If Not (TypeOf ctl Is Label Or TypeOf ctl Is CommandButton) Then
For Each fld In rst.Fields
If fld.Name = ctl.Name Then
ctl = rst(fld.Name)
Exit For
End If
Next
End If
Next
End If
rst.Close
Set rst = Nothing
End Function
Public Function AddData(TableName As String)
Dim rst As DAO.Recordset
Dim ctl As Control
Dim fld As Object
Set rst = CurrentDb.OpenRecordset(TableName, , dbReadOnly)
For Each ctl In ctlFormName
If Not (TypeOf ctl Is Label Or TypeOf ctl Is CommandButton) Then
推荐阅读
- 西门子编程软件怎么下载安装 s7 200编程软件
- 电子入门基础知识讲解 电子元件基础知识
- 福州邓拓故居讲解 邓拓故居
- 装修房子风水讲究讲解 房屋装修风水学
- 慢走丝编程培训 慢走丝机床
- 英语反义疑问句的用法讲解 反意疑问句
- 从下往上编织儿童套头毛衣 儿童毛衣编织实例
- 宾语从句全方位掌握 英语宾语从句讲解
- ap100编程教程视频!ap100破解版怎么注册
- 六十四卦详解,喜欢易经的朋友, 易经讲解
