| 发表于:2007-03-08 18:21:3411楼 得分:0 |
接前答复: private sub cmdmanualsend_click() if not me.mscomm.portopen then me.mscomm.commport = intport me.mscomm.settings = strset me.mscomm.portopen = true end if call ctrtimer_timer if not blnautosendflag then me.mscomm.portopen = false end if end sub private sub cmdautosend_click() if blnautosendflag then me.ctrtimer.enabled = false if not blnreceiveflag then me.mscomm.portopen = false end if me.cmdautosend.caption = "自动发送 " else if not me.mscomm.portopen then me.mscomm.commport = intport me.mscomm.settings = strset me.mscomm.portopen = true end if me.ctrtimer.interval = inttime me.ctrtimer.enabled = true me.cmdautosend.caption = "停止发送 " end if blnautosendflag = not blnautosendflag end sub private sub ctrtimer_timer() dim longth as integer strsendtext = me.txtsend.text longth = strhextobytearray(strsendtext, bytsendbyte()) if longth > 0 then me.mscomm.output = bytsendbyte end if end sub '输入处理,处理接收到的字节流,并保存在全局变量 private sub inputmanage(bytinput() as byte, intinputlenth as integer) dim n as integer '定义变量及初始化 redim preserve bytreceivebyte(intreceivelen + intinputlenth) for n = 1 to intinputlenth step 1 bytreceivebyte(intreceivelen + n - 1) = bytinput(n - 1) next n intreceivelen = intreceivelen + intinputlenth end sub '为输出准备文本,保存在全局变量 '总行数保存在intline public sub getdisplaytext() dim n as integer dim intvalue as integer dim inthighhex as integer dim intlowhex as integer dim strsinglechr as string * 1 dim intaddress as integer dim intaddressarray(8) as integer dim inthighaddress as integer strascii = " " '设置初值 strhex = " " straddress = " " '获得16进制码和ascii码的字符串 for n = 1 to intreceivelen intvalue = bytreceivebyte(n - 1) if intvalue < 32 or intvalue > 128 then '处理非法字符 strsinglechr = chr(46) '对于不能显示的ascii码, else '用 ". "表示 strsinglechr = chr(intvalue) end if strascii = strascii + strsinglechr inthighhex = intvalue \ 16 intlowhex = intvalue - inthighhex * 16 if inthighhex < 10 then inthighhex = inthighhex + 48 else inthighhex = inthighhex + 55 end if if intlowhex < 10 then intlowhex = intlowhex + 48 else intlowhex = intlowhex + 55 end if strhex = strhex + chr$(inthighhex) + chr$(intlowhex) + " " if (n mod inthexwidth) = 0 then strascii = strascii + chr$(13) + chr$(10) strhex = strhex + chr$(13) + chr$(10) else end if next n txtasc = strascii 'ascii txthex = strhex '16进制 '获得地址字符串 intline = intreceivelen \ inthexwidth if (intreceivelen - inthexwidth * intline) > 0 then intline = intline + 1 end if '设置换行 for n = 1 to intline intaddress = (n - 1) * inthexwidth inthighaddress = 8 intaddressarray(0) = intaddress for m = 1 to inthighaddress intaddressarray(m) = intaddressarray(m - 1) \ 16 next m for m = 1 to inthighaddress intaddressarray(m - 1) = intaddressarray(m - 1) - intaddressarray(m) * 16 next m for m = 1 to inthighaddress if intaddressarray(inthighaddress - m) < 10 then intaddressarray(inthighaddress - m) = intaddressarray(inthighaddress - m) + asc( "0 ") else intaddressarray(inthighaddress - m) = intaddressarray(inthighaddress - m) + asc( "a ") - 10 end if straddress = straddress + chr$(intaddressarray(inthighaddress - m)) next m straddress = straddress + chr$(13) + chr$(10) next n txtadd = straddress '地址 end sub private sub cmdreceive_click() if blnreceiveflag then if not blnreceiveflag then me.mscomm.portopen = false end if me.cmdreceive.caption = "开始接收 " else if not me.mscomm.portopen then me.mscomm.commport = intport me.mscomm.settings = strset me.mscomm.portopen = true end if me.mscomm.inputlen = 0 me.mscomm.inputmode = 0 me.mscomm.inbuffercount = 0 me.mscomm.rthreshold = 1 me.cmdreceive.caption = "停止接收 " end if blnreceiveflag = not blnreceiveflag end sub private sub form_load() inthexwidth = 8 txtadd = " " txthex = " " txtasc = " " txtsend = " " txtadd.width = 1335 txthex.width = 2535 txtasc.width = 1215 '设置默认发送接收关闭状态 blnautosendflag = false blnreceiveflag = false '接收初始化 intreceivelen = 0 '默认发送方式为16进制 'intoutmode = 1 '初始化串行口 intport = 2 inttime = 1000 strset = "19200,e,8,2 " me.mscomm.inbuffersize = 1024 me.mscomm.outbuffersize = 512 if not me.mscomm.portopen then me.mscomm.commport = intport me.mscomm.settings = strset me.mscomm.portopen = true end if me.mscomm.portopen = false end sub private sub cmdclear_click() dim byttemp(0) as byte redim bytreceivebyte(0) intreceivelen = 0 call inputmanage(byttemp, 0) call getdisplaytext call display end sub private sub mscomm_oncomm() dim bytinput() as byte dim intinputlen as integer select case me.mscomm.commevent case comevreceive if blnreceiveflag then if not me.mscomm.portopen then me.mscomm.commport = intport me.mscomm.settings = strset me.mscomm.portopen = true end if '此处添加处理接收的代码 me.mscomm.inputmode = cominputmodebinary '二进制接收 intinputlen = me.mscomm.inbuffercount redim bytinput(intinputlen) bytinput = me.mscomm.input call inputmanage(bytinput, intinputlen) call getdisplaytext 'call display if not blnreceiveflag then me.mscomm.portopen = false end if end if end select end sub private sub display() txthex = " " txtasc = " " txtadd = " " end sub | | |
|