读书人

求在PowerBuilder中调用Pcomm串行通讯

发布时间: 2012-03-16 16:34:56 作者: rapoo

求在PowerBuilder中调用Pcomm串行通讯组件的例子
想在pb9下调用Pcomm串行通讯组件实现通讯,在Moxa的官网上下载了PComm Lite 2000,Version1.4,但是HELP里没有pb调用的例子。哪位大虾用过,望不吝赐教。e_mail:csupk@163.com

[解决办法]
不是有帮助么?这个有个不好的是不能主动接受消息 只能读写
[解决办法]
PB65下从串口通信获取程控交换机计费数据的API调用


首先定义外部函数:
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("错误","无法创—CB" + " #" + 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 )
具体函数使用方法可参见MSDN中的TAPI。 有时可能用控件做起来更简单一些(如SCOMM、SAX COMM等)。
[解决办法]
我这里有个例子

http://download.csdn.net/source/2452946

读书人网 >PB

热点推荐