| 发表于:2008-01-18 09:43:531楼 得分:5 |
贴子中的代码存在错误,不能接收. 1. bte(0)是何变量名,接收代码中用的是bytinput()byte动态数组 2. mscomm1属性rthreshold必需> 0,方能产生oncomm事件 3. 必需注意程序中变量的准确定义 代码修改如下: - 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 = "001234567890ff00"
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
| | |
|