serial.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. { Unit for handling the serial interfaces for Linux and similar Unices.
  2. (c) 2000 Sebastian Guenther, [email protected]; modified MarkMLl 2012.
  3. }
  4. unit Serial;
  5. {$MODE objfpc}
  6. {$H+}
  7. {$PACKRECORDS C}
  8. interface
  9. uses BaseUnix,termio,unix;
  10. type
  11. TSerialHandle = LongInt;
  12. TParityType = (NoneParity, OddParity, EvenParity);
  13. TSerialFlags = set of (RtsCtsFlowControl);
  14. TSerialState = record
  15. LineState: LongWord;
  16. tios: termios;
  17. end;
  18. { Open the serial device with the given device name, for example:
  19. /dev/ttyS0, /dev/ttyS1... for normal serial ports
  20. /dev/ttyI0, /dev/ttyI1... for ISDN emulated serial ports
  21. other device names are possible; refer to your OS documentation.
  22. Returns "0" if device could not be found }
  23. function SerOpen(const DeviceName: String): TSerialHandle;
  24. { Closes a serial device previously opened with SerOpen. }
  25. procedure SerClose(Handle: TSerialHandle);
  26. { Flushes the data queues of the given serial device. DO NOT USE THIS:
  27. use either SerSync (non-blocking) or SerDrain (blocking). }
  28. procedure SerFlush(Handle: TSerialHandle); deprecated;
  29. { Suggest to the kernel that buffered output data should be sent. This
  30. is unlikely to have a useful effect except possibly in the case of
  31. buggy ports that lose Tx interrupts, and is implemented as a preferred
  32. alternative to the deprecated SerFlush procedure. }
  33. procedure SerSync(Handle: TSerialHandle);
  34. { Wait until all buffered output has been transmitted. It is the caller's
  35. responsibility to ensure that this won't block permanently due to an
  36. inappropriate handshake state. }
  37. procedure SerDrain(Handle: TSerialHandle);
  38. { Discard all pending input. }
  39. procedure SerFlushInput(Handle: TSerialHandle);
  40. { Discard all unsent output. }
  41. procedure SerFlushOutput(Handle: TSerialHandle);
  42. { Reads a maximum of "Count" bytes of data into the specified buffer.
  43. Result: Number of bytes read. }
  44. function SerRead(Handle: TSerialHandle; var Buffer; Count: LongInt): LongInt;
  45. { Tries to write "Count" bytes from "Buffer".
  46. Result: Number of bytes written. }
  47. function SerWrite(Handle: TSerialHandle; var Buffer; Count: LongInt): LongInt;
  48. procedure SerSetParams(Handle: TSerialHandle; BitsPerSec: LongInt;
  49. ByteSize: Integer; Parity: TParityType; StopBits: Integer;
  50. Flags: TSerialFlags);
  51. { Saves and restores the state of the serial device. }
  52. function SerSaveState(Handle: TSerialHandle): TSerialState;
  53. procedure SerRestoreState(Handle: TSerialHandle; State: TSerialState);
  54. { Getting and setting the line states directly. }
  55. procedure SerSetDTR(Handle: TSerialHandle; State: Boolean);
  56. procedure SerSetRTS(Handle: TSerialHandle; State: Boolean);
  57. function SerGetCTS(Handle: TSerialHandle): Boolean;
  58. function SerGetDSR(Handle: TSerialHandle): Boolean;
  59. function SerGetCD(Handle: TSerialHandle): Boolean;
  60. function SerGetRI(Handle: TSerialHandle): Boolean;
  61. { Set a line break state. If the requested time is greater than zero this is in
  62. mSec, in the case of unix this is likely to be rounded up to a few hundred
  63. mSec and to increase by a comparable increment; on unix if the time is less
  64. than or equal to zero its absolute value will be passed directly to the
  65. operating system with implementation-specific effect. If the third parameter
  66. is omitted or true there will be an implicit call of SerDrain() before and
  67. after the break.
  68. NOTE THAT on Linux, the only reliable mSec parameter is zero which results in
  69. a break of around 250 mSec. Might be completely ineffective on Solaris.
  70. }
  71. procedure SerBreak(Handle: TSerialHandle; mSec: LongInt=0; sync: boolean= true);
  72. type TSerialIdle= procedure(h: TSerialHandle);
  73. { Set this to a shim around Application.ProcessMessages if calling SerReadTimeout(),
  74. SerBreak() etc. from the main thread so that it doesn't lock up a Lazarus app. }
  75. var SerialIdle: TSerialIdle= nil;
  76. { This is similar to SerRead() but adds a mSec timeout. Note that this variant
  77. returns as soon as a single byte is available, or as dictated by the timeout. }
  78. function SerReadTimeout(Handle: TSerialHandle; var Buffer; mSec: LongInt): LongInt;
  79. { This is similar to SerRead() but adds a mSec timeout. Note that this variant
  80. attempts to accumulate as many bytes as are available, but does not exceed
  81. the timeout. Set up a SerIdle callback if using this in a main thread in a
  82. Lazarus app. }
  83. function SerReadTimeout(Handle: TSerialHandle; var Buffer: array of byte; count, mSec: LongInt): LongInt;
  84. { ************************************************************************** }
  85. implementation
  86. function SerOpen(const DeviceName: String): TSerialHandle;
  87. begin
  88. Result := fpopen(DeviceName, O_RDWR or O_NOCTTY);
  89. end;
  90. procedure SerClose(Handle: TSerialHandle);
  91. begin
  92. fpClose(Handle);
  93. end;
  94. procedure SerFlush(Handle: TSerialHandle); deprecated;
  95. begin
  96. fpfsync(Handle);
  97. end;
  98. procedure SerSync(Handle: TSerialHandle);
  99. begin
  100. fpfsync(Handle)
  101. end;
  102. procedure SerDrain(Handle: TSerialHandle);
  103. begin
  104. tcdrain(Handle)
  105. end;
  106. procedure SerFlushInput(Handle: TSerialHandle);
  107. begin
  108. tcflush(Handle, TCIFLUSH)
  109. end;
  110. procedure SerFlushOutput(Handle: TSerialHandle);
  111. begin
  112. tcflush(Handle, TCOFLUSH)
  113. end;
  114. function SerRead(Handle: TSerialHandle; var Buffer; Count: LongInt): LongInt;
  115. begin
  116. Result := fpRead(Handle, Buffer, Count);
  117. end;
  118. function SerWrite(Handle: TSerialHandle; var Buffer; Count: LongInt): LongInt;
  119. begin
  120. Result := fpWrite(Handle, Buffer, Count);
  121. end;
  122. procedure SerSetParams(Handle: TSerialHandle; BitsPerSec: LongInt;
  123. ByteSize: Integer; Parity: TParityType; StopBits: Integer;
  124. Flags: TSerialFlags);
  125. var
  126. tios: termios;
  127. begin
  128. FillChar(tios, SizeOf(tios), #0);
  129. case BitsPerSec of
  130. 50: tios.c_cflag := B50;
  131. 75: tios.c_cflag := B75;
  132. 110: tios.c_cflag := B110;
  133. 134: tios.c_cflag := B134;
  134. 150: tios.c_cflag := B150;
  135. 200: tios.c_cflag := B200;
  136. 300: tios.c_cflag := B300;
  137. 600: tios.c_cflag := B600;
  138. 1200: tios.c_cflag := B1200;
  139. 1800: tios.c_cflag := B1800;
  140. 2400: tios.c_cflag := B2400;
  141. 4800: tios.c_cflag := B4800;
  142. 19200: tios.c_cflag := B19200;
  143. 38400: tios.c_cflag := B38400;
  144. 57600: tios.c_cflag := B57600;
  145. 115200: tios.c_cflag := B115200;
  146. 230400: tios.c_cflag := B230400;
  147. {$ifndef BSD}
  148. 460800: tios.c_cflag := B460800;
  149. {$endif}
  150. else tios.c_cflag := B9600;
  151. end;
  152. {$ifndef SOLARIS}
  153. tios.c_ispeed := tios.c_cflag;
  154. tios.c_ospeed := tios.c_ispeed;
  155. {$endif}
  156. tios.c_cflag := tios.c_cflag or CREAD or CLOCAL;
  157. case ByteSize of
  158. 5: tios.c_cflag := tios.c_cflag or CS5;
  159. 6: tios.c_cflag := tios.c_cflag or CS6;
  160. 7: tios.c_cflag := tios.c_cflag or CS7;
  161. else tios.c_cflag := tios.c_cflag or CS8;
  162. end;
  163. case Parity of
  164. OddParity: tios.c_cflag := tios.c_cflag or PARENB or PARODD;
  165. EvenParity: tios.c_cflag := tios.c_cflag or PARENB;
  166. end;
  167. if StopBits = 2 then
  168. tios.c_cflag := tios.c_cflag or CSTOPB;
  169. if RtsCtsFlowControl in Flags then
  170. tios.c_cflag := tios.c_cflag or CRTSCTS;
  171. tcflush(Handle, TCIOFLUSH);
  172. tcsetattr(Handle, TCSANOW, tios)
  173. end;
  174. function SerSaveState(Handle: TSerialHandle): TSerialState;
  175. begin
  176. fpioctl(Handle, TIOCMGET, @Result.LineState);
  177. // fpioctl(Handle, TCGETS, @Result.tios);
  178. TcGetAttr(handle,result.tios);
  179. end;
  180. procedure SerRestoreState(Handle: TSerialHandle; State: TSerialState);
  181. begin
  182. // fpioctl(Handle, TCSETS, @State.tios);
  183. TCSetAttr(handle,TCSANOW,State.tios);
  184. fpioctl(Handle, TIOCMSET, @State.LineState);
  185. end;
  186. procedure SerSetDTR(Handle: TSerialHandle; State: Boolean);
  187. const
  188. DTR: Cardinal = TIOCM_DTR;
  189. begin
  190. if State then
  191. fpioctl(Handle, TIOCMBIS, @DTR)
  192. else
  193. fpioctl(Handle, TIOCMBIC, @DTR);
  194. end;
  195. procedure SerSetRTS(Handle: TSerialHandle; State: Boolean);
  196. const
  197. RTS: Cardinal = TIOCM_RTS;
  198. begin
  199. if State then
  200. fpioctl(Handle, TIOCMBIS, @RTS)
  201. else
  202. fpioctl(Handle, TIOCMBIC, @RTS);
  203. end;
  204. function SerGetCTS(Handle: TSerialHandle): Boolean;
  205. var
  206. Flags: Cardinal;
  207. begin
  208. fpioctl(Handle, TIOCMGET, @Flags);
  209. Result := (Flags and TIOCM_CTS) <> 0;
  210. end;
  211. function SerGetDSR(Handle: TSerialHandle): Boolean;
  212. var
  213. Flags: Cardinal;
  214. begin
  215. fpioctl(Handle, TIOCMGET, @Flags);
  216. Result := (Flags and TIOCM_DSR) <> 0;
  217. end;
  218. function SerGetCD(Handle: TSerialHandle): Boolean;
  219. var
  220. Flags: Cardinal;
  221. begin
  222. fpioctl(Handle, TIOCMGET, @Flags);
  223. Result := (Flags and TIOCM_CD) <> 0
  224. end;
  225. function SerGetRI(Handle: TSerialHandle): Boolean;
  226. var
  227. Flags: Cardinal;
  228. begin
  229. fpioctl(Handle, TIOCMGET, @Flags);
  230. Result := (Flags and TIOCM_RI) <> 0;
  231. end;
  232. procedure SerBreak(Handle: TSerialHandle; mSec: LongInt= 0; sync: boolean= true);
  233. begin
  234. if sync then
  235. tcdrain(Handle);
  236. if mSec <= 0 then
  237. tcsendbreak(Handle, Abs(mSec))
  238. else
  239. tcsendbreak(Handle, Trunc(mSec / 250));
  240. if sync then
  241. tcdrain(Handle)
  242. end;
  243. function SerReadTimeout(Handle: TSerialHandle; var Buffer; mSec: LongInt): LongInt;
  244. VAR readSet: TFDSet;
  245. selectTimeout: TTimeVal;
  246. begin
  247. fpFD_ZERO(readSet);
  248. fpFD_SET(Handle, readSet);
  249. selectTimeout.tv_sec := mSec div 1000;
  250. selectTimeout.tv_usec := (mSec mod 1000) * 1000;
  251. result := 0;
  252. if fpSelect(Handle + 1, @readSet, nil, nil, @selectTimeout) > 0 then
  253. result := fpRead(Handle, Buffer, 1)
  254. end { SerReadTimeout } ;
  255. {$ifdef LINUX}
  256. {$define SELECT_UPDATES_TIMEOUT}
  257. {$endif}
  258. {$ifdef SELECT_UPDATES_TIMEOUT}
  259. function SerReadTimeout(Handle: TSerialHandle; var Buffer: array of byte; count, mSec: LongInt): LongInt;
  260. VAR readSet: TFDSet;
  261. selectTimeout: TTimeVal;
  262. begin
  263. fpFD_ZERO(readSet);
  264. fpFD_SET(Handle, readSet);
  265. selectTimeout.tv_sec := mSec div 1000;
  266. selectTimeout.tv_usec := (mSec mod 1000) * 1000;
  267. result := 0;
  268. // Note: this variant of fpSelect() is a thin wrapper around the kernel's syscall.
  269. // In the case of Linux the syscall DOES update the timeout parameter.
  270. while fpSelect(Handle + 1, @readSet, nil, nil, @selectTimeout) > 0 do begin
  271. Inc(result,fpRead(Handle, Buffer[result], count - result));
  272. if result >= count then
  273. break;
  274. if Assigned(SerialIdle) then
  275. SerialIdle(Handle)
  276. end
  277. end { SerReadTimeout } ;
  278. {$else}
  279. function SerReadTimeout(Handle: TSerialHandle; var Buffer: array of byte; count, mSec: LongInt): LongInt;
  280. VAR readSet: TFDSet;
  281. selectTimeout: TTimeVal;
  282. uSecOnEntry, uSecElapsed: QWord;
  283. function now64uSec: QWord;
  284. var tv: timeval;
  285. begin
  286. fpgettimeofday(@tv, nil);
  287. result := tv.tv_sec * 1000000 + tv.tv_usec
  288. end { now64uSec } ;
  289. begin
  290. fpFD_ZERO(readSet);
  291. fpFD_SET(Handle, readSet);
  292. selectTimeout.tv_sec := mSec div 1000;
  293. selectTimeout.tv_usec := (mSec mod 1000) * 1000;
  294. result := 0;
  295. uSecOnEntry := now64uSec;
  296. // Note: this variant of fpSelect() is a thin wrapper around the kernel's syscall.
  297. // In the case of Solaris the syscall DOES NOT update the timeout parameter.
  298. while fpSelect(Handle + 1, @readSet, nil, nil, @selectTimeout) > 0 do begin
  299. Inc(result,fpRead(Handle, Buffer[result], count - result));
  300. uSecElapsed := now64uSec - uSecOnEntry;
  301. if (result >= count) or (uSecElapsed >= mSec * 1000) then
  302. break;
  303. selectTimeout.tv_sec := (mSec * 1000 - uSecElapsed) div 1000000;
  304. selectTimeout.tv_usec := (mSec * 1000 - uSecElapsed) mod 1000000;
  305. if Assigned(SerialIdle) then
  306. SerialIdle(Handle)
  307. end
  308. end { SerReadTimeout } ;
  309. {$endif}
  310. end.