| 发表于:2007-01-28 19:45:121楼 得分:0 |
代码修改如下,调试时见实时错误 '8018 ',只有端口打开时,操作有效. option explicit private outtemp(5) as byte '//接收数组 private chnlcnt as integer private samflag as boolean private sub form_load() mscomm1.commport = 1 mscomm1.settings = "9600,n,8,1 " mscomm1.inbuffersize = 20 mscomm1.outbuffersize = 20 mscomm1.inputmode = cominputmodebinary mscomm1.inputlen = 1 mscomm1.sthreshold = 0 mscomm1.inbuffercount = 0 mscomm1.outbuffercount = 0 mscomm1.rthreshold = 1 outtemp(0) = &h1 outtemp(1) = &h2 outtemp(2) = &h30 outtemp(3) = &h30 outtemp(4) = &h3 end sub private sub mscomm1_oncomm() dim buffer as variant samflag = false with mscomm1 select case .commevent case comevreceive buffer = .input intemp(1) = buffer(0) if intemp(1) = &h2 then .rthreshold = 0 '关闭oncomm事件接收 do doevents loop until .inbuffercount > = 1 buffer = .input intemp(2) = buffer(0) if intemp(2) = &h1e then '//如果是联络帧,五个字节 label1.caption = "连接成功 " do doevents loop until .inbuffercount > = 3 '//一直到接收完数据 commok = true end if '//联络部分 if intemp(2) = &h6 then '//要求重发数据 do doevents loop until .inbuffercount > = 3 end if if intemp(2) = &h1f then '是数据 数据帧11字节 do doevents loop until .inbuffercount > = 8 buffer = mscomm1.input intemp(3) = buffer(0) buffer = mscomm1.input intemp(4) = buffer(0) buffer = mscomm1.input buffer = mscomm1.input indata(0) = buffer(0) buffer = mscomm1.input indata(1) = buffer(0) buffer = mscomm1.input indata(2) = buffer(0) buffer = mscomm1.input indata(3) = buffer(0) buffer = mscomm1.input indata(4) = buffer(0) buffer = mscomm1.input samflag = true '//一组数据接受完毕 end if end if mscomm1.rthreshold = 1 case else end select end with end sub '定时器2s private sub timer3_timer() if chnlcnt > = 8 then chnlcnt = 1 timer3.enabled = false samflag = false exit sub end if outtemp(3) = chnlcnt mscomm1.output = outtemp '//采集命令outtemp(3)为通道号 do doevents loop until samflag samflag = false chnlcnt = chnlcnt + 1 end sub 3 end if if intemp(2) = &h1f then '是数据 数据帧11字节 do doevents loop until .inbuffercount > = 8 buffer = .input intemp(3) = buffer(0) buffer = .input intemp(4) = buffer(0) buffer = .input buffer = .input indata(0) = buffer(0) buffer = .input indata(1) = buffer(0) buffer = .input indata(2) = buffer(0) buffer = .input indata(3) = buffer(0) buffer = .input indata(4) = buffer(0) buffer = .input samflag = true '//一组数据接受完毕 end if end if .rthreshold = 1 case else end select end with end sub 定时器2s private sub timer3_timer() if chnlcnt > = 8 then chnlcnt = 1 timer3.enabled = false samflag = false exit sub end if outtemp(3) = chnlcnt mscomm1.output = outtemp '//采集命令outtemp(3)为通道号 do doevents loop until samflag samflag = false chnlcnt = chnlcnt + 1 end sub | | |
|