Home > Software engineering >  Want to learn to be a VB RS232 communication tools, but had been communication on that
Want to learn to be a VB RS232 communication tools, but had been communication on that

Time:03-20

 Private Sub Combo1_Click () 'for serial number 
Label6. Caption=Mid (Combo1. Text, 4, 1)
End Sub
Private Sub Combo2_click () 'to get the data format
Label7. Caption=Combo2. Text
End Sub
Private Sub Command1_Click ()
Err. Clear
If Combo1. Text="please select the port number" Or Combo2. Text="please select a data format" Then
MsgBox "please select the port number and data format"
ElseIf MSComm1. PortOpen=True Then
MSComm1. PortOpen=False
Command1. Caption="open the serial port"
ElseIf MSComm1. PortOpen=False Then
'MSComm1. PortOpen=True
'MsgBox TypeName (CInt (Mid (Combo1. Text, 4, 1)))

With MSComm1
CommPort=Val (mids (Combo1. Text, 4, 1))
. The Settings=Combo2. Text
. An InputMode=comInputModeBinary
. InputLen=0 'each receive bytes
. SThreshold=8 'set to 0 will not trigger comEvSend
. RThreshold=1 'receive threshold, when the number of bytes is received & gt;=1, trigger receive events
InBufferSize=1024 'accept buffer size
OutBufferSize=1024 'send buffer size
. InBufferCount=0
. OutBufferCount=0
. RTSEnable=True 'receiver function can make

On the Error Resume Next
. PortOpen=True
End With
Command1. Caption="close serial port"
The Else
Label9. Caption="connection is broken!"

End the If
If MSComm1. PortOpen=True Then
Label9. Caption="connection success!"
Label9. ForeColor=vbGreen
The Else
Label9. Caption="connection is broken!"
Label9. ForeColor=vbRed
End the If
'0 means no error occurs, the initial connection normal
Label10. Caption=Err. Number
End Sub

Private Sub Command2_Click ()
Err. Clear
Label9. Caption="connection is broken!"
Label9. ForeColor=vbRed
If MSComm1. PortOpen=True Then MSComm1. PortOpen=False Else MsgBox "not connected, without having to disconnect!"
End Sub

Private Sub Command3_Click ()
If MSComm1. PortOpen=True Then
Dim CMDSTR As String
Dim sdata As Byte (8)
Sdata (0)=CByte (& amp; H1) 'CInt (Combo4. Text)
Sdata (1)=CByte (& amp; H3)
Sdata (2)=CByte (& amp; H6)
Sdata (3)=CByte (& amp; H3)
Sdata (4)=CByte (& amp; H0)
Sdata (5)=CByte (& amp; H1)
Sdata (6)=CByte (& amp; H74)
Sdata (7)=CByte (& amp; H82)
For I=0 To 7
If Len (Hex (sdata (I))) for the processing not 0=1 Then 'to fill
CMDSTR=CMDSTR & amp; "0" & amp; Hex (sdata (I))
The Else
CMDSTR=CMDSTR & amp; Hex (sdata (I))
End the If
Next I

MSComm1. The Output=CMDSTR
Label12. Caption=CMDSTR
Label15. Caption mEvent=MSComm1.Com
End the If
End Sub

Private Sub Command4_Click () 'to close the window
If MSComm1. PortOpen=True Then MSComm1. PortOpen=False
Unload Form1
End Sub

Private Sub Command5_Click ()
Label11. Caption=""
Label12. Caption=""
MSComm1. InBufferCount=0
MSComm1. OutBufferCount=0

End Sub

Private Sub form_load ()
Timer1. Interval=500 '500 ms
Timer1. Enabled=True

Dim I, iCounter As Integer
ICounter=0
For I=1 To 16 'cycle test serial
MPort=I
MSComm1.ComOn the Error Resume Next
MSComm1. PortOpen=True
If Err. Number & lt;> Then 8002
MSComm1. PortOpen=False
Combo3. AddItem "COM" & amp; CStr (I), iCounter
ICounter=iCounter + 1
End the If
Next I
If Combo3. ListCount & gt; 0 Then Combo3. ListIndex=0

End Sub
Private Sub MSComm1_OnComm ()
Dim inx () As Byte
Select Case mEvent MSComm1.Com
Case comEvReceive
Label13. Caption="came in?" '... Receive data
Inx=MSComm1. Input
'MSComm1. InBufferCount=0
For k=0 To 7
TMPSTR=TMPSTR & amp; "/" & amp; Hex (inx (k))
Next k
Label11. Caption=TMPSTR '... Receive data
End the Select
End Sub

Private Sub Timer1_Timer () 'entered here triggered after according to the set time, but need a global variable
'the Label13. Caption="Interval=" & amp; CStr (ivisibal * 1)
'If ivisibal Mod 2=0 Then implementation effect
'the Command1. Visible=True
'the Label13. Visible=True
'the Else
'the Command1. Visible=False
'the Label13. Visible=False
'End If
Ivisibal=ivisibal + 1

End Sub


Question:
1, whether the connection is successful, whether directly use PortOpen=TRUE? Or how to drop?
2, remove the question 1, is now under the procedures and a connection machine equipment, such as servo drive, even if I send data write death there, as shown above:

 Private Sub Command3_Click () 
If MSComm1. PortOpen=True Then
Dim CMDSTR As String
Dim sdata As Byte (8)
Sdata (0)=CByte (& amp; H1) 'CInt (Combo4. Text)
Sdata (1)=CByte (& amp; H3)
Sdata (2)=CByte (& amp; H6)
Sdata (3)=CByte (& amp; H3)
Sdata (4)=CByte (& amp; H0)
Sdata (5)=CByte (& amp; H1)
Sdata (6)=CByte (& amp; H74)
Sdata (7)=CByte (& amp; H82)
For I=0 To 7
If Len (Hex (sdata (I))) for the processing not 0=1 Then 'to fill
CMDSTR=CMDSTR & amp; "0" & amp; Hex (sdata (I))
The Else
CMDSTR=CMDSTR & amp; Hex (sdata (I))
End the If
Next I

MSComm1. The Output=CMDSTR

By displaying the outbuffercount has a value of 0, if there is not data buffer, stop at 0 OMcomm events at the same time, the whole 3 days, is trying not to come out,


question 3: requirements for data types have MSCOMM attributes, such as whether the COMMPORT must be converted to an integer, such as: Cint () (if I use the variable to transfer instead of writing death)


nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related