是接受串口数据,每秒4个数据,自动传. 我的问题是我这样做可以不,因为vb是单线程,用这种方法接收数据会
我的问题是我这样做可以不以,因为vb是单线程,用这种方法接收数据会不会造成程序出错,我的整个程序中还用了几个timer控件,以前我在ide环境下调试会出现缓冲区益出的问题...
我的问题是我这样做可以不以,因为vb是单线程,用这种方法接收数据会不会造成程序出错,我的整个程序中还用了几个timer控件,以前我在ide环境下调试会出现缓冲区益出的问题,所以我加了一个时间控件定时把缓冲区清空.
现在用这个代码能运行,我不知道有什么缺陷,如果长时间运行会不会死掉?
Private Sub PresureMSComm_OnComm()
Dim Buf$
Dim Inbyte() As Byte
Dim temp$
On Error GoTo ErrorTrap
Select Case PresureMSComm.CommEvent
Case comEvReceive
Inbyte = PresureMSComm.Input
For i = LBound(Inbyte) To UBound(Inbyte)
Buf = Buf + Hex(Inbyte(i))
Next i
If Len(Buf) >= 8 And Mid(Buf, 1, 4) = "4044" Then '
ComStateText.Text = "压力通讯:" & Buf & ",工作正常。" & vbCrLf & Gcalcomstr
Buf = Mid(Buf, 6) '40 44 01 xx xx 0D接收数据16进制格式
'取压力值
Buf = Mid(Trim(Buf), 1, Len(Trim(Buf)) - 1)
'转十进制
Buf = Val("&H" & Buf)
temp = Val(Buf)
Text1.Text = Format$(temp * 0.01, "0.00")
Else
PresureMSComm.PortOpen = False
Buf = ""
PresureMSComm.PortOpen = True
writelog ("com测试" & "小于8个或者首字符不是4044" & Now)
ComStateText.Text = "压力通讯:" & "通讯故障!" & vbCrLf & Gcalcomstr
Text1.Text = Format$(0, "0.00")
End If
Comflag = True '通讯标志
Case comEvSend
Case Else
PresureMSComm.InBufferCount = 0
writelog ("com测试" & "出错" & Now)
End Select
Exit Sub
ErrorTrap:
writelog ("FormMain—" & "PresureMSComm_OnComm()" & Err.Description & Now)
End Sub 展开
现在用这个代码能运行,我不知道有什么缺陷,如果长时间运行会不会死掉?
Private Sub PresureMSComm_OnComm()
Dim Buf$
Dim Inbyte() As Byte
Dim temp$
On Error GoTo ErrorTrap
Select Case PresureMSComm.CommEvent
Case comEvReceive
Inbyte = PresureMSComm.Input
For i = LBound(Inbyte) To UBound(Inbyte)
Buf = Buf + Hex(Inbyte(i))
Next i
If Len(Buf) >= 8 And Mid(Buf, 1, 4) = "4044" Then '
ComStateText.Text = "压力通讯:" & Buf & ",工作正常。" & vbCrLf & Gcalcomstr
Buf = Mid(Buf, 6) '40 44 01 xx xx 0D接收数据16进制格式
'取压力值
Buf = Mid(Trim(Buf), 1, Len(Trim(Buf)) - 1)
'转十进制
Buf = Val("&H" & Buf)
temp = Val(Buf)
Text1.Text = Format$(temp * 0.01, "0.00")
Else
PresureMSComm.PortOpen = False
Buf = ""
PresureMSComm.PortOpen = True
writelog ("com测试" & "小于8个或者首字符不是4044" & Now)
ComStateText.Text = "压力通讯:" & "通讯故障!" & vbCrLf & Gcalcomstr
Text1.Text = Format$(0, "0.00")
End If
Comflag = True '通讯标志
Case comEvSend
Case Else
PresureMSComm.InBufferCount = 0
writelog ("com测试" & "出错" & Now)
End Select
Exit Sub
ErrorTrap:
writelog ("FormMain—" & "PresureMSComm_OnComm()" & Err.Description & Now)
End Sub 展开
展开全部
假如你需显示在frmMain窗体,用
frmMain.Text1.Text = Hex(a(3))
显示在frmwel窗体,用
frmwel.Text1.Text = Hex(a(3))
frmwel窗体代码:
Option Explicit
Dim inData As String
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "38400,N,8,1"
MSComm1.InBufferSize = 1024
MSComm1.OutBufferSize = 1024
MSComm1.InputMode = comInputModeBinary '.InputModeConstants.comInputModeBinary
MSComm1.RThreshold = 1
MSComm1.InputLen = 0
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Dim inLen As Integer
Dim i As Integer
Dim a() As Byte
Select Case MSComm1.CommEvent
Case MSCommLib.OnCommConstants.comEvReceive
inLen = MSComm1.InputLen
ReDim a(inLen)
a = MSComm1.Input
Text1.Text = Hex(a(3))
frmMain.Text1.Text = Hex(a(3))
For i = 0 To UBound(a)
If Len(Hex(a(i))) = 1 Then
inData = inData & "0" & Hex(a(i))
Else
inData = inData & Hex(a(i))
End If
Next
End Select
End Sub
frmMain窗体代码:
Option Explicit
Private Sub Command1_Click()
Dim bData(5) As Byte
bData(0) = &HBA
bData(1) = &H4
bData(2) = &HC7
bData(3) = &H2
bData(4) = &H1
frmwel.MSComm1.Output = bData
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Form_Load()
Load frmwel
End Sub
frmMain.Text1.Text = Hex(a(3))
显示在frmwel窗体,用
frmwel.Text1.Text = Hex(a(3))
frmwel窗体代码:
Option Explicit
Dim inData As String
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "38400,N,8,1"
MSComm1.InBufferSize = 1024
MSComm1.OutBufferSize = 1024
MSComm1.InputMode = comInputModeBinary '.InputModeConstants.comInputModeBinary
MSComm1.RThreshold = 1
MSComm1.InputLen = 0
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Dim inLen As Integer
Dim i As Integer
Dim a() As Byte
Select Case MSComm1.CommEvent
Case MSCommLib.OnCommConstants.comEvReceive
inLen = MSComm1.InputLen
ReDim a(inLen)
a = MSComm1.Input
Text1.Text = Hex(a(3))
frmMain.Text1.Text = Hex(a(3))
For i = 0 To UBound(a)
If Len(Hex(a(i))) = 1 Then
inData = inData & "0" & Hex(a(i))
Else
inData = inData & Hex(a(i))
End If
Next
End Select
End Sub
frmMain窗体代码:
Option Explicit
Private Sub Command1_Click()
Dim bData(5) As Byte
bData(0) = &HBA
bData(1) = &H4
bData(2) = &HC7
bData(3) = &H2
bData(4) = &H1
frmwel.MSComm1.Output = bData
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Form_Load()
Load frmwel
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
清空什么缓冲区?我看你这用的都是局部变量啊
追问
PresureMSComm.InBufferCount = 0
接收的缓冲区
追答
正因为vb是单线程的所以我觉得这个程序应该没问题PresureMSComm_OnComm接收的数据在没处理完之前不会被timer清空,第一次数据处理完之前到来的新的PresureMSComm_OnComm也会被阻塞,所以应该没什么问题
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询