modbus-rtu.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * Copyright © 2001-2010 Stéphane Raimbault <stephane.raimbault@gmail.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <errno.h>
  20. #include <termios.h>
  21. #include <fcntl.h>
  22. #include <string.h>
  23. #include "modbus.h"
  24. #include "modbus-private.h"
  25. #include "modbus-rtu.h"
  26. #include "modbus-rtu-private.h"
  27. /* Table of CRC values for high-order byte */
  28. static const uint8_t table_crc_hi[] = {
  29. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
  30. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  31. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  32. 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  33. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
  34. 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
  35. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
  36. 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  37. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
  38. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
  39. 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
  40. 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  41. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
  42. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
  43. 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  44. 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
  45. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
  46. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  47. 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  48. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  49. 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
  50. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
  51. 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
  52. 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
  53. 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
  54. 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
  55. };
  56. /* Table of CRC values for low-order byte */
  57. static const uint8_t table_crc_lo[] = {
  58. 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
  59. 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
  60. 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
  61. 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
  62. 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
  63. 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
  64. 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
  65. 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
  66. 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
  67. 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
  68. 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
  69. 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
  70. 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
  71. 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
  72. 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
  73. 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
  74. 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
  75. 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
  76. 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
  77. 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
  78. 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
  79. 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
  80. 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
  81. 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
  82. 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
  83. 0x43, 0x83, 0x41, 0x81, 0x80, 0x40
  84. };
  85. static int _modbus_set_slave(modbus_t *ctx, int slave)
  86. {
  87. if (slave >= 1 && slave <= 247) {
  88. ctx->slave = slave;
  89. } else {
  90. errno = EINVAL;
  91. return -1;
  92. }
  93. return 0;
  94. }
  95. /* Builds a RTU request header */
  96. static int _modbus_rtu_build_request_basis(modbus_t *ctx, int function,
  97. int addr, int nb,
  98. uint8_t *req)
  99. {
  100. req[0] = ctx->slave;
  101. req[1] = function;
  102. req[2] = addr >> 8;
  103. req[3] = addr & 0x00ff;
  104. req[4] = nb >> 8;
  105. req[5] = nb & 0x00ff;
  106. return _MODBUS_RTU_PRESET_REQ_LENGTH;
  107. }
  108. /* Builds a RTU response header */
  109. static int _modbus_rtu_build_response_basis(sft_t *sft, uint8_t *rsp)
  110. {
  111. rsp[0] = sft->slave;
  112. rsp[1] = sft->function;
  113. return _MODBUS_RTU_PRESET_RSP_LENGTH;
  114. }
  115. static uint16_t crc16(uint8_t *buffer, uint16_t buffer_length)
  116. {
  117. uint8_t crc_hi = 0xFF; /* high CRC byte initialized */
  118. uint8_t crc_lo = 0xFF; /* low CRC byte initialized */
  119. unsigned int i; /* will index into CRC lookup */
  120. /* pass through message buffer */
  121. while (buffer_length--) {
  122. i = crc_hi ^ *buffer++; /* calculate the CRC */
  123. crc_hi = crc_lo ^ table_crc_hi[i];
  124. crc_lo = table_crc_lo[i];
  125. }
  126. return (crc_hi << 8 | crc_lo);
  127. }
  128. int _modbus_rtu_prepare_response_tid(const uint8_t *req, int *req_length)
  129. {
  130. (*req_length) -= _MODBUS_RTU_CHECKSUM_LENGTH;
  131. /* No TID */
  132. return 0;
  133. }
  134. int _modbus_rtu_send_msg_pre(uint8_t *req, int req_length)
  135. {
  136. uint16_t crc = crc16(req, req_length);
  137. req[req_length++] = crc >> 8;
  138. req[req_length++] = crc & 0x00FF;
  139. return req_length;
  140. }
  141. ssize_t _modbus_rtu_send(int s, const uint8_t *req, int req_length)
  142. {
  143. return write(s, req, req_length);
  144. }
  145. ssize_t _modbus_rtu_recv(int s, uint8_t *rsp, int rsp_length)
  146. {
  147. return read(s, rsp, rsp_length);
  148. }
  149. /* The check_crc16 function shall return the message length if the CRC is
  150. valid. Otherwise it shall return -1 and set errno to EMBADCRC. */
  151. int _modbus_rtu_check_integrity(modbus_t *ctx, uint8_t *msg,
  152. const int msg_length)
  153. {
  154. uint16_t crc_calculated;
  155. uint16_t crc_received;
  156. crc_calculated = crc16(msg, msg_length - 2);
  157. crc_received = (msg[msg_length - 2] << 8) | msg[msg_length - 1];
  158. /* Check CRC of msg */
  159. if (crc_calculated == crc_received) {
  160. return msg_length;
  161. } else {
  162. if (ctx->debug) {
  163. fprintf(stderr, "ERROR CRC received %0X != CRC calculated %0X\n",
  164. crc_received, crc_calculated);
  165. }
  166. if (ctx->error_recovery) {
  167. _modbus_rtu_flush(ctx);
  168. }
  169. errno = EMBBADCRC;
  170. return -1;
  171. }
  172. }
  173. /* Sets up a serial port for RTU communications */
  174. static int _modbus_rtu_connect(modbus_t *ctx)
  175. {
  176. struct termios tios;
  177. speed_t speed;
  178. modbus_rtu_t *ctx_rtu = ctx->backend_data;
  179. if (ctx->debug) {
  180. printf("Opening %s at %d bauds (%c, %d, %d)\n",
  181. ctx_rtu->device, ctx_rtu->baud, ctx_rtu->parity,
  182. ctx_rtu->data_bit, ctx_rtu->stop_bit);
  183. }
  184. /* The O_NOCTTY flag tells UNIX that this program doesn't want
  185. to be the "controlling terminal" for that port. If you
  186. don't specify this then any input (such as keyboard abort
  187. signals and so forth) will affect your process
  188. Timeouts are ignored in canonical input mode or when the
  189. NDELAY option is set on the file via open or fcntl */
  190. ctx->s = open(ctx_rtu->device, O_RDWR | O_NOCTTY | O_NDELAY | O_EXCL);
  191. if (ctx->s == -1) {
  192. fprintf(stderr, "ERROR Can't open the device %s (%s)\n",
  193. ctx_rtu->device, strerror(errno));
  194. return -1;
  195. }
  196. /* Save */
  197. tcgetattr(ctx->s, &(ctx_rtu->old_tios));
  198. memset(&tios, 0, sizeof(struct termios));
  199. /* C_ISPEED Input baud (new interface)
  200. C_OSPEED Output baud (new interface)
  201. */
  202. switch (ctx_rtu->baud) {
  203. case 110:
  204. speed = B110;
  205. break;
  206. case 300:
  207. speed = B300;
  208. break;
  209. case 600:
  210. speed = B600;
  211. break;
  212. case 1200:
  213. speed = B1200;
  214. break;
  215. case 2400:
  216. speed = B2400;
  217. break;
  218. case 4800:
  219. speed = B4800;
  220. break;
  221. case 9600:
  222. speed = B9600;
  223. break;
  224. case 19200:
  225. speed = B19200;
  226. break;
  227. case 38400:
  228. speed = B38400;
  229. break;
  230. case 57600:
  231. speed = B57600;
  232. break;
  233. case 115200:
  234. speed = B115200;
  235. break;
  236. default:
  237. speed = B9600;
  238. if (ctx->debug) {
  239. fprintf(stderr,
  240. "WARNING Unknown baud rate %d for %s (B9600 used)\n",
  241. ctx_rtu->baud, ctx_rtu->device);
  242. }
  243. }
  244. /* Set the baud rate */
  245. if ((cfsetispeed(&tios, speed) < 0) ||
  246. (cfsetospeed(&tios, speed) < 0)) {
  247. return -1;
  248. }
  249. /* C_CFLAG Control options
  250. CLOCAL Local line - do not change "owner" of port
  251. CREAD Enable receiver
  252. */
  253. tios.c_cflag |= (CREAD | CLOCAL);
  254. /* CSIZE, HUPCL, CRTSCTS (hardware flow control) */
  255. /* Set data bits (5, 6, 7, 8 bits)
  256. CSIZE Bit mask for data bits
  257. */
  258. tios.c_cflag &= ~CSIZE;
  259. switch (ctx_rtu->data_bit) {
  260. case 5:
  261. tios.c_cflag |= CS5;
  262. break;
  263. case 6:
  264. tios.c_cflag |= CS6;
  265. break;
  266. case 7:
  267. tios.c_cflag |= CS7;
  268. break;
  269. case 8:
  270. default:
  271. tios.c_cflag |= CS8;
  272. break;
  273. }
  274. /* Stop bit (1 or 2) */
  275. if (ctx_rtu->stop_bit == 1)
  276. tios.c_cflag &=~ CSTOPB;
  277. else /* 2 */
  278. tios.c_cflag |= CSTOPB;
  279. /* PARENB Enable parity bit
  280. PARODD Use odd parity instead of even */
  281. if (ctx_rtu->parity == 'N') {
  282. /* None */
  283. tios.c_cflag &=~ PARENB;
  284. } else if (ctx_rtu->parity == 'E') {
  285. /* Even */
  286. tios.c_cflag |= PARENB;
  287. tios.c_cflag &=~ PARODD;
  288. } else {
  289. /* Odd */
  290. tios.c_cflag |= PARENB;
  291. tios.c_cflag |= PARODD;
  292. }
  293. /* Read the man page of termios if you need more information. */
  294. /* This field isn't used on POSIX systems
  295. tios.c_line = 0;
  296. */
  297. /* C_LFLAG Line options
  298. ISIG Enable SIGINTR, SIGSUSP, SIGDSUSP, and SIGQUIT signals
  299. ICANON Enable canonical input (else raw)
  300. XCASE Map uppercase \lowercase (obsolete)
  301. ECHO Enable echoing of input characters
  302. ECHOE Echo erase character as BS-SP-BS
  303. ECHOK Echo NL after kill character
  304. ECHONL Echo NL
  305. NOFLSH Disable flushing of input buffers after
  306. interrupt or quit characters
  307. IEXTEN Enable extended functions
  308. ECHOCTL Echo control characters as ^char and delete as ~?
  309. ECHOPRT Echo erased character as character erased
  310. ECHOKE BS-SP-BS entire line on line kill
  311. FLUSHO Output being flushed
  312. PENDIN Retype pending input at next read or input char
  313. TOSTOP Send SIGTTOU for background output
  314. Canonical input is line-oriented. Input characters are put
  315. into a buffer which can be edited interactively by the user
  316. until a CR (carriage return) or LF (line feed) character is
  317. received.
  318. Raw input is unprocessed. Input characters are passed
  319. through exactly as they are received, when they are
  320. received. Generally you'll deselect the ICANON, ECHO,
  321. ECHOE, and ISIG options when using raw input
  322. */
  323. /* Raw input */
  324. tios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  325. /* C_IFLAG Input options
  326. Constant Description
  327. INPCK Enable parity check
  328. IGNPAR Ignore parity errors
  329. PARMRK Mark parity errors
  330. ISTRIP Strip parity bits
  331. IXON Enable software flow control (outgoing)
  332. IXOFF Enable software flow control (incoming)
  333. IXANY Allow any character to start flow again
  334. IGNBRK Ignore break condition
  335. BRKINT Send a SIGINT when a break condition is detected
  336. INLCR Map NL to CR
  337. IGNCR Ignore CR
  338. ICRNL Map CR to NL
  339. IUCLC Map uppercase to lowercase
  340. IMAXBEL Echo BEL on input line too long
  341. */
  342. if (ctx_rtu->parity == 'N') {
  343. /* None */
  344. tios.c_iflag &= ~INPCK;
  345. } else {
  346. tios.c_iflag |= INPCK;
  347. }
  348. /* Software flow control is disabled */
  349. tios.c_iflag &= ~(IXON | IXOFF | IXANY);
  350. /* C_OFLAG Output options
  351. OPOST Postprocess output (not set = raw output)
  352. ONLCR Map NL to CR-NL
  353. ONCLR ant others needs OPOST to be enabled
  354. */
  355. /* Raw ouput */
  356. tios.c_oflag &=~ OPOST;
  357. /* C_CC Control characters
  358. VMIN Minimum number of characters to read
  359. VTIME Time to wait for data (tenths of seconds)
  360. UNIX serial interface drivers provide the ability to
  361. specify character and packet timeouts. Two elements of the
  362. c_cc array are used for timeouts: VMIN and VTIME. Timeouts
  363. are ignored in canonical input mode or when the NDELAY
  364. option is set on the file via open or fcntl.
  365. VMIN specifies the minimum number of characters to read. If
  366. it is set to 0, then the VTIME value specifies the time to
  367. wait for every character read. Note that this does not mean
  368. that a read call for N bytes will wait for N characters to
  369. come in. Rather, the timeout will apply to the first
  370. character and the read call will return the number of
  371. characters immediately available (up to the number you
  372. request).
  373. If VMIN is non-zero, VTIME specifies the time to wait for
  374. the first character read. If a character is read within the
  375. time given, any read will block (wait) until all VMIN
  376. characters are read. That is, once the first character is
  377. read, the serial interface driver expects to receive an
  378. entire packet of characters (VMIN bytes total). If no
  379. character is read within the time allowed, then the call to
  380. read returns 0. This method allows you to tell the serial
  381. driver you need exactly N bytes and any read call will
  382. return 0 or N bytes. However, the timeout only applies to
  383. the first character read, so if for some reason the driver
  384. misses one character inside the N byte packet then the read
  385. call could block forever waiting for additional input
  386. characters.
  387. VTIME specifies the amount of time to wait for incoming
  388. characters in tenths of seconds. If VTIME is set to 0 (the
  389. default), reads will block (wait) indefinitely unless the
  390. NDELAY option is set on the port with open or fcntl.
  391. */
  392. /* Unused because we use open with the NDELAY option */
  393. tios.c_cc[VMIN] = 0;
  394. tios.c_cc[VTIME] = 0;
  395. if (tcsetattr(ctx->s, TCSANOW, &tios) < 0) {
  396. return -1;
  397. }
  398. return 0;
  399. }
  400. void _modbus_rtu_close(modbus_t *ctx)
  401. {
  402. /* Closes the file descriptor in RTU mode */
  403. modbus_rtu_t *ctx_rtu = ctx->backend_data;
  404. tcsetattr(ctx->s, TCSANOW, &(ctx_rtu->old_tios));
  405. close(ctx->s);
  406. }
  407. int _modbus_rtu_flush(modbus_t *ctx)
  408. {
  409. return tcflush(ctx->s, TCIOFLUSH);
  410. }
  411. int _modbus_rtu_listen(modbus_t *ctx, int nb_connection)
  412. {
  413. if (ctx->debug) {
  414. fprintf(stderr, "Not implemented");
  415. }
  416. errno = EINVAL;
  417. return -1;
  418. }
  419. int _modbus_rtu_accept(modbus_t *ctx, int *socket)
  420. {
  421. if (ctx->debug) {
  422. fprintf(stderr, "Not implemented");
  423. }
  424. errno = EINVAL;
  425. return -1;
  426. }
  427. int _modbus_rtu_filter_request(modbus_t *ctx, int slave)
  428. {
  429. /* Filter on the Modbus unit identifier (slave) in RTU mode */
  430. if (slave != ctx->slave && slave != MODBUS_BROADCAST_ADDRESS) {
  431. /* Ignores the request (not for me) */
  432. if (ctx->debug) {
  433. printf("Request for slave %d ignored (not %d)\n",
  434. slave, ctx->slave);
  435. }
  436. return 1;
  437. } else {
  438. return 0;
  439. }
  440. }
  441. const modbus_backend_t _modbus_rtu_backend = {
  442. _MODBUS_BACKEND_TYPE_RTU,
  443. _MODBUS_RTU_HEADER_LENGTH,
  444. _MODBUS_RTU_CHECKSUM_LENGTH,
  445. MODBUS_RTU_MAX_ADU_LENGTH,
  446. _modbus_set_slave,
  447. _modbus_rtu_build_request_basis,
  448. _modbus_rtu_build_response_basis,
  449. _modbus_rtu_prepare_response_tid,
  450. _modbus_rtu_send_msg_pre,
  451. _modbus_rtu_send,
  452. _modbus_rtu_recv,
  453. _modbus_rtu_check_integrity,
  454. _modbus_rtu_connect,
  455. _modbus_rtu_close,
  456. _modbus_rtu_flush,
  457. _modbus_rtu_listen,
  458. _modbus_rtu_accept,
  459. _modbus_rtu_filter_request
  460. };
  461. /* Allocate and initialize the modbus_t structure for RTU
  462. - device: "/dev/ttyS0"
  463. - baud: 9600, 19200, 57600, 115200, etc
  464. - parity: 'N' stands for None, 'E' for Even and 'O' for odd
  465. - data_bits: 5, 6, 7, 8
  466. - stop_bits: 1, 2
  467. - slave: slave number of the caller
  468. */
  469. modbus_t* modbus_new_rtu(const char *device,
  470. int baud, char parity, int data_bit,
  471. int stop_bit, int slave)
  472. {
  473. modbus_t *ctx;
  474. modbus_rtu_t *ctx_rtu;
  475. ctx = (modbus_t *) malloc(sizeof(modbus_t));
  476. _modbus_init_common(ctx);
  477. if (_modbus_set_slave(ctx, slave) == -1) {
  478. return NULL;
  479. }
  480. ctx->backend = &_modbus_rtu_backend;
  481. ctx->backend_data = (modbus_rtu_t *) malloc(sizeof(modbus_rtu_t));
  482. ctx_rtu = (modbus_rtu_t *)ctx->backend_data;
  483. #if defined(OpenBSD)
  484. strlcpy(ctx_rtu->device, device, sizeof(ctx_rtu->device));
  485. #else
  486. strcpy(ctx_rtu->device, device);
  487. #endif
  488. ctx_rtu->baud = baud;
  489. if (parity == 'N' || parity == 'E' || parity == 'O') {
  490. ctx_rtu->parity = parity;
  491. } else {
  492. errno = EINVAL;
  493. return NULL;
  494. }
  495. ctx_rtu->data_bit = data_bit;
  496. ctx_rtu->stop_bit = stop_bit;
  497. return ctx;
  498. }