├── Modbus ├── Modbus.cpp ├── Modbus.h └── keywords.txt ├── ModbusIP_ENC28J60_STM32 ├── ModbusIP_ENC28J60_STM.cpp ├── ModbusIP_ENC28J60_STM.h ├── examples │ └── modbus_IP_STM_example │ │ └── modbus_IP_STM_example.ino └── keywords.txt └── README.md /Modbus/Modbus.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlexPuts/Modbus-TCP-for-stm32-blue-pill/1d4583d817b66b250353e5317ea37198f3b8db9b/Modbus/Modbus.cpp -------------------------------------------------------------------------------- /Modbus/Modbus.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AlexPuts/Modbus-TCP-for-stm32-blue-pill/1d4583d817b66b250353e5317ea37198f3b8db9b/Modbus/Modbus.h -------------------------------------------------------------------------------- /Modbus/keywords.txt: -------------------------------------------------------------------------------- 1 | # Syntax Coloring Map For Modbus 2 | 3 | # Datatypes (KEYWORD1) 4 | Modbus KEYWORD1 5 | u_int KEYWORD1 6 | TRegister KEYWORD1 7 | 8 | # Methods and Functions (KEYWORD2) 9 | readCoils KEYWORD2 10 | readInputStatus KEYWORD2 11 | readRegisters KEYWORD2 12 | readInputRegisters KEYWORD2 13 | writeSingleCoil KEYWORD2 14 | writeSingleRegister KEYWORD2 15 | writeMultipleCoils KEYWORD2 16 | writeMultipleRegisters KEYWORD2 17 | searchRegister KEYWORD2 18 | receivePDU KEYWORD2 19 | addReg KEYWORD2 20 | addCoil KEYWORD2 21 | addIsts KEYWORD2 22 | addIreg KEYWORD2 23 | addHreg KEYWORD2 24 | Reg KEYWORD2 25 | Coil KEYWORD2 26 | Ists KEYWORD2 27 | Ireg KEYWORD2 28 | Hreg KEYWORD2 29 | 30 | # Constants (LITERAL1) 31 | MB_FC_READ_COILS LITERAL1 32 | MB_FC_READ_INPUTS LITERAL1 33 | MB_FC_READ_REGS LITERAL1 34 | MB_FC_READ_INPUT_STAT LITERAL1 35 | MB_FC_WRITE_COIL LITERAL1 36 | MB_FC_WRITE_REG LITERAL1 37 | MB_FC_WRITE_COILS LITERAL1 38 | MB_FC_WRITE_REGS LITERAL1 39 | MB_REPLY_OFF LITERAL1 40 | MB_REPLY_ECHO LITERAL1 41 | MB_REPLY_NORMAL LITERAL1 42 | -------------------------------------------------------------------------------- /ModbusIP_ENC28J60_STM32/ModbusIP_ENC28J60_STM.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | ModbusIP_ENC28J60.cpp - Source for Modbus IP ENC28J60 Library 3 | Copyright (C) 2015 André Sarmento Barbosa 4 | */ 5 | #include "ModbusIP_ENC28J60_STM.h" 6 | 7 | byte Ethernet::buffer[MODBUSIP_MAXFRAME]; 8 | 9 | ModbusIP::ModbusIP() { 10 | ether.hisport = MODBUSIP_PORT; 11 | } 12 | 13 | void ModbusIP::config(uint8_t *mac) { 14 | ether.begin(sizeof Ethernet::buffer, mac, ENC28J60_CS); 15 | ether.dhcpSetup(); 16 | } 17 | 18 | void ModbusIP::config(uint8_t *mac, uint8_t * ip) { 19 | ether.begin(sizeof Ethernet::buffer, mac, ENC28J60_CS); 20 | ether.staticSetup(ip); 21 | } 22 | 23 | void ModbusIP::config(uint8_t *mac, uint8_t * ip, uint8_t * dns) { 24 | ether.begin(sizeof Ethernet::buffer, mac, ENC28J60_CS); 25 | ether.staticSetup(ip, 0, dns); 26 | } 27 | 28 | void ModbusIP::config(uint8_t *mac, uint8_t * ip, uint8_t * dns, uint8_t * gateway) { 29 | ether.begin(sizeof Ethernet::buffer, mac, ENC28J60_CS); 30 | ether.staticSetup(ip, gateway, dns); 31 | } 32 | 33 | void ModbusIP::config(uint8_t *mac, uint8_t * ip, uint8_t * dns, uint8_t * gateway, uint8_t * subnet) { 34 | ether.begin(sizeof Ethernet::buffer, mac, ENC28J60_CS); 35 | ether.staticSetup(ip, gateway, dns, subnet); 36 | } 37 | 38 | void ModbusIP::task() { 39 | word len = ether.packetReceive(); 40 | word pos = ether.packetLoop(len); 41 | 42 | if (pos) { 43 | int i = 0; 44 | while (i < 7) { 45 | _MBAP[i] = Ethernet::buffer[pos+i]; 46 | i++; 47 | } 48 | 49 | _len = _MBAP[4] << 8 | _MBAP[5]; 50 | _len--; // Do not count with last byte from MBAP 51 | if (_MBAP[2] !=0 | _MBAP[3] !=0) return; //Not a MODBUSIP packet 52 | if (_len > MODBUSIP_MAXFRAME) return; //Length is over MODBUSIP_MAXFRAME 53 | 54 | _frame = (byte*) malloc(_len); 55 | i = 0; 56 | while (i < _len){ 57 | _frame[i] = Ethernet::buffer[pos+7+i]; //Forget MBAP and take just modbus pdu 58 | i++; 59 | } 60 | 61 | this->receivePDU(_frame); 62 | 63 | if (_reply != MB_REPLY_OFF) { 64 | //MBAP 65 | _MBAP[4] = (_len+1) >> 8; //_len+1 for last byte from MBAP 66 | _MBAP[5] = (_len+1) & 0x00FF; 67 | 68 | BufferFiller bfill = ether.tcpOffset(); 69 | bfill.emit_raw((const char *)_MBAP, 7); 70 | bfill.emit_raw((const char *)_frame, _len); 71 | #ifdef TCP_KEEP_ALIVE 72 | ether.httpServerReplyAck (); 73 | ether.httpServerReply_with_flags(bfill.position(), TCP_FLAGS_ACK_V|TCP_FLAGS_PUSH_V); 74 | #else 75 | ether.httpServerReply(bfill.position()); 76 | #endif 77 | 78 | } 79 | 80 | free(_frame); 81 | _len = 0; 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /ModbusIP_ENC28J60_STM32/ModbusIP_ENC28J60_STM.h: -------------------------------------------------------------------------------- 1 | /* 2 | ModbusIP_ENC28J60.h - Header for Modbus IP ENC28J60 Library 3 | Copyright (C) 2015 André Sarmento Barbosa 4 | */ 5 | #include 6 | #include 7 | #include 8 | 9 | #ifndef MODBUSIP_ENC28J60_STM_H 10 | #define MODBUSIP_ENC28J60_STM_H 11 | 12 | #define MODBUSIP_PORT 502 13 | #define MODBUSIP_MAXFRAME 200 14 | 15 | #define ENC28J60_CS PA8 //Default chip select pin 16 | #define TCP_KEEP_ALIVE 17 | 18 | class ModbusIP : public Modbus { 19 | private: 20 | byte _MBAP[7]; 21 | 22 | public: 23 | ModbusIP(); 24 | void config(uint8_t *mac); 25 | void config(uint8_t *mac, uint8_t * ip); 26 | void config(uint8_t *mac, uint8_t * ip, uint8_t * dns); 27 | void config(uint8_t *mac, uint8_t * ip, uint8_t * dns, uint8_t * gateway); 28 | void config(uint8_t *mac, uint8_t * ip, uint8_t * dns, uint8_t * gateway, uint8_t * subnet); 29 | void task(); 30 | }; 31 | 32 | #endif //MODBUSIP_ENC28J60_H 33 | 34 | -------------------------------------------------------------------------------- /ModbusIP_ENC28J60_STM32/examples/modbus_IP_STM_example/modbus_IP_STM_example.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Originally library comes from: 3 | http://github.com/andresarmento/modbus-arduino 4 | Copyright by André Sarmento Barbosa 5 | 6 | Some modifications were added to provide compatibility 7 | with stm32 duino blue pill board. by AlexPutz 8 | 9 | PIN Connections (Using STM32F103): 10 | /////////////////////// 11 | ENC28J60 STM32F103 12 | Q3 3.3V 13 | SCK PA5 14 | SO PA6 15 | SI PA7 16 | CS PA8 17 | RST 3.3V 18 | ////////////////////// 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | //ModbusIP object 26 | ModbusIP mb; 27 | 28 | void setup() { 29 | 30 | // The media access control (ethernet hardware) address for the shield 31 | byte mac[] = { 0xDE, 0xAD, 0xB1, 0xFF, 0xFD, 0xED }; 32 | // The IP address for the shield 33 | byte ip[] = { 192, 168, 0, 121 }; 34 | //Config Modbus IP 35 | if (ether.begin(500*8, mac) == 0){ 36 | Serial.println( "Failed to access Ethernet controller"); 37 | } 38 | mb.config(mac, ip); 39 | //Create 50 empty modbus registers 40 | for(int i = 0; i <= 50; i++) 41 | { 42 | mb.addHreg(i,0); 43 | } 44 | 45 | } 46 | 47 | void loop() { 48 | //Call once inside loop() - all magic here 49 | mb.task(); 50 | //Setup the registers to contain some numbers; 51 | mb.Hreg(2,1488617171/65536); 52 | mb.Hreg(3,1488617171%65536); 53 | 54 | } 55 | -------------------------------------------------------------------------------- /ModbusIP_ENC28J60_STM32/keywords.txt: -------------------------------------------------------------------------------- 1 | # Syntax Coloring Map For ModbusIP 2 | 3 | # Datatypes (KEYWORD1) 4 | ModbusIP KEYWORD1 5 | ModbusIP_ENC28J60 KEYWORD1 6 | 7 | # Methods and Functions (KEYWORD2) 8 | config KEYWORD2 9 | task KEYWORD2 10 | 11 | # Constants (LITERAL1) 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Modbus-TCP-for-stm32-blue-pill 2 | This project features few corrections for original modbus TCP ENC28J60 in order for it to work with stm32 (stm32duino) blue pill 3 | 4 | PIN Connections (Using STM32F103): 5 | 6 | ENC28J60 | STM32F103 | 7 | ------------------ |------------------| 8 | Q3 | 3.3V | 9 | SCK |PA5 | 10 | SO |PA6 | 11 | SI |PA7 | 12 | CS |PA8 | 13 | RST |3.3V | 14 | 15 | 16 | For reference see the page of original library. 17 | https://github.com/andresarmento/modbus-arduino 18 | --------------------------------------------------------------------------------