记事本里有数据:
6899999999DE17680808000011484E45238000C816
68999999991E18680808000011484E452380020B16
68999999990000680810000011484E451080AAAAAAAAAAAAAAAA061E16
怎么把这3条数据取出来并且通过232一条一条的发送到单片机? 连续发送5遍.
6899999999DE17680808000011484E45238000C816
68999999991E18680808000011484E452380020B16
68999999990000680810000011484E451080AAAAAAAAAAAAAAAA061E16
怎么把这3条数据取出来并且通过232一条一条的发送到单片机? 连续发送5遍.
给你一个做通断电的小例子,自己加下延时就可以试了
Private Sub Command1_Click()
Dim strtxd(17) As Byte
Dim bh, mm As String
Dim av() As Byte
Dim inttemp As Integer
Form1.Text1.Text = ""
Form1.Text2.Text = ""
Form1.Text3.Text = ""
Form1.Text4.Text = ""
Form1.Text5.Text = ""
Form1.Text6.Text = ""
bh = "12位表号"
mm = "密码"
s1 = ""
If Form1.MSComm1.PortOpen = False Then
Form1.MSComm1.CommPort = "4"
Form1.MSComm1.Settings = "9600,e,8,1"
Form1.MSComm1.PortOpen = True
End If
6:
inttemp = "0"
strtxd(0) = CByte("&H68")
For j = 0 To 5
strtxd(6 - j) = CByte("&H" + Mid(Trim(bh), 2 * j + 1, 2))
Next j
strtxd(7) = CByte("&H68")
strtxd(8) = CByte("&H04")
strtxd(9) = CByte("&H06")
strtxd(10) = CByte("&H6D")
strtxd(11) = CByte("&HF3")
strtxd(12) = CByte("&H33")
For j = 0 To 2
strtxd(15 - j) = CByte("&H" & (Val(Mid(Trim(mm), 2 * j + 1, 2))))
strtxd(15 - j) = strtxd(15 - j) + 51
Next j
For j = 0 To 15
inttemp = inttemp + Val(CByte(strtxd(j)))
Next j
inttemp = inttemp Mod 256
strtxd(16) = "&H" + Trim(Hex(Val(inttemp)))
strtxd(17) = CByte("&H16")
Form1.MSComm1.InputLen = 0
Form1.MSComm1.InBufferCount = 0
Form1.Text1.Text = strtxd
Form1.MSComm1.Output = strtxd
'延时
Form1.MSComm1.RThreshold = 0
Form1.MSComm1.InputMode = comInputModeBinary
Form1.MSComm1.RThreshold = 1
If Form1.MSComm1.InBufferCount >= 11 Then
s2 = Form1.MSComm1.InBufferCount
s = ""
av = Form1.MSComm1.Input
For m = 0 To (s2 - 1) Step 1
If (av(m) < 16) Then
If av(m) <> "63" Then
s = s & "0" + Hex(av(m))
End If
Else
If av(m) <> "63" Then
s = s & Hex(av(m))
End If
End If
Next m
SD1 = Mid(s, 13, 2) & Mid(s, 11, 2) & Mid(s, 9, 2) & Mid(s, 7, 2) & Mid(s, 5, 2) & Mid(s, 3, 2)
If SD1 = bh Then
s8 = Mid(s, 17, 4)
If s8 <> "8400" Then
GoTo 6
Else
Form1.Text7.Text = "断电报警成功"
End If
Else
GoTo 6
End If
Form1.Text2.Text = av
Else
GoTo 6
End If
If Form1.MSComm1.PortOpen = True Then Form1.MSComm1.PortOpen = False
If Form1.MSComm1.PortOpen = False Then
Form1.MSComm1.CommPort = "4"
Form1.MSComm1.Settings = "9600,e,8,1"
Form1.MSComm1.PortOpen = True
End If
7:
inttemp = 0
strtxd(0) = CByte("&H68")
For j = 0 To 5
strtxd(6 - j) = CByte("&H" + Mid(Trim(bh), 2 * j + 1, 2))
Next j
strtxd(7) = CByte("&H68")
strtxd(8) = CByte("&H04")
strtxd(9) = CByte("&H06")
strtxd(10) = CByte("&H6F")
strtxd(11) = CByte("&HF3")
strtxd(12) = CByte("&H33")
For j = 0 To 2
strtxd(15 - j) = CByte("&H" & (Val(Mid(Trim(mm), 2 * j + 1, 2))))
strtxd(15 - j) = strtxd(15 - j) + 51
Next j
For j = 0 To 15
inttemp = inttemp + Val(CByte(strtxd(j)))
Next j
inttemp = inttemp Mod 256
strtxd(16) = "&H" + Trim(Hex(Val(inttemp)))
strtxd(17) = CByte("&H16")
Form1.MSComm1.InputLen = 0
Form1.MSComm1.InBufferCount = 0
Form1.Text3.Text = strtxd
Form1.MSComm1.Output = strtxd
'延时
Form1.MSComm1.RThreshold = 0
Form1.MSComm1.InputLen = 0
Form1.MSComm1.InputMode = comInputModeBinary
Form1.MSComm1.InputLen = 0
Form1.MSComm1.RThreshold = 1
If Form1.MSComm1.InBufferCount >= 11 Then
s2 = Form1.MSComm1.InBufferCount
s = ""
av = Form1.MSComm1.Input
For m = 0 To (s2 - 1) Step 1
If (av(m) < 16) Then
If av(m) <> "63" Then
s = s & "0" + Hex(av(m))
End If
Else
If av(m) <> "63" Then
s = s & Hex(av(m))
End If
End If
Next m
SD1 = Mid(s, 13, 2) & Mid(s, 11, 2) & Mid(s, 9, 2) & Mid(s, 7, 2) & Mid(s, 5, 2) & Mid(s, 3, 2)
If SD1 = bh Then
s8 = Mid(s, 17, 4)
If s8 <> "8400" Then
GoTo 7
Else
Form1.Text8.Text = "断电成功"
End If
Else
GoTo 7
End If
Form1.Text4.Text = av
Else
GoTo 7
End If
If Form1.MSComm1.PortOpen = True Then Form1.MSComm1.PortOpen = False
If Form1.MSComm1.PortOpen = False Then
Form1.MSComm1.CommPort = "4"
Form1.MSComm1.Settings = "9600,e,8,1"
Form1.MSComm1.PortOpen = True
End If
8:
inttemp = 0
strtxd(0) = CByte("&H68")
For j = 0 To 5
strtxd(6 - j) = CByte("&H" + Mid(Trim(bh), 2 * j + 1, 2))
Next j
strtxd(7) = CByte("&H68")
strtxd(8) = CByte("&H04")
strtxd(9) = CByte("&H06")
strtxd(10) = CByte("&H70")
strtxd(11) = CByte("&HF3")
strtxd(12) = CByte("&H33")
For j = 0 To 2
strtxd(15 - j) = CByte("&H" & (Val(Mid(Trim(mm), 2 * j + 1, 2))))
strtxd(15 - j) = strtxd(15 - j) + 51
Next j
For j = 0 To 15
inttemp = inttemp + Val(CByte(strtxd(j)))
Next j
inttemp = inttemp Mod 256
strtxd(16) = "&H" + Trim(Hex(Val(inttemp)))
strtxd(17) = CByte("&H16")
Form1.MSComm1.InputLen = 0
Form1.MSComm1.InBufferCount = 0
Form1.Text5.Text = strtxd
Form1.MSComm1.Output = strtxd
'延时
Form1.MSComm1.RThreshold = 0
Form1.MSComm1.InputLen = 0
Form1.MSComm1.InputMode = comInputModeBinary
Form1.MSComm1.InputLen = 0
Form1.MSComm1.RThreshold = 1
If Form1.MSComm1.InBufferCount >= 11 Then
s2 = Form1.MSComm1.InBufferCount
s = ""
av = Form1.MSComm1.Input
For m = 0 To (s2 - 1) Step 1
If (av(m) < 16) Then
If av(m) <> "63" Then
s = s & "0" + Hex(av(m))
End If
Else
If av(m) <> "63" Then
s = s & Hex(av(m))
End If
End If
Next m
SD1 = Mid(s, 13, 2) & Mid(s, 11, 2) & Mid(s, 9, 2) & Mid(s, 7, 2) & Mid(s, 5, 2) & Mid(s, 3, 2)
If SD1 = bh Then
s8 = Mid(s, 17, 4)
If s8 <> "8400" Then
GoTo 8
Else
Form1.Text9.Text = "通电成功"
End If
Else
GoTo 8
End If
Form1.Text6.Text = av
Else
GoTo 8
End If
If Form1.MSComm1.PortOpen = True Then Form1.MSComm1.PortOpen = False
MsgBox ("通断电测试完成!")
End Sub
1)读取多行文本,通过SPLIT分割为字符串数组.
2)采用TIMER控件执行发送,以下提供发送部分代码:
Option Explicit
Dim Send(2) As String
Dim sum As Integer
Dim sum1 As IntegerPrivate Sub Command1_Click()
sum = 0
sum1 = 0
Timer1.Enabled = True
End SubPrivate Sub Form_Load()
Send(0) = "6899999999DE17680808000011484E45238000C816"
Send(1) = "68999999991E18680808000011484E452380020B16"
Send(2) = "68999999990000680810000011484E451080AAAAAAAAAAAAAAAA061E16"
MSComm1.PortOpen = True
Timer1.Interval = 500
Timer1.Enabled = False
sum = 0
sum1 = 0
End SubPrivate Sub Timer1_Timer()
MSComm1.Output = Send(sum)
sum = sum + 1
If sum > 2 Then
sum = 0
sum1 = sum1 + 1
If sum1 >= 5 Then
Timer1.Enabled = False
End If
End If
End Sub
其次LZ你的文本似乎是16进制表示的指令,如果通信是按BYTE字节指令发送的,则应该将16进制字符串转换为BYTE字节流发送.