Add modbus module (currently not work)

This commit is contained in:
2022-03-26 20:31:51 +03:00
parent 4746b6269c
commit cf049a6101
11 changed files with 621 additions and 27 deletions

View File

@@ -66,6 +66,18 @@ typedef enum {
START, STOP
} OptStates;
extern OptStates opt_state;
extern uint16_t modbus_cnt;
#define DEVICE_CONTROL 0x01
#define DEVICE_CNT modbus_cnt
#define DEVICE_MIN_RECEIVE_PULSES 5
#define DEVICE_MAX_LOST_PULSES 20
#define DEVICE_MIN_PULSE_WIDTH 250
#define DEVICE_MAX_PULSE_WIDTH 750
typedef struct {} ModbusRegistersStruct;
extern ModbusRegistersStruct ModbusRegisters;
/* USER CODE END Private defines */
#ifdef __cplusplus

36
Core/Inc/modbus_crc.h Normal file
View File

@@ -0,0 +1,36 @@
/*
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* File: $Id: mbcrc.h,v 1.5 2006/12/07 22:10:34 wolti Exp $
*/
#ifndef _MB_CRC_H
#define _MB_CRC_H
CRC_t usMBCRC16( UCHAR * pucFrame, USHORT usLen );
#endif

84
Core/Inc/modbus_lib.h Normal file
View File

@@ -0,0 +1,84 @@
#define MODBUS_LIB_MAX_BUFFER 256
/* ----------------------- Defines ------------------------------------------*/
#define MB_ADDRESS_BROADCAST ( 0 ) /*! Modbus broadcast address. */
#define MB_ADDRESS_MIN ( 1 ) /*! Smallest possible slave address. */
#define MB_ADDRESS_MAX ( 247 ) /*! Biggest possible slave address. */
#define MB_FUNC_NONE ( 0 )
#define MB_FUNC_READ_COILS ( 1 )
#define MB_FUNC_READ_DISCRETE_INPUTS ( 2 )
#define MB_FUNC_WRITE_SINGLE_COIL ( 5 )
#define MB_FUNC_WRITE_MULTIPLE_COILS ( 15 )
#define MB_FUNC_READ_HOLDING_REGISTERS ( 3 )
#define MB_FUNC_READ_INPUT_REGISTER ( 4 )
#define MB_FUNC_WRITE_REGISTER ( 6 )
#define MB_FUNC_WRITE_MULTIPLE_REGISTERS ( 16 )
#define MB_FUNC_READWRITE_MULTIPLE_REGISTERS ( 23 )
#define MB_FUNC_DIAG_READ_EXCEPTION ( 7 )
#define MB_FUNC_DIAG_DIAGNOSTIC ( 8 )
#define MB_FUNC_DIAG_GET_COM_EVENT_CNT ( 11 )
#define MB_FUNC_DIAG_GET_COM_EVENT_LOG ( 12 )
#define MB_FUNC_OTHER_REPORT_SLAVEID ( 17 )
#define MB_FUNC_ERROR ( 128 )
/*----------------------- Response types ------------------------------*/
#define MBUS_RESPONSE_OK 0x00
#define MBUS_RESPONSE_NONE 0xFF
/* MBUS_RESPONSE_ILLEGAL_FUNCTION
The function code received in the query is not an allowable action
for the server. This may be because the function code is only
applicable to newerdevices, and was not implemented in the unit
selected. It could also indicate that the serveris in the wrong state to
process a request of this type, for example because it is
unconfigured and is being asked to return register values.
*/
#define MBUS_RESPONSE_ILLEGAL_FUNCTION 0x01
/* MBUS_RESPONSE_ILLEGAL_DATA_ADDRESS
The data address received in the query is not an allowable address
for the server. More specifically, the combination of reference number
and transfer length is invalid. For a controller with 100 registers,
the PDU addresses the first register as 0, and the last one as 99. If
a request is submitted with a starting register address of 96 and a
quantity of registers of 4, then this request will successfully
operate (address-wise at least) on registers 96, 97, 98, 99. If
a request is submitted with a starting register address of 96 and
a quantity of registers of 5, then this request will fail with
Exception Code 0x02 “Illegal Data Address” since it attempts to
operate on registers 96, 97, 98, 99 and 100, and there is no
register with address 100.
*/
#define MBUS_RESPONSE_ILLEGAL_DATA_ADDRESS 0x02
/* A value contained in the query data field is not an allowable value
for server. This indicates a fault in the structure of the remainder of
a complex request, such as that the implied length is
incorrect. It specifically does NOT mean that a data item submitted for
storage in a register has a value outside the expectation of the application
program, since the MODBUS protocol is unaware of the significance of
any particular val ue of any particular register.
*/
#define MBUS_RESPONSE_ILLEGAL_DATA_VALUE 0x03
/*
An unrecoverable error occurred while the server
was attempting to perform the requested action.
*/
#define MBUS_RESPONSE_SERVICE_DEVICE_FAILURE 0x04
#define MB_ADDRESS_HOLDING_REGISTER_OFFSET (40001)
typedef struct ModbusConfig_t{
uint16_t address;
} ModbusConfig_t;
extern uint8_t g_modbus_lib_received_telegram[MODBUS_LIB_MAX_BUFFER];
extern uint16_t g_modbus_lib_received_length;
extern int modbus_lib_init(ModbusConfig_t*);
extern void modbus_lib_append_data(uint8_t);
extern void modbus_lib_end_of_telegram(void);
extern uint16_t modbus_lib_send_error(int error_code);
extern int modbus_lib_transport_write(uint8_t* buffer, uint16_t length);
extern uint16_t modbus_lib_read_handler(uint16_t la);
extern uint16_t modbus_lib_write_handler(uint16_t la, uint16_t value);

View File

@@ -0,0 +1,10 @@
#ifndef _MODBUS_LIB_H_
#define _MODBUS_LIB_H_
#include <stdint.h>
/* ----------------------- Defines ------------------------------------------*/
#define MODBUS_LIB_MIN_TELEGRAM_SIZE 4 /*!< Minimum size of a Modbus RTU frame. */
#endif // _MODBUS_LIB_H_

21
Core/Inc/port.h Normal file
View File

@@ -0,0 +1,21 @@
#include <stdint.h>
typedef unsigned char BOOL;
typedef unsigned char UCHAR;
typedef char CHAR;
typedef unsigned int USHORT;
typedef int SHORT;
typedef unsigned long ULONG;
typedef long LONG;
typedef union {
struct {
uint8_t low;
uint8_t high;
} bytes;
uint16_t value;
} CRC_t, MbDataField;