我想通过按"接收"后,文本框显示接收到的数据,但是为什么文本就不显示数据呢?望大侠帮忙看看我的程序有什么问题!谢谢!我是通过串口调试工具来发送数据的.
Private blnReceiveFlag As BooleanPrivate Sub cmdReceive_Click()
blnReceiveFlag = ture
Timer1.Enabled = True
Text1.Text = CStr(df)
End SubPrivate Sub comm_OnComm()
Dim intInputLen As Integer
Dim df() As Byte '定义这个接收
If blnReceiveFlag = True Then
If Not comm.PortOpen Then
comm.CommPort = 1
comm.Settings = "4800,n,8,1"
comm.PortOpen = True
End If
Do While comm.InBufferCount <> 0
df = comm.Input
Text1.Text = CStr(df)
Loop
End If
End SubPrivate Sub Form_Load()
blnReceiveFlag = False
Timer1.Enabled = False
Timer1.Interval = 1000
'初始化串行口
comm.CommPort = 1
comm.Settings = "4800,n,8,1"
comm.InBufferCount = 0
comm.InputLen = 0
comm.RThreshold = 1
comm.InputMode = comInputModeBinary
comm.PortOpen = True
End SubPrivate Sub Form_Unload(Cancel As Integer)
comm.PortOpen = False
End Sub
Private blnReceiveFlag As BooleanPrivate Sub cmdReceive_Click()
blnReceiveFlag = ture
Timer1.Enabled = True
Text1.Text = CStr(df)
End SubPrivate Sub comm_OnComm()
Dim intInputLen As Integer
Dim df() As Byte '定义这个接收
If blnReceiveFlag = True Then
If Not comm.PortOpen Then
comm.CommPort = 1
comm.Settings = "4800,n,8,1"
comm.PortOpen = True
End If
Do While comm.InBufferCount <> 0
df = comm.Input
Text1.Text = CStr(df)
Loop
End If
End SubPrivate Sub Form_Load()
blnReceiveFlag = False
Timer1.Enabled = False
Timer1.Interval = 1000
'初始化串行口
comm.CommPort = 1
comm.Settings = "4800,n,8,1"
comm.InBufferCount = 0
comm.InputLen = 0
comm.RThreshold = 1
comm.InputMode = comInputModeBinary
comm.PortOpen = True
End SubPrivate Sub Form_Unload(Cancel As Integer)
comm.PortOpen = False
End Sub
可以这样改就行了:
Vartext = MSComm1.Input
VarLen = MSComm1.InBufferCount
ReDim VarSave(LBound(Vartext) To UBound(Vartext))
For i = LBound(Vartext) + 1 To UBound(Vartext) - 1
VarSave(i) = Vartext(i)
Text2.Text = Text2.Text & VarSave(i)
Next
你可以参考以上的方法写,这是我调试成功过的!!
If MSComm1.PortOpen = False Then
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
' 告诉控件读入整个缓冲区
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeText
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.PortOpen = True
If Err Then
MsgBox ("串口通信无效")
Exit Sub
End If
Else
MsgBox ("端口已经打开")
Exit Sub
End IfEnd SubPrivate Sub cmd_receieve_Click()
txt_receieve.Text = MSComm1.Input
MsgBox ("串口通信")
End SubPrivate Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
' 告诉控件读入整个缓冲区
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeText
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
If Err Then
MsgBox ("串口通信无效")
Exit Sub
End If
End If
End Sub这是我写的两台计算机串口通信的,已经测试过了,没问题,你的程序主要在于你定义的是二进制方式接收:
Dim df() As Byte '定义这个接收
你把类型改为variant试试,而且对于接收数据,用MSComm的oncomm事件中的comEvReceive比较好,再者对于二进制数据通信,要设定每次发送和接收的字节数,以及一共要接收多少位,这些你都没有体现,你试试这段代码Private Sub Command1_Click()
Dim Buffer as Variant
Dim Arr() as Byte'设定并打开端口
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.PortOpen = True'设定 InputMode 读取二进制数据
MSComm1.InputMode = comInputModeBinary'等待直到输入缓冲区有 10 个字节
Do Until MSComm1.InBufferCount < 10
DoEvents
Loop'往缓冲区存二进制数据
Buffer = MSComm1.Input'赋值于字节数组以便处理
Arr = Buffertext1.text=cstr(Buffer)
End Sub