Visual Basic串口通讯调试方法
发布时间:2008/6/3 0:00:00 访问次数:632
visual basic串口通讯调试方法
现有电子秤一台,使用串口与计算机进行通讯。编写vb程序来访问串口,达到读取电子秤上显示的数据。该电子秤为be01型仪表,输出为rs-232c标准接口,波特率为300-9600、偶校验、7个数据位、2个停止位。所有字符均发送11位ascii码,一个起始位。在vb中与串口通讯需要引入控件mscomm串口通讯控件(在microsoft comm control 6.0中)。具体程序如下:控件简称:msc
dim out(12) as byte '接收var中的值
dim var as variant '接收msc.input中的数值
dim nrece as integer '计算msc.inputbuffer的个数
dim i as integer, j as integer '随即变量,计算循环
****************************************************************************
private sub form_load()
cleartext
with msc
.commport = 1 '设置com1为通信端口
.settings = "9600,e,7,2" '设置通信端口参数 9600赫兹、偶校验、7个数据位、1个停止位.(这里需要进一步说明的是:.setting=”bbbb,p,d,s”。
含义是:b:baud rate(波特率);p:parity(奇偶);d:data bit;s:stop bit)
.inbuffersize = 40 '设置缓冲区接收数据为40字节
.inputlen = 1 '设置input一次从接收缓冲读取字节数为1
.rthreshold = 1 '设置接收一个字节就产生oncomm事件
end with
end sub
****************************************************************************
private sub cleartext()
text3.text = ""
text2.text = "5"
text1.text = ""
end sub
private sub command1_click()
cleartext
' nrece = 0 '计数器清零
with msc
.inputmode = cominputmodebinary '设置数据接收模式为二进制形式
.inbuffercount = 0 '清除接收缓冲区
if not .portopen then
.portopen = true '打开通信端口
end if
end with
end sub
private sub msc_oncomm()
delaytime ‘用来延续时间
cleartext
with msc
select case .commevent '判断通信事件
case comevreceive: '收到rthreshold个字节产生的接收事件
swichvar 1
if out(1) = 2 then '判断是否为数据的开始标志
.rthreshold = 0 '关闭oncomm事件接收
end if
do
doevents
loop until .inbuffercount >= 3 '循环等待接收缓冲区>=3个字节
' nrece = nrece + 1
for i = 2 to 12
swichvar i
text1.text = text1.text & chr(out(i))
next
text1.text = ltrim(text1.text)
text2.text = text2.text & cstr(nrece)
.rthreshold = 1 '打开mscomm事件接收
case else
' .portopen = false
end select
end with
end sub
****************************************************************************
private sub delaytime()
dim bdt as boolean
dim sprevious as single, slast as single
bdt = true
sprevious = timer (timer可以计算从子夜到现在所经过的秒数,在microsoft windows中,timer函数可以返回一秒的小数部分)
do while bdt
if timer - sprevious >= 0.3 then bdt = false
loop
bdt = true
end sub
(通信传输速率为9600bps,则最快速度1.04ms发送一个字节,仪表每秒发送50帧数据,每帧数据有4个字节,即每秒发送200个字节,平均5.0ms 发送一个字节,连续读取串口数据时要在程序中添加循环等待程序)
private sub swichvar(byval nnum as integer)
delaytime
var = null
var = msc.input
out(nnum) = var(0)
end sub
(设置接收数据模式采用二进制形式,即 inputmode=cominputmodebinary,但用input属性读取数据时,不能直接赋值给 byte 类型变量,只能通过先赋值给一个 variant 类型变量,返回一个二进制数据的数组,再转换保存到byte类型数变量中。)
private sub text1_change()
text3.text = ctext(text1.text) - ctext(text2.text)
end sub
****************************************************************************
private function ctext(byval str as string) as currency
if str <> "" then
ctext = ccur(val(str))
else
ctext = 0
end if
end function
(仪表每秒发送50帧数据,微机收到一帧完整数据
visual basic串口通讯调试方法
现有电子秤一台,使用串口与计算机进行通讯。编写vb程序来访问串口,达到读取电子秤上显示的数据。该电子秤为be01型仪表,输出为rs-232c标准接口,波特率为300-9600、偶校验、7个数据位、2个停止位。所有字符均发送11位ascii码,一个起始位。在vb中与串口通讯需要引入控件mscomm串口通讯控件(在microsoft comm control 6.0中)。具体程序如下:控件简称:msc
dim out(12) as byte '接收var中的值
dim var as variant '接收msc.input中的数值
dim nrece as integer '计算msc.inputbuffer的个数
dim i as integer, j as integer '随即变量,计算循环
****************************************************************************
private sub form_load()
cleartext
with msc
.commport = 1 '设置com1为通信端口
.settings = "9600,e,7,2" '设置通信端口参数 9600赫兹、偶校验、7个数据位、1个停止位.(这里需要进一步说明的是:.setting=”bbbb,p,d,s”。
含义是:b:baud rate(波特率);p:parity(奇偶);d:data bit;s:stop bit)
.inbuffersize = 40 '设置缓冲区接收数据为40字节
.inputlen = 1 '设置input一次从接收缓冲读取字节数为1
.rthreshold = 1 '设置接收一个字节就产生oncomm事件
end with
end sub
****************************************************************************
private sub cleartext()
text3.text = ""
text2.text = "5"
text1.text = ""
end sub
private sub command1_click()
cleartext
' nrece = 0 '计数器清零
with msc
.inputmode = cominputmodebinary '设置数据接收模式为二进制形式
.inbuffercount = 0 '清除接收缓冲区
if not .portopen then
.portopen = true '打开通信端口
end if
end with
end sub
private sub msc_oncomm()
delaytime ‘用来延续时间
cleartext
with msc
select case .commevent '判断通信事件
case comevreceive: '收到rthreshold个字节产生的接收事件
swichvar 1
if out(1) = 2 then '判断是否为数据的开始标志
.rthreshold = 0 '关闭oncomm事件接收
end if
do
doevents
loop until .inbuffercount >= 3 '循环等待接收缓冲区>=3个字节
' nrece = nrece + 1
for i = 2 to 12
swichvar i
text1.text = text1.text & chr(out(i))
next
text1.text = ltrim(text1.text)
text2.text = text2.text & cstr(nrece)
.rthreshold = 1 '打开mscomm事件接收
case else
' .portopen = false
end select
end with
end sub
****************************************************************************
private sub delaytime()
dim bdt as boolean
dim sprevious as single, slast as single
bdt = true
sprevious = timer (timer可以计算从子夜到现在所经过的秒数,在microsoft windows中,timer函数可以返回一秒的小数部分)
do while bdt
if timer - sprevious >= 0.3 then bdt = false
loop
bdt = true
end sub
(通信传输速率为9600bps,则最快速度1.04ms发送一个字节,仪表每秒发送50帧数据,每帧数据有4个字节,即每秒发送200个字节,平均5.0ms 发送一个字节,连续读取串口数据时要在程序中添加循环等待程序)
private sub swichvar(byval nnum as integer)
delaytime
var = null
var = msc.input
out(nnum) = var(0)
end sub
(设置接收数据模式采用二进制形式,即 inputmode=cominputmodebinary,但用input属性读取数据时,不能直接赋值给 byte 类型变量,只能通过先赋值给一个 variant 类型变量,返回一个二进制数据的数组,再转换保存到byte类型数变量中。)
private sub text1_change()
text3.text = ctext(text1.text) - ctext(text2.text)
end sub
****************************************************************************
private function ctext(byval str as string) as currency
if str <> "" then
ctext = ccur(val(str))
else
ctext = 0
end if
end function
(仪表每秒发送50帧数据,微机收到一帧完整数据