123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- $PBExportHeader$uo_pbcomm.sru
- $PBExportComments$串口访问对象
- forward
- global type uo_pbcomm from nonvisualobject
- end type
- type commprop from structure within uo_pbcomm
- end type
- type commtimeouts from structure within uo_pbcomm
- end type
- type dcb from structure within uo_pbcomm
- end type
- end forward
- type commprop from structure
- integer wPacketLength
- integer wPacketVersion
- long dwServiceMask
- long dwReserved1
- long dwMaxTxQueue
- long dwMaxRxQueue
- long dwMaxBaud
- long dwProvSubType
- long dwProvCapabilities
- long dwSettableParams
- long dwSettableBaud
- integer wSettableData
- integer wSettableStopParity
- long dwCurrentTxQueue
- long dwCurrentRxQueue
- long dwProvSpec1
- long dwProvSpec2
- character wcProvChar[1]
- end type
- type commtimeouts from structure
- long ReadIntervalTimeout
- long ReadTotalTimeoutMultiplier
- long ReadTotalTimeoutConstant
- long WriteTotalTimeoutMultiplier
- long WriteTotalTimeoutConstant
- end type
- type dcb from structure
- long DCBlength
- long RaudRate
- long fBinary
- long fParity
- long fOutxCtsFlow
- long fOutxDsrFlow
- long fDtrControl
- long fDsrSensitivity
- long fTXContinueOnXoff
- long fOutX
- long fInX
- long fErrorChar
- long fNull
- long fRtsControl
- long fAbortOnError
- long fDummy2
- integer wReserved
- integer XonLim
- integer XoffLim
- character ByteSize
- character Parity
- character StopBits
- character XonChar
- character XoffChar
- character ErrorChar
- character EofChar
- character EvtChar
- character wReserved1
- end type
- global type uo_pbcomm from nonvisualobject
- event ue_thread ( )
- end type
- global uo_pbcomm uo_pbcomm
- type prototypes
- 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" alias for "ReadFile;Ansi"
- 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" alias for "CreateFileA;Ansi"
- Function Boolean WriteFile(uLong handle,ref string lpbuffer,ulong numbytes, ref ulong bytesread, ulong lpOverLaped) Library "Kernel32.dll" alias for "WriteFile;Ansi"
- Function Boolean GetCommProperties(ulong hFile,ref COMMPROP lpCommProp ) Library "Kernel32.dll" alias for "GetCommProperties;Ansi"
- Function Boolean BuildCommDCBA(ref string lpDef,ref DCB lpDCB ) Library "Kernel32.dll" alias for "BuildCommDCBA;Ansi"
- Function Boolean SetCommState(ulong hCommDev,ref DCB lpdcb ) Library "Kernel32.dll" alias for "SetCommState;Ansi"
- Function Boolean GetCommState(ulong hCommDev,ref DCB lpdcb ) Library "Kernel32.dll" alias for "GetCommState;Ansi"
- Function ulong GetLastError() Library "Kernel32.dll"
- Function Boolean SetCommTimeouts(ulong hCommDev, ref COMMTIMEOUTS lpctmo ) Library "Kernel32.dll" alias for "SetCommTimeouts;Ansi"
- Function Boolean PurgeComm(ulong hCommDev, ulong fdwAction ) Library "Kernel32.dll"
- end prototypes
- type variables
- ulong iu_file //句柄
- boolean ib_lock //锁
- window iw_parent //父对象
- uo_thread uo_thread_pbcomm //线程对象
- string input,output //数据缓冲
- boolean PortOpen //端口打开状态
- long InputLen,OutputLen //数据缓冲长度
- string CommPort //端口名称
- string CommSettings //端口参数
- long CommError //错误代码
- end variables
- forward prototypes
- public function boolean of_open (string ls_com, string ls_commset)
- public subroutine of_close ()
- public function string of_read ()
- public subroutine of_setparent (window arg_parent)
- end prototypes
- event ue_thread();if CommError=0 and PortOpen and (not ib_lock) then
- ulong lu_bytesread, lu_numbytes
- string ls_buff
- ulong lstr_locstruct
- ls_buff = space(4096)
- setnull(lstr_locstruct)
- lu_numbytes = 4096
- if (not readfile ( iu_file, ls_buff, lu_numbytes, lu_bytesread, lstr_locstruct)) then
- CommError=getlasterror ( )
- else
- input+=left(ls_buff,lu_bytesread)
- inputlen=len(input)
- if lu_bytesread > 0 then
- iw_parent.postevent("ue_recieve")
- else
- end if
- end if
- end if
- end event
- public function boolean of_open (string ls_com, string ls_commset);dcb lst_dcb
- PortOpen = true
- //打开串口
- iu_file = CreateFileA(ls_com,3221225472,0,0,3,128,0)
- if (iu_file < 0) then
- if PortOpen then closehandle(iu_file)
- PortOpen=false
- goto ext
- end if
- //初始化DCB
- if (Not BuildCommDcbA ( ls_commset, lst_dcb )) then
- if PortOpen then closehandle(iu_file)
- PortOpen=false
- goto ext
- end if
- //设置串口
- if (Not setcommstate ( iu_file, lst_dcb )) then
- if PortOpen then closehandle(iu_file)
- PortOpen=false
- goto ext
- end if
- //设置超时
- commtimeouts lst_to
- lst_to.readintervaltimeout = 4294967295 //MAXDWORD
- SetCommTimeouts(iu_file, lst_to)
- CommError=0
- ext:
- return PortOpen
- end function
- public subroutine of_close ();//uo_thread_pbcomm.ins_stopflag=1
- if PortOpen then
- closehandle(iu_file)
- PortOpen=false
- end if
- end subroutine
- public function string of_read ();if not PortOpen then
- messagebox("错误","端口未打开",StopSign!)
- return ""
- end if
- string ls_input_temp
- ib_lock=true //加锁
- ls_input_temp=input
- input=""
- inputlen=0
- ib_lock=false //解锁
- return ls_input_temp
- end function
- public subroutine of_setparent (window arg_parent);iw_parent = arg_parent
- end subroutine
- on uo_pbcomm.create
- call super::create
- TriggerEvent( this, "constructor" )
- end on
- on uo_pbcomm.destroy
- TriggerEvent( this, "destructor" )
- call super::destroy
- end on
- event constructor;PortOpen=false
- ib_lock=false
- SharedObjectRegister("uo_thread","pbcommobject") //将uo—thread对象注册为pbcommobject
- SharedObjectGet("pbcommobject",uo_thread_pbcomm) //用uo—thread引用共享对象uo_thread_pbcomm
- uo_thread_pbcomm.of_setparent(this)
- uo_thread_pbcomm.Post of_start()
- end event
- event destructor;of_close()
- SharedObjectUnRegister("pbcommobject")
- //destroy uo_thread_pbcomm
- end event
|