msousa@0: /* msousa@0: * Copyright (c) 2002,2016 Mario de Sousa (msousa@fe.up.pt) msousa@0: * msousa@0: * This file is part of the Modbus library for Beremiz and matiec. msousa@0: * msousa@0: * This Modbus library is free software: you can redistribute it and/or modify msousa@0: * it under the terms of the GNU Lesser General Public License as published by msousa@0: * the Free Software Foundation, either version 3 of the License, or msousa@0: * (at your option) any later version. msousa@0: * msousa@0: * This program is distributed in the hope that it will be useful, but msousa@0: * WITHOUT ANY WARRANTY; without even the implied warranty of msousa@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser msousa@0: * General Public License for more details. msousa@0: * msousa@0: * You should have received a copy of the GNU Lesser General Public License msousa@0: * along with this Modbus library. If not, see . msousa@0: * msousa@0: * This code is made available on the understanding that it will not be msousa@0: * used in safety-critical situations without a full and competent review. msousa@0: */ msousa@0: msousa@0: msousa@0: msousa@0: msousa@0: /* write a modbus frame */ msousa@0: /* WARNING: when calling this function, the *frame_data buffer msousa@0: * must be allocated with an extra *extra_bytes msousa@0: * beyond those required for the frame_length. msousa@0: * This is because the extra bytes will be used msousa@0: * to store the crc before sending the frame. msousa@0: * msousa@0: * The *extra_bytes value will be returned by the msousa@0: * modbus_init() function call. msousa@0: */ msousa@0: /* NOTE: calling this function will flush the input stream, msousa@0: * which means any frames that may have arrived msousa@0: * but have not yet been read using modbus_read() msousa@0: * will be permanently lost... msousa@0: */ msousa@0: int modbus_write(int nd, msousa@0: u8 *frame_data, msousa@0: size_t frame_length, msousa@0: u16 transaction_id, msousa@0: const struct timespec *transmit_timeout msousa@0: ); msousa@0: msousa@0: /* read a modbus frame */ msousa@0: /* msousa@0: * The frame is read from: msousa@0: * - the node descriptor nd, if nd >= 0 msousa@0: * - any valid and initialised node descriptor, if nd = -1 msousa@0: * In this case, the node where the data is eventually read from msousa@0: * is returned in *nd. msousa@0: * NOTE: (only avaliable if using TCP) msousa@0: */ msousa@0: /* NOTE: calling modbus_write() will flush the input stream, msousa@0: * which means any frames that may have arrived msousa@0: * but have not yet been read using modbus_read() msousa@0: * will be permanently lost... msousa@0: * msousa@0: * NOTE: Ususal select semantics for (a: recv_timeout == NULL) and msousa@0: * (b: *recv_timeout == 0) also apply. msousa@0: * (a) Indefinite timeout msousa@0: * (b) Try once, and and quit if no data available. msousa@0: */ msousa@0: /* NOTE: send_data and send_length is used to pass to the modbus_read() function msousa@0: * the frame that was previously sent over the same connection (node). msousa@0: * This data is then allows the modbus_read() function to ignore any msousa@0: * data that is read but identical to the previously sent data. This msousa@0: * is used when using serial ports that echoes back all the data that is msousa@0: * sent out over the same serial port. When using some RS232 to RS485 msousa@0: * converters, this functionality is essential as not all these converters msousa@0: * are capable of not echoing back the sent data. msousa@0: * These parameters are ignored when using TCP! msousa@0: */ msousa@0: msousa@0: /* RETURNS: number of bytes read msousa@0: * -1 on read from file/node error msousa@0: * -2 on timeout msousa@0: */ msousa@0: int modbus_read(int *nd, /* node descriptor */ msousa@0: u8 **recv_data_ptr, msousa@0: u16 *transaction_id, msousa@0: const u8 *send_data, msousa@0: int send_length, msousa@0: const struct timespec *recv_timeout); msousa@0: msousa@0: msousa@0: /* init the library */ msousa@0: int modbus_init(int nd_count, /* maximum number of nodes... */ msousa@0: optimization_t opt, msousa@0: int *extra_bytes); msousa@0: msousa@0: /* shutdown the library...*/ msousa@0: int modbus_done(void); msousa@0: msousa@0: msousa@0: /* Open a node for master / slave operation. msousa@0: * Returns the node descriptor, or -1 on error. msousa@0: */ msousa@0: int modbus_connect(node_addr_t node_addr); msousa@0: int modbus_listen(node_addr_t node_addr); msousa@0: msousa@0: /* Close a node, needs a node descriptor as argument... */ msousa@0: int modbus_close(int nd); msousa@0: msousa@0: /* Tell the library that the user will probably not be communicating msousa@0: * for some time... msousa@0: * This will allow the library to release any resources it will not msousa@0: * be needing during the silence. msousa@0: * NOTE: This is onlyused by the TCP version to close down tcp connections msousa@0: * when the silence will going to be longer than second. msousa@0: */ msousa@0: int modbus_silence_init(void); msousa@0: msousa@0: /* determine the minmum acceptable timeout... */ msousa@0: /* NOTE: timeout values passed to modbus_read() lower than the value returned msousa@0: * by this function may result in frames being aborted midway, since they msousa@0: * take at least modbus_get_min_timeout() seconds to transmit. msousa@0: */ msousa@0: double modbus_get_min_timeout(int baud, msousa@0: int parity, msousa@0: int data_bits, msousa@0: int stop_bits); msousa@0: msousa@0: msousa@0: msousa@0: msousa@0: msousa@0: msousa@0: msousa@0: msousa@0: msousa@0: