Home > Software engineering >  Vb to a serial port to send a string of hexadecimal instruction, to realize communication
Vb to a serial port to send a string of hexadecimal instruction, to realize communication

Time:09-30

Pray god to help look at this program, enter the decimal finally how to convert hexadecimal, how to realize serial communication? The target instruction code is as follows:
FEFE FE 68 20 00 00 03 13 24 December 03 01 00 00 00 00 00 00 00 00 05 00 00 00 00 05 00 00 00 00 0 2 c e 16
Requirements in the first column input values such as 123456.78 (999999.99), the maximum is converted to 78 h 56 h 34 h, 12 h style and replace to 00 00 00 00 05 00 00 00 00 05 00 00 00 00 between 2 c (05 05 2 c as a unit of the same) and send instructions to the single chip microcomputer, a great god help me analysis the error, how to correct?

Private Sub Command2_Click ()
Dim s As a String, I As an Integer, j As an Integer, k As an Integer, m (3) As an Integer, a

S=Val (Text1. Text) * 100
J=Len (s)
For k=0 To 7 - j Step 1
S="0" & amp; S
Next k
For I=1 To Len (s) Step 2
Text5. Text=Text5. Text & amp; Mid (s, Len (s) - I, 2) & amp; "
"Next
M (3)=Mid (s, 1, 2)
Text6. Text=Val (m (3) 10) \ * 16 + Val (m (3) the Mod 10)
'm (2)=Mid (s, 3, 2)
'm (1)=Mid (s, 5, 2)
'm (0)=Mid (s, 7, 2)
'a=Array (m (0), m (1), m (2), m (3))
'For I=0 To 3
'Text6. Text=Text6. Text & amp; "" & amp; M (I) 'Text6=Text6 & amp; VbCrLf & amp; M (0) & amp; M (1) & amp; M (2) & amp; M (3)
'Next I
's=Text2. Text * 100
'j=Len (s)
'For k=0 To 7 - j Step 1
's="0" & amp; S
'the Next k
'For I=1 To Len (s) Step 2
'Text6. Text=Text6. Text & amp; Mid (s, Len (s) - I, 2) & amp; "
"'Next
's=Text3. Text * 100' traffic cannot exceed six integer, consider * 100 remove
'j=Len (s)
'For k=0 To 7 - j Step 1
's="0" & amp; S
'the Next k
'For I=1 To Len (s) Step 2
'Text7. Text=Text7. Text & amp; Mid (s, Len (s) - I, 2) & amp; "
"'Next
'Text4. Text=& amp; H46 & amp; H45 & amp; H46 & amp; H45 & amp; H46 & amp; H45 & amp; H46 & amp; H45 & amp; H68 & amp; H20 & amp; HAA & amp; HAA & amp; HAA & amp; HAA & amp; HAA & amp; HAA & amp; HAA & amp; VbCrLf & amp; Text5. Text & amp; "05" & amp; Text6. Text & amp; "05" & amp; Text7. Text & amp; "2 c 81 16"
End Sub

Private Sub Form_Load ()
MPort=3
MSComm1.ComMSComm1. Settings="2400, N, 8, 1", "
'MSComm1. An InputMode=comInputModeText
MSComm1. InputLen=1
MSComm1. RThreshold=1
MSComm1. The Output=$(46) + cha cha $(45)
'MSComm1. PortOpen=True
End Sub

Private Sub MSComm1_OnComm ()
Dim buffer As a String
Buffer=MSComm1. Input
MSComm1. OutBufferCount=0

End Sub

CodePudding user response:

Reference: http://www.vbgood.com/thread-83700-1-1.html

CodePudding user response:

 Option Explicit 

Private Function Bytes2String ((a) As Byte) As String
Dim s As String
Dim lCount As Long
Dim As Long I

LCount=UBound (a) + 1
S=Space $(lCount * 3)
For I=0 To lCount - 1
Mid $(s, I * 3 + 1, 2)=Right $(" 0 "& amp; Hex (a (I)), 2)
Next

Bytes2String=s
End the Function

Private Function String2Bytes (ByVal s As String) As Byte ()
Dim (a) As Byte
Dim lCount As Long
Dim As Long I

LCount=Len (s) \ 3
ReDim a (lCount - 1)
For I=0 To lCount - 1
A (I)=CByte (" & amp; H "& amp; Mid $(s, I * 3 + 1, 2))
Next

String2Bytes=a
End the Function

Private Sub SetValue (aData () As Byte, ByVal index As Long, ByVal value As Double)
Dim s As String

S=Right $(" 0000000 "& amp; CStr (Int (100) value *), 8)

AData (index + 0)=CByte (" & amp; H "& amp; Mid $(s, 7, 2))
AData (index + 1)=CByte (" & amp; H "& amp; Mid $(s, 5, 2))
AData (index + 2)=CByte (" & amp; H "& amp; Mid $(s, 3, 2))
AData (index + 3)=CByte (" & amp; H "& amp; Mid $(s, 1, 2))
End Sub

Private Sub Command2_Click ()
Dim aData () As Byte

AData=https://bbs.csdn.net/topics/String2Bytes (" FE FE FE 68 20 00 00 03 13 24 December 03 01 00 00 00 00 00 00 00 00 05 00 00 00 00 05 00 00 00 00 0 2 c e 16 ")
SetValue aData, 17, Val (Text1)
SetValue aData, 22, Val (Text2)
SetValue aData, 27, Val (Text3)
Text4=Bytes2String (aData)

MSComm1. The Output=aData
End Sub

Here are three values were 123456.78, 3.777, 98765432.1 data
 FE FE FE 20 00 00 00 00 00 03 13 24 68 03 01 00 78 56 34 12 December 05, 78 03 00 00 0 05 10 32 54 76 2 c e 16 

  • Related