紧急求救:怎样在 pb 中控制并口(parallel port)?

wsv 2002-03-19 09:19:05
紧急求救:怎样在 pb 中控制并口(parallel port)?,往它发送字符?
目的是控制打钱机执行一些操作。
...全文
81 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
autumn_fox 2002-03-19
  • 打赏
  • 举报
回复
关注
killerdanny 2002-03-19
  • 打赏
  • 举报
回复
下面是两位高手以不同方法来处理对并口发送字符的方法,希望对大家有所帮助

第一种方法:
作者:JiangAn anjiang@writeme.com
我是通过调用API实现串口调用的,根据TAPI介绍,串口调用与并口调用并没有什么不同,下面的
程序可试一试(我只实现串口,并口并没有试过,希望能有一点参考作用):
首先定义外部函数:
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 )


具体函数使用方法可参见MSDN中的TAPI。
有时可能用控件做起来更简单一些(如SCOMM、SAX COMM等)。



第二种方法:作者主页:http://niceera.yeah.net, 点这里下载相关文件.(还需要另外两个文件,它们在C++Builder 4和Delphi 4中都有,Borlndmn.dll和vcl40.bpl)

作者前些时间用C++Builder4专为PB写的一个向并口发送数据的DLL,希望对大家有帮助。

Function Boolean PrepareForPrint(String port) Library "rawLptDrv.dll"
打开指定的并口,比如“LPT1”;

Function Boolean RawPrint(String dt,integer ln) Library "rawLptDrv.dll"
向并口发送数据;

Function Boolean OverPrint() Library "rawLptDrv.dll"
关闭并口。

---------------------------------------------------

下面的函数是为了测试用的。
Function Boolean ShowPrinterStatus(integer x,integer y) Library "rawLptDrv.dll"
Function Boolean HidePrinterStatus() Library "rawLptDrv.dll"
Subroutine calltest() Library "rawLptDr.dll"

---------------------------------------------------

如果运行时报告什么错误,可能是缺少 C++Builder4 的一些DLL。
//

wsv 2002-03-19
  • 打赏
  • 举报
回复
对不起,上面我输错一个字,'打钱机'应为'打印机'.

401

社区成员

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

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