| 发表于:2007-05-18 15:06:08 楼主 |
这个问题已经困扰了我一个多月,刚接触vb开发,头疼啊!!! 具体设计方案如下: 利用msp430单片机和nrf401无线模块做232通信 nrf401模块发射和接收是利用一个电平高低来切换的,我利用了串口里的 rts脚,通过修改mscomm1.rtsenable来做pc端nrf401模块的切换,单片机端 利用一个io口 pc上用vb编程,控件选用mscomm利用串口发送指令给nrf401模块a 这时候模块a为发射状态,模块b为接收状态, a发射之后,切换为接收状态,b接收到数据之后交给单片机校验 如果为正确则单片机切换模块b为发射状态,将处理之后的数据通过 b发送到a 现在问题就出在如果程序连续运行就是没有数据返回,如果让单片机和pc都分别手动中断一下,然后让单片机手动运行,数据发送完成之后再让pc程序运行,这样可以接收到数据。真令人费解啊,问题出在哪里呢??? 我的vb程序如下所示: option explicit private declare function gettickcount lib "kernel32 " () as long dim frame_ok as boolean dim rebuf$ dim outbyte() as byte dim redata() as byte dim inbyte() as byte private sub command1_click() dim buf$, i% '要发射的一帧数据 redim outbyte(1 to 13) outbyte(1) = &h55 outbyte(2) = &h68 outbyte(3) = &h6 outbyte(4) = &h6 outbyte(5) = &h68 outbyte(6) = &h1 outbyte(7) = &h10 outbyte(8) = &h1 outbyte(9) = &h0 outbyte(10) = &h0 outbyte(11) = &h0 outbyte(12) = &h12 outbyte(13) = &h16 mscomm1.output = outbyte do doevents loop until mscomm1.outbuffercount = 0 mscomm1.outbuffercount = 0 mscomm1.portopen = false mscomm1.rtsenable = false 'rts复位,进入接收状态 mscomm1.portopen = true dim t as long t = gettickcount() do datareceive loop until frame_ok = true or gettickcount - t > 10000 mscomm1.portopen = false mscomm1.rtsenable = true 'rts置位,返回发射状态 mscomm1.portopen = true if frame_ok = true then frame_ok = false msgbox "接收成功 " erase redata else msgbox "无数据返回 " erase redata end if end sub private sub command2_click() mscomm1.portopen = false end end sub private sub form_load() mscomm1.commport = 1 mscomm1.portopen = true mscomm1.rtsenable = true 'rts置位,为发射状态 end sub public function datareceive() as byte() dim i%, j%, inbytelen%, temp% inbyte = mscomm1.input for i = lbound(inbyte) to ubound(inbyte) if inbyte(i) = &h68 then '检测帧头 checkframe (i) if frame_ok = true then inbytelen = inbyte(i + 1) redim redata(1 to inbytelen) for j = 1 to inbytelen redata(j) = inbyte(i + j + 3) next j erase inbyte exit function end if end if next i end function public function checkframe(i as integer) '帧校验程序,从略 end function //现在可以确定的是pc发射数据到了单片机,单片机做了正确的处理,也发射了正确的数据,但是pc端就是接收不到 我曾经以为是单片机端处理速度过快,在pc端还没切换为接收状态的时候就已经发射完毕,但是我在单片机端做了延时,还是接收不到数据,令人费解啊!!! |
|
|
|
|