uo_pbcomm.sru 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. $PBExportHeader$uo_pbcomm.sru
  2. $PBExportComments$串口访问对象
  3. forward
  4. global type uo_pbcomm from nonvisualobject
  5. end type
  6. type commprop from structure within uo_pbcomm
  7. end type
  8. type commtimeouts from structure within uo_pbcomm
  9. end type
  10. type dcb from structure within uo_pbcomm
  11. end type
  12. end forward
  13. type commprop from structure
  14. integer wPacketLength
  15. integer wPacketVersion
  16. long dwServiceMask
  17. long dwReserved1
  18. long dwMaxTxQueue
  19. long dwMaxRxQueue
  20. long dwMaxBaud
  21. long dwProvSubType
  22. long dwProvCapabilities
  23. long dwSettableParams
  24. long dwSettableBaud
  25. integer wSettableData
  26. integer wSettableStopParity
  27. long dwCurrentTxQueue
  28. long dwCurrentRxQueue
  29. long dwProvSpec1
  30. long dwProvSpec2
  31. character wcProvChar[1]
  32. end type
  33. type commtimeouts from structure
  34. long ReadIntervalTimeout
  35. long ReadTotalTimeoutMultiplier
  36. long ReadTotalTimeoutConstant
  37. long WriteTotalTimeoutMultiplier
  38. long WriteTotalTimeoutConstant
  39. end type
  40. type dcb from structure
  41. long DCBlength
  42. long RaudRate
  43. long fBinary
  44. long fParity
  45. long fOutxCtsFlow
  46. long fOutxDsrFlow
  47. long fDtrControl
  48. long fDsrSensitivity
  49. long fTXContinueOnXoff
  50. long fOutX
  51. long fInX
  52. long fErrorChar
  53. long fNull
  54. long fRtsControl
  55. long fAbortOnError
  56. long fDummy2
  57. integer wReserved
  58. integer XonLim
  59. integer XoffLim
  60. character ByteSize
  61. character Parity
  62. character StopBits
  63. character XonChar
  64. character XoffChar
  65. character ErrorChar
  66. character EofChar
  67. character EvtChar
  68. character wReserved1
  69. end type
  70. global type uo_pbcomm from nonvisualobject
  71. event ue_thread ( )
  72. end type
  73. global uo_pbcomm uo_pbcomm
  74. type prototypes
  75. Function Boolean CloseHandle(ulong hObject ) Library "kernel32.dll"
  76. FUNCTION boolean ReadFile(ulong fhand, ref string lpbuffer, ulong numbyte, ref ulong bytesread, ulong lpover) LIBRARY "kernel32.dll" alias for "ReadFile;Ansi"
  77. 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"
  78. Function Boolean WriteFile(uLong handle,ref string lpbuffer,ulong numbytes, ref ulong bytesread, ulong lpOverLaped) Library "Kernel32.dll" alias for "WriteFile;Ansi"
  79. Function Boolean GetCommProperties(ulong hFile,ref COMMPROP lpCommProp ) Library "Kernel32.dll" alias for "GetCommProperties;Ansi"
  80. Function Boolean BuildCommDCBA(ref string lpDef,ref DCB lpDCB ) Library "Kernel32.dll" alias for "BuildCommDCBA;Ansi"
  81. Function Boolean SetCommState(ulong hCommDev,ref DCB lpdcb ) Library "Kernel32.dll" alias for "SetCommState;Ansi"
  82. Function Boolean GetCommState(ulong hCommDev,ref DCB lpdcb ) Library "Kernel32.dll" alias for "GetCommState;Ansi"
  83. Function ulong GetLastError() Library "Kernel32.dll"
  84. Function Boolean SetCommTimeouts(ulong hCommDev, ref COMMTIMEOUTS lpctmo ) Library "Kernel32.dll" alias for "SetCommTimeouts;Ansi"
  85. Function Boolean PurgeComm(ulong hCommDev, ulong fdwAction ) Library "Kernel32.dll"
  86. end prototypes
  87. type variables
  88. ulong iu_file //句柄
  89. boolean ib_lock //锁
  90. window iw_parent //父对象
  91. uo_thread uo_thread_pbcomm //线程对象
  92. string input,output //数据缓冲
  93. boolean PortOpen //端口打开状态
  94. long InputLen,OutputLen //数据缓冲长度
  95. string CommPort //端口名称
  96. string CommSettings //端口参数
  97. long CommError //错误代码
  98. end variables
  99. forward prototypes
  100. public function boolean of_open (string ls_com, string ls_commset)
  101. public subroutine of_close ()
  102. public function string of_read ()
  103. public subroutine of_setparent (window arg_parent)
  104. end prototypes
  105. event ue_thread();if CommError=0 and PortOpen and (not ib_lock) then
  106. ulong lu_bytesread, lu_numbytes
  107. string ls_buff
  108. ulong lstr_locstruct
  109. ls_buff = space(4096)
  110. setnull(lstr_locstruct)
  111. lu_numbytes = 4096
  112. if (not readfile ( iu_file, ls_buff, lu_numbytes, lu_bytesread, lstr_locstruct)) then
  113. CommError=getlasterror ( )
  114. else
  115. input+=left(ls_buff,lu_bytesread)
  116. inputlen=len(input)
  117. if lu_bytesread > 0 then
  118. iw_parent.postevent("ue_recieve")
  119. else
  120. end if
  121. end if
  122. end if
  123. end event
  124. public function boolean of_open (string ls_com, string ls_commset);dcb lst_dcb
  125. PortOpen = true
  126. //打开串口
  127. iu_file = CreateFileA(ls_com,3221225472,0,0,3,128,0)
  128. if (iu_file < 0) then
  129. if PortOpen then closehandle(iu_file)
  130. PortOpen=false
  131. goto ext
  132. end if
  133. //初始化DCB
  134. if (Not BuildCommDcbA ( ls_commset, lst_dcb )) then
  135. if PortOpen then closehandle(iu_file)
  136. PortOpen=false
  137. goto ext
  138. end if
  139. //设置串口
  140. if (Not setcommstate ( iu_file, lst_dcb )) then
  141. if PortOpen then closehandle(iu_file)
  142. PortOpen=false
  143. goto ext
  144. end if
  145. //设置超时
  146. commtimeouts lst_to
  147. lst_to.readintervaltimeout = 4294967295 //MAXDWORD
  148. SetCommTimeouts(iu_file, lst_to)
  149. CommError=0
  150. ext:
  151. return PortOpen
  152. end function
  153. public subroutine of_close ();//uo_thread_pbcomm.ins_stopflag=1
  154. if PortOpen then
  155. closehandle(iu_file)
  156. PortOpen=false
  157. end if
  158. end subroutine
  159. public function string of_read ();if not PortOpen then
  160. messagebox("错误","端口未打开",StopSign!)
  161. return ""
  162. end if
  163. string ls_input_temp
  164. ib_lock=true //加锁
  165. ls_input_temp=input
  166. input=""
  167. inputlen=0
  168. ib_lock=false //解锁
  169. return ls_input_temp
  170. end function
  171. public subroutine of_setparent (window arg_parent);iw_parent = arg_parent
  172. end subroutine
  173. on uo_pbcomm.create
  174. call super::create
  175. TriggerEvent( this, "constructor" )
  176. end on
  177. on uo_pbcomm.destroy
  178. TriggerEvent( this, "destructor" )
  179. call super::destroy
  180. end on
  181. event constructor;PortOpen=false
  182. ib_lock=false
  183. SharedObjectRegister("uo_thread","pbcommobject") //将uo—thread对象注册为pbcommobject
  184. SharedObjectGet("pbcommobject",uo_thread_pbcomm) //用uo—thread引用共享对象uo_thread_pbcomm
  185. uo_thread_pbcomm.of_setparent(this)
  186. uo_thread_pbcomm.Post of_start()
  187. end event
  188. event destructor;of_close()
  189. SharedObjectUnRegister("pbcommobject")
  190. //destroy uo_thread_pbcomm
  191. end event