serial.pp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. { $Id$
  2. Unit for handling the serial interfaces under Linux.
  3. (c) 2000 Sebastian Guenther, [email protected]
  4. }
  5. unit Serial;
  6. {$PACKRECORDS C}
  7. interface
  8. uses Linux;
  9. type
  10. TSerialHandle = LongInt;
  11. TParityType = (NoneParity, OddParity, EvenParity);
  12. TSerialFlags = set of (RtsCtsFlowControl);
  13. TSerialState = record
  14. LineState: LongWord;
  15. tios: termios;
  16. end;
  17. { Open the serial device with the given device name, for example:
  18. /dev/ttyS0, /dev/ttyS1... for normal serial ports
  19. /dev/ttyI0, /dev/ttyI1... for ISDN emulated serial ports
  20. other device names are possible; refer to your Linux documentation.
  21. Returns "0" if device could not be found }
  22. function SerOpen(const DeviceName: PChar): TSerialHandle;
  23. { Closes a serial device previously opened with SerOpen. }
  24. procedure SerClose(Handle: TSerialHandle);
  25. { Flushes the data queues of the given serial device. }
  26. procedure SerFlush(Handle: TSerialHandle);
  27. { Reads a maximum of "Count" bytes of data into the specified buffer.
  28. Result: Number of bytes read. }
  29. function SerRead(Handle: TSerialHandle; var Buffer; Count: LongInt): LongInt;
  30. { Tries to write "Count" bytes from "Buffer".
  31. Result: Number of bytes written. }
  32. function SerWrite(Handle: TSerialHandle; var Buffer; Count: LongInt): LongInt;
  33. procedure SerSetParams(Handle: TSerialHandle; BitsPerSec: LongInt;
  34. ByteSize: LongInt; Parity: TParityType; StopBits: LongInt;
  35. Flags: TSerialFlags);
  36. { Saves and restores the state of the serial device. }
  37. function SerSaveState(Handle: TSerialHandle): TSerialState;
  38. procedure SerRestoreState(Handle: TSerialHandle; State: TSerialState);
  39. { Getting and setting the line states directly. }
  40. procedure SerSetDTR(Handle: TSerialHandle; State: Boolean);
  41. procedure SerSetRTS(Handle: TSerialHandle; State: Boolean);
  42. function SerGetCTS(Handle: TSerialHandle): Boolean;
  43. function SerGetDSR(Handle: TSerialHandle): Boolean;
  44. function SerGetRI(Handle: TSerialHandle): Boolean;
  45. { ************************************************************************** }
  46. implementation
  47. function SerOpen(const DeviceName: PChar): TSerialHandle;
  48. begin
  49. SerOpen := fdOpen(DeviceName, OPEN_RDWR or OPEN_EXCL or OPEN_NOCTTY);
  50. end;
  51. procedure SerClose(Handle: TSerialHandle);
  52. begin
  53. fdClose(Handle);
  54. end;
  55. procedure SerFlush(Handle: TSerialHandle);
  56. begin
  57. fdFlush(Handle);
  58. end;
  59. function SerRead(Handle: TSerialHandle; var Buffer; Count: LongInt): LongInt;
  60. begin
  61. SerRead := fdRead(Handle, Buffer, Count);
  62. end;
  63. function SerWrite(Handle: TSerialHandle; var Buffer; Count: LongInt): LongInt;
  64. begin
  65. SerWrite := fdWrite(Handle, Buffer, Count);
  66. end;
  67. procedure SerSetParams(Handle: TSerialHandle; BitsPerSec: LongInt;
  68. ByteSize: LongInt; Parity: TParityType; StopBits: LongInt;
  69. Flags: TSerialFlags);
  70. var
  71. tios: termios;
  72. begin
  73. ioctl(Handle, TCGETS, @tios);
  74. case BitsPerSec of
  75. 50: tios.c_cflag := B50;
  76. 75: tios.c_cflag := B75;
  77. 110: tios.c_cflag := B110;
  78. 134: tios.c_cflag := B134;
  79. 150: tios.c_cflag := B150;
  80. 200: tios.c_cflag := B200;
  81. 300: tios.c_cflag := B300;
  82. 600: tios.c_cflag := B600;
  83. 1200: tios.c_cflag := B1200;
  84. 1800: tios.c_cflag := B1800;
  85. 2400: tios.c_cflag := B2400;
  86. 4800: tios.c_cflag := B4800;
  87. 19200: tios.c_cflag := B19200;
  88. 38400: tios.c_cflag := B38400;
  89. 57600: tios.c_cflag := B57600;
  90. 115200: tios.c_cflag := B115200;
  91. 230400: tios.c_cflag := B230400;
  92. 460800: tios.c_cflag := B460800;
  93. else tios.c_cflag := B9600;
  94. end;
  95. case ByteSize of
  96. 5: tios.c_cflag := tios.c_cflag or CS5;
  97. 6: tios.c_cflag := tios.c_cflag or CS6;
  98. 7: tios.c_cflag := tios.c_cflag or CS7;
  99. else tios.c_cflag := tios.c_cflag or CS8;
  100. end;
  101. case Parity of
  102. OddParity: tios.c_cflag := tios.c_cflag or PARENB or PARODD;
  103. EvenParity: tios.c_cflag := tios.c_cflag or PARENB;
  104. end;
  105. if StopBits = 2 then
  106. tios.c_cflag := tios.c_cflag or CSTOPB;
  107. if RtsCtsFlowControl in Flags then
  108. tios.c_cflag := tios.c_cflag or CRTSCTS;
  109. tios.c_cflag := tios.c_cflag or CLOCAL or CREAD;
  110. ioctl(Handle, TCSETS, @tios);
  111. end;
  112. function SerSaveState(Handle: TSerialHandle): TSerialState;
  113. var
  114. Result: TSerialState;
  115. begin
  116. ioctl(Handle, TIOCMGET, @Result.LineState);
  117. ioctl(Handle, TCGETS, @Result.tios);
  118. SerSaveState := Result;
  119. end;
  120. procedure SerRestoreState(Handle: TSerialHandle; State: TSerialState);
  121. begin
  122. ioctl(Handle, TCSETS, @State.tios);
  123. ioctl(Handle, TIOCMSET, @State.LineState);
  124. end;
  125. procedure SerSetDTR(Handle: TSerialHandle; State: Boolean);
  126. const
  127. DTR: Cardinal = TIOCM_DTR;
  128. begin
  129. if State then
  130. ioctl(Handle, TIOCMBIS, @DTR)
  131. else
  132. ioctl(Handle, TIOCMBIC, @DTR);
  133. end;
  134. procedure SerSetRTS(Handle: TSerialHandle; State: Boolean);
  135. const
  136. RTS: Cardinal = TIOCM_RTS;
  137. begin
  138. if State then
  139. ioctl(Handle, TIOCMBIS, @RTS)
  140. else
  141. ioctl(Handle, TIOCMBIC, @RTS);
  142. end;
  143. function SerGetCTS(Handle: TSerialHandle): Boolean;
  144. var
  145. Flags: Cardinal;
  146. begin
  147. ioctl(Handle, TIOCMGET, @Flags);
  148. SerGetCTS := (Flags and TIOCM_CTS) <> 0;
  149. end;
  150. function SerGetDSR(Handle: TSerialHandle): Boolean;
  151. var
  152. Flags: Cardinal;
  153. begin
  154. ioctl(Handle, TIOCMGET, @Flags);
  155. SerGetDSR := (Flags and TIOCM_DSR) <> 0;
  156. end;
  157. function SerGetRI(Handle: TSerialHandle): Boolean;
  158. var
  159. Flags: Cardinal;
  160. begin
  161. ioctl(Handle, TIOCMGET, @Flags);
  162. SerGetRI := (Flags and TIOCM_RI) <> 0;
  163. end;
  164. end.
  165. {
  166. $Log$
  167. Revision 1.3 2000-07-10 07:04:18 sg
  168. * Now compiles in default mode instead of OBJFPC mode
  169. Revision 1.2 2000/07/10 07:00:20 sg
  170. * Added ID and Log tags
  171. }