关于vb串口通讯的问题.请帮我看看如何修改

来源:百度知道 编辑:UC知道 时间:2024/06/25 22:24:01
这是我的程序..
Option Explicit

Private Sub Command1_Click()
MSComm1.Settings = "9600,n,8,1"
MSComm1.CommPort = Text3.Text '通讯端口号,根据text3中所输入的内容
MSComm1.NullDiscard = False
MSComm1.RThreshold = 1
MSComm1.InputMode = 1
MSComm1.PortOpen = True
If Len(Text1.Text) = 0 Then Exit Sub
Dim x, l, i As Integer
Dim SendByte() As Byte
l = Len(Text1.Text) \ 2
ReDim SendByte(l - 1)
For i = 0 To l - 1
SendByte(i) = "&h" & Mid(Text1.Text, i * 2 + 1, 2)
Next
If MSComm1.PortOpen = True Then MSComm1.Output = SendByte
End Sub

Private Sub Command2_Click()
Dim data() As Byte
Dim i As Integer
Dim g As Double
ReDim data(1)
For i = 0 To 1
data(i) = "&h" & Mid(Text2.Text, i * 2 + 4, 2)
Next
Text4.Text = Hex(data(1)) & Hex(data(0))
g = CInt("&h" & Text4.Text)
If g < 0 Then g = g + 65536
Text4.Text = CStr(g)<

Private Sub Command1_Click()
If Len(Text1.Text) = 0 Then Exit Sub
Dim x, l, i As Integer
Dim SendByte() As Byte
l = Len(Text1.Text) \ 2
ReDim SendByte(l - 1)
For i = 0 To l - 1
SendByte(i) = "&h" & Mid(Text1.Text, i * 2 + 1, 2)
Next
If MSComm1.PortOpen = True Then MSComm1.Output = SendByte
End Sub

Private Sub Form_Load()
Dim x, l, i As Integer
Text1.Text = ""
Text2.Text = ""
Text3.Text = 1
Text4.Text = ""
MSComm1.Settings = "9600,n,8,1"
MSComm1.CommPort = Text3.Text '通讯端口号,根据text3中所输入的内容
MSComm1.NullDiscard = False
MSComm1.RThreshold = 1
MSComm1.InputMode = 1
MSComm1.PortOpen = True
End Sub
部分代码作了修改。
对你三点疑问的看法:
1)运行时,按第一次command1能接发数据,第二次再按就出错了!!

出错是串口重复打开。
2)接收串口的数据是不断变化,我用了定时器但数据都不会改变,为什么呢??<