在pb中如何用ole控制串口收发数值型数据

 我来答
崔亚正
2014-11-21 · 超过23用户采纳过TA的回答
知道答主
回答量:30
采纳率:0%
帮助的人:7.3万
展开全部
PB65下从串口通信获取程控交换机计费数据的API调用如何做?
Posted by powerbuilder学生 on August 13, 1999 at 10:15:46:
我是通过调用API实现的:
如你用的是二进制的,请把缓冲区改成BLOB型的。
首先定义外部函数:
Function Boolean CloseHandle(ulong hObject ) Library "kernel32.dll"
FUNCTION boolean ReadFile(ulong fhand, ref string lpbuffer, ulong numbyte, ref ulong bytesread, ulong lpover) LIBRARY "kernel32.dll"
FUNCTION ulong CreateFileA(ref string fname, ulong f_access, ulong f_share, ulong f_sec, ulong f_create, ulong f_flag, ulong f_attrib) LIBRARY "kernel32.dll"
Function Boolean WriteFile(uLong handle,ref string lpbuffer, ulong numbytes, ref ulong bytesread, ulong lpOverLaped) Library "Kernel32.dll"
Function Boolean GetCommProperties(ulong hFile,ref COMMPROP lpCommProp ) Library "Kernel32.dll"
Function Boolean BuildCommDCBA(ref string lpDef,ref DCB lpDCB ) Library "Kernel32.dll"
Function Boolean SetCommState(ulong hCommDev,ref DCB lpdcb ) Library "Kernel32.dll"
Function Boolean GetCommState(ulong hCommDev,ref DCB lpdcb ) Library "Kernel32.dll"
Function ulong GetLastError() Library "Kernel32.dll"
Function Boolean SetCommTimeouts(ulong hCommDev,ref COMMTIMEOUTS lpctmo ) Library "Kernel32.dll"
Function Boolean PurgeComm(ulong hCommDev, ulong fdwAction ) Library "Kernel32.dll"
实例变量定义:
ulong iu_file
string is_buffer//数据缓冲
string is_valid_num = "123" //有效数据
第二是初始设置
string ls_com,ls_commset
dcb lst_dcb
ls_com = "COM1"
//ib_reading = true
//打开串口
iu_file = CreateFileA(ls_com,3221225472,0,0,3,128,0)
if (iu_file < 0) then
messagebox("错误","无法打开" + ls_com + " #" + string(getlasterror (
)),StopSign!)
end if
//初始化DCB
ls_commset = "4800,N,8,1"
if (Not BuildCommDcbA ( ls_commset, lst_dcb )) then
messagebox("错误","无法创建DCB" + " #" + string(getlasterror ( )),StopSign!)
end if

//设置串口
if (Not setcommstate ( iu_file, lst_dcb )) then
messagebox("错误","无法设置串口"+ ls_com + " #" + string(getlasterror (
)),StopSign!)
end if

//设置超时
commtimeouts lst_to
lst_to.readintervaltimeout = 4294967295 //MAXDWORD
//lst_to.readtotaltimeoutconstant = 60000
//lst_to.readtotaltimeoutmultiplier = 10
SetCommTimeouts(iu_file, lst_to)

第三步:读或写(例子中的是读,写可用writefile)
ulong lu_bytesread, lu_numbytes
string ls_buff
ulong lstr_locstruct //注意!这是ULONG 而不是STRUCT

ls_buff = space(100)
setnull(lstr_locstruct)
lu_numbytes = 100

if (not readfile ( iu_file, ls_buff, lu_numbytes, &
lu_bytesread, lstr_locstruct)) then
messagebox("错误","无法读取 #" + string(getlasterror ( )),StopSign!)
timer(0)
else
if( lu_bytesread > 0) then
is_buffer += trim(ls_buff)
mle_1.text += trim(ls_buff)
//处理数据
this.wf_is_valid_receieve ( ) //检验函数
end if
end if

第四步:完成后释放端口
closehandle ( iu_file )
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式