| 发表于:2008-02-19 16:54:038楼 得分:0 |
按你的问题,发送chr$(16) + chr$(58) + pr + chr$(13),其中chr$(16)和chr$(13)可发送,如果接收到chr$(16)和chr$(13)等不能显示的ascii字符,建议使用二进制方式发送,通常是将16进制字符串转为二进制的byte数据类型发送。 接收的ascii码值有> 128到255,同样建议按二进制方式接收。给你连接的代码就是按二进制方式接收的。我将我的代码修改如下: - vbscript code
option explicit
dim sj() as byte
dim strdata as string
private sub command1_click() '发送数据
dim buffer as variant
dim strsend as string
dim i as integer
redim sj(len(text1.text) / 2 - 1)
for i = 0 to len(text1.text) - 1 step 2
sj(i / 2) = val("&h" & mid(text1.text, i + 1, 2))
next
mscomm1.output = sj
strdata = ""
end sub
private sub form_load()
mscomm1.settings = "9600,n,8,1"
mscomm1.rthreshold = 1
mscomm1.portopen = true
text1 = "103a0d"
end sub
private sub mscomm1_oncomm()
'通讯事件发生
dim indata as variant
dim bytinput() as byte
dim intinputlen as integer
dim i as integer
select case mscomm1.commevent
case comevreceive '...有接受事件发生
'此处添加处理接收的代码
mscomm1.inputmode = cominputmodebinary '二进制接收
intinputlen = mscomm1.inbuffercount
redim bytinput(intinputlen)
bytinput = mscomm1.input
'jieshou
for i = 0 to ubound(bytinput)
if len(hex(bytinput(i))) = 1 then
strdata = strdata & "0" & hex(bytinput(i))
else
strdata = strdata & hex(bytinput(i))
end if
next
text2.text = strdata
end select
mscomm1.inbuffercount = 0
mscomm1.outbuffercount = 0
end sub
如需连续发送可通过timer计时器按固定时间间隔 call command1_click事件 - vbscript code
private sub command1_click()
timer1.enabled = true
end sub
private sub form_load()
mscomm1.settings = "9600,n,8,1"
mscomm1.rthreshold = 1
mscomm1.portopen = true
text1 = "103a0d"
timer1.interval = 1000 '
timer1.enabled = false
end sub
private sub timer1_timer()
call command1_click
end sub
| | |
|