在pb中用Api函数与多个串口通讯

Fancy_Free 2005-04-12 02:55:53
现在可以与一个串口通讯,要实现同时与多个串口通讯,即同时打开,同时发送和同时接收怎么写,求助!
...全文
335 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
hwh88888 2005-07-15
  • 打赏
  • 举报
回复
首先定义外部函数:
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 )

其中有几个结构,如不知道可以查查其他的文档
hwh88888 2005-07-15
  • 打赏
  • 举报
回复
如果用api函数写串口通讯的话,则几个串口与一个串口是一样的写法,再定义的时候多定义几个即可
Fancy_Free 2005-07-15
  • 打赏
  • 举报
回复
有啊,比如说即使不加扩展卡,也可以有两个串口通讯端口的
PHDYCN 2005-07-13
  • 打赏
  • 举报
回复
我也只用过一对一的串口通讯, 一个主机哪来N个串口进行上下位机的通讯啊
Fancy_Free 2005-07-13
  • 打赏
  • 举报
回复
都这么久了,还没有人帮我解决啊?
Fancy_Free 2005-04-20
  • 打赏
  • 举报
回复
请问具体怎么写,有源代码吗?
wu_07 2005-04-19
  • 打赏
  • 举报
回复
将相关操作封装到用户对象,然后

SharedObjectRegister

SharedObjectGet

SharedObjectUnregister

试试
Fancy_Free 2005-04-19
  • 打赏
  • 举报
回复
难到pb就不能吗?
hygougou 2005-04-16
  • 打赏
  • 举报
回复
最好用其他线程比较支持的工具开发
Fancy_Free 2005-04-16
  • 打赏
  • 举报
回复
Fancy_Free 2005-04-15
  • 打赏
  • 举报
回复
没有人知道阿?
j9dai 2005-04-14
  • 打赏
  • 举报
回复
API串口通讯?没用过,帮顶
Fancy_Free 2005-04-14
  • 打赏
  • 举报
回复
我也用过mscomm组件,感觉不太好,所以用api改了,但是只能同时与一个串口通讯,也可以先关闭一个串口再打开另一个串口,但是不知道怎么与多个串口通讯
hornbilltofy 2005-04-13
  • 打赏
  • 举报
回复
同时发送和同时接收是指多线程并行吗?
本人只试过在一个窗口中放了n个ms comm控件,分别设置他们的port,分别操作,分别响应,感觉跟同时差不多,似乎不需要API.
Fancy_Free 2005-04-13
  • 打赏
  • 举报
回复
自己顶一下

680

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder API 调用
社区管理员
  • API 调用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧