исправление модуля Smi2_m

This commit is contained in:
Dmitry Borisenko
2023-06-25 19:14:45 +02:00
parent 415fe9c5b3
commit 1b42083f43
8 changed files with 261 additions and 390 deletions

View File

@@ -387,45 +387,7 @@
},
{
"global": 0,
"name": "26. DS2423 счетчик 1",
"type": "Reading",
"subtype": "Ds2423a",
"id": "dscounta",
"widget": "anydataDef",
"page": "Счетчики",
"descr": "DS1 V",
"plus": 0,
"multiply": 1,
"pin": 4,
"index": 0,
"addr": "",
"int": 10,
"round": 0,
"needSave": 0,
"num": 26
},
{
"global": 0,
"name": "27. DS2423 счетчик 2",
"type": "Reading",
"subtype": "Ds2423b",
"id": "dscountb",
"widget": "anydataDef",
"page": "Счетчики",
"descr": "DS2 V",
"plus": 0,
"multiply": 1,
"pin": 4,
"index": 0,
"addr": "",
"int": 10,
"round": 0,
"needSave": 0,
"num": 27
},
{
"global": 0,
"name": "28. Аналоговый счетчик импульсов",
"name": "26. Аналоговый счетчик импульсов",
"type": "Writing",
"subtype": "Impulse",
"id": "impulse",
@@ -438,32 +400,11 @@
"pinMode": "INPUT",
"debounceDelay": 3,
"multiply": 1,
"num": 28
"num": 26
},
{
"global": 0,
"name": "29. Cенсор температуры NTC",
"type": "Reading",
"subtype": "Ntc",
"id": "Ntctmp",
"widget": "anydataTmp",
"page": "Сенсоры",
"descr": "NTC Температура",
"needSave": 0,
"val": "0",
"int": 15,
"pin": "35",
"R1": "10000",
"R0": "10000",
"Beta": "3950.0",
"T0": "25",
"Vs": "3.3",
"round": 1,
"num": 29
},
{
"global": 0,
"name": "30. PZEM 004t Напряжение",
"name": "27. PZEM 004t Напряжение",
"type": "Reading",
"subtype": "Pzem004v",
"id": "v",
@@ -473,11 +414,11 @@
"int": 15,
"addr": "0xF8",
"round": 1,
"num": 30
"num": 27
},
{
"global": 0,
"name": "31. PZEM 004t Сила тока",
"name": "28. PZEM 004t Сила тока",
"type": "Reading",
"subtype": "Pzem004a",
"id": "a",
@@ -487,11 +428,11 @@
"int": 15,
"addr": "0xF8",
"round": 1,
"num": 31
"num": 28
},
{
"global": 0,
"name": "32. PZEM 004t Мощность",
"name": "29. PZEM 004t Мощность",
"type": "Reading",
"subtype": "Pzem004w",
"id": "w",
@@ -501,11 +442,11 @@
"int": 15,
"addr": "0xF8",
"round": 1,
"num": 32
"num": 29
},
{
"global": 0,
"name": "33. PZEM 004t Энергия",
"name": "30. PZEM 004t Энергия",
"type": "Reading",
"subtype": "Pzem004wh",
"id": "wh",
@@ -515,11 +456,11 @@
"int": 15,
"addr": "0xF8",
"round": 1,
"num": 33
"num": 30
},
{
"global": 0,
"name": "34. PZEM 004t Частота",
"name": "31. PZEM 004t Частота",
"type": "Reading",
"subtype": "Pzem004hz",
"id": "hz",
@@ -529,11 +470,11 @@
"int": 15,
"addr": "0xF8",
"round": 1,
"num": 34
"num": 31
},
{
"global": 0,
"name": "35. PZEM 004t Косинус",
"name": "32. PZEM 004t Косинус",
"type": "Reading",
"subtype": "Pzem004pf",
"id": "pf",
@@ -543,11 +484,11 @@
"int": 15,
"addr": "0xF8",
"round": 1,
"num": 35
"num": 32
},
{
"global": 0,
"name": "36. PZEM настройка",
"name": "33. PZEM настройка",
"type": "Reading",
"subtype": "Pzem004cmd",
"id": "set",
@@ -559,11 +500,11 @@
"changeaddr": 0,
"setaddr": "0x01",
"reset": 0,
"num": 36
"num": 33
},
{
"global": 0,
"name": "37. Часы реального времени",
"name": "34. Часы реального времени",
"type": "Reading",
"subtype": "RTC",
"id": "rtc",
@@ -579,11 +520,11 @@
"int": 5,
"btn-setUTime": "0",
"btn-setSysTime": "nil",
"num": 37
"num": 34
},
{
"name": "38. (S8) Cенсор качества воздуха",
"num": 38,
"name": "35. (S8) Cенсор качества воздуха",
"num": 35,
"type": "Reading",
"subtype": "S8co",
"id": "s8co",
@@ -597,50 +538,7 @@
},
{
"global": 0,
"name": "39. SCD40 Углекислый газ",
"type": "Reading",
"subtype": "Scd40co2",
"id": "Scd40co2",
"widget": "anydataPpm",
"page": "Сенсоры",
"descr": "SCD40 Углекислый газ",
"int": 15,
"round": 0,
"lowPeriodic": 1,
"autoCalibration": 1,
"btn-Recalibration": 0,
"num": 39
},
{
"global": 0,
"name": "40. SCD40 Влажность",
"type": "Reading",
"subtype": "Scd40h",
"id": "Scd40h",
"widget": "anydataHum",
"page": "Сенсоры",
"descr": "SCD40 Влажность",
"int": 15,
"round": 1,
"num": 40
},
{
"global": 0,
"name": "41. SCD40 Температура",
"type": "Reading",
"subtype": "Scd40t",
"id": "Scd40t",
"widget": "anydataTmp",
"page": "Сенсоры",
"descr": "SCD40 Температура",
"int": 15,
"round": 1,
"offset": 4,
"num": 41
},
{
"global": 0,
"name": "42. Sht20 Температура",
"name": "36. Sht20 Температура",
"type": "Reading",
"subtype": "Sht20t",
"id": "tmp2",
@@ -649,11 +547,11 @@
"descr": "Температура",
"int": 15,
"round": 1,
"num": 42
"num": 36
},
{
"global": 0,
"name": "43. Sht20 Влажность",
"name": "37. Sht20 Влажность",
"type": "Reading",
"subtype": "Sht20h",
"id": "Hum2",
@@ -662,11 +560,11 @@
"descr": "Влажность",
"int": 15,
"round": 1,
"num": 43
"num": 37
},
{
"global": 0,
"name": "44. Sht30 Температура",
"name": "38. Sht30 Температура",
"type": "Reading",
"subtype": "Sht30t",
"id": "tmp30",
@@ -675,11 +573,11 @@
"descr": "SHT30 Температура",
"int": 15,
"round": 1,
"num": 44
"num": 38
},
{
"global": 0,
"name": "45. Sht30 Влажность",
"name": "39. Sht30 Влажность",
"type": "Reading",
"subtype": "Sht30h",
"id": "Hum30",
@@ -688,12 +586,12 @@
"descr": "SHT30 Влажность",
"int": 15,
"round": 1,
"num": 45
"num": 39
},
{
"global": 0,
"name": "46. HC-SR04 Ультразвуковой дальномер",
"num": 46,
"name": "40. HC-SR04 Ультразвуковой дальномер",
"num": 40,
"type": "Reading",
"subtype": "Sonar",
"id": "sonar",
@@ -705,7 +603,7 @@
"int": 5
},
{
"name": "47. UART",
"name": "41. UART",
"type": "Reading",
"subtype": "UART",
"page": "",
@@ -717,14 +615,14 @@
"line": 2,
"speed": 9600,
"eventFormat": 0,
"num": 47
"num": 41
},
{
"header": "Исполнительные устройства"
},
{
"global": 0,
"name": "48. Кнопка подключенная к пину",
"name": "42. Кнопка подключенная к пину",
"type": "Writing",
"subtype": "ButtonIn",
"id": "btn",
@@ -739,11 +637,11 @@
"debounceDelay": 50,
"fixState": 0,
"inv": 0,
"num": 48
"num": 42
},
{
"global": 0,
"name": "49. Управление пином",
"name": "43. Управление пином",
"type": "Writing",
"subtype": "ButtonOut",
"needSave": 0,
@@ -754,11 +652,11 @@
"int": 0,
"inv": 0,
"pin": 2,
"num": 49
"num": 43
},
{
"global": 0,
"name": "50. Пассивный звуковой извещатель",
"name": "44. Пассивный звуковой извещатель",
"type": "Writing",
"subtype": "Buzzer",
"id": "buzzer",
@@ -777,11 +675,11 @@
"cycle": 0,
"indication": 1,
"val": 0,
"num": 50
"num": 44
},
{
"global": 0,
"name": "51. Энкодер",
"name": "45. Энкодер",
"type": "Writing",
"subtype": "Encoder",
"id": "enc",
@@ -794,11 +692,11 @@
"step": 1,
"stepOnPress": 5,
"pins": "4,5,2",
"num": 51
"num": 45
},
{
"global": 0,
"name": "52. Сервопривод",
"name": "46. Сервопривод",
"type": "Writing",
"subtype": "IoTServo",
"id": "servo",
@@ -809,11 +707,11 @@
"pin": 12,
"apin": -1,
"amap": "0, 4096, 0, 180",
"num": 52
"num": 46
},
{
"global": 0,
"name": "53. Расширитель портов Mcp23017",
"name": "47. Расширитель портов Mcp23017",
"type": "Reading",
"subtype": "Mcp23017",
"id": "Mcp",
@@ -823,11 +721,11 @@
"int": "0",
"addr": "0x20",
"index": 1,
"num": 53
"num": 47
},
{
"global": 0,
"name": "54. MP3 плеер",
"name": "48. MP3 плеер",
"type": "Reading",
"subtype": "Mp3",
"id": "mp3",
@@ -837,11 +735,11 @@
"int": 1,
"pins": "14,12",
"volume": 20,
"num": 54
"num": 48
},
{
"global": 0,
"name": "55. Сенсорная кнопка",
"name": "49. Сенсорная кнопка",
"type": "Writing",
"subtype": "Multitouch",
"id": "impulse",
@@ -855,11 +753,11 @@
"pinMode": "INPUT",
"debounceDelay": 50,
"PWMDelay": 500,
"num": 55
"num": 49
},
{
"global": 0,
"name": "56. Расширитель портов Pcf8574",
"name": "50. Расширитель портов Pcf8574",
"type": "Reading",
"subtype": "Pcf8574",
"id": "Pcf",
@@ -869,11 +767,11 @@
"int": "0",
"addr": "0x20",
"index": 1,
"num": 56
"num": 50
},
{
"global": 0,
"name": "57. PWM ESP32",
"name": "51. PWM ESP32",
"type": "Writing",
"subtype": "Pwm32",
"id": "pwm",
@@ -887,11 +785,11 @@
"PWM_resolution": 10,
"val": 0,
"apin": -1,
"num": 57
"num": 51
},
{
"global": 0,
"name": "58. Телеграм-Лайт",
"name": "52. Телеграм-Лайт",
"type": "Writing",
"subtype": "TelegramLT",
"id": "tg",
@@ -900,14 +798,14 @@
"descr": "",
"token": "",
"chatID": "",
"num": 58
"num": 52
},
{
"header": "Экраны"
},
{
"global": 0,
"name": "59. LCD экран 2004",
"name": "53. LCD экран 2004",
"type": "Reading",
"subtype": "Lcd2004",
"id": "Lcd",
@@ -919,10 +817,10 @@
"size": "20,4",
"coord": "0,0",
"id2show": "id датчика",
"num": 59
"num": 53
},
{
"name": "60. LCD экран 1602",
"name": "54. LCD экран 1602",
"type": "Reading",
"subtype": "Lcd2004",
"id": "Lcd",
@@ -934,6 +832,23 @@
"size": "16,2",
"coord": "0,0",
"id2show": "id датчика",
"num": 60
"num": 54
},
{
"global": 0,
"name": "55. Smi2_m",
"type": "Writing",
"subtype": "Smi2_m",
"id": "Smi",
"widget": "",
"page": "Дисплеи",
"descr": "",
"int": 2,
"pin": 4,
"id2show": "id датчика",
"baud": "9600",
"rx": "16",
"tx": "17",
"num": 55
}
]

View File

@@ -118,7 +118,7 @@
},
{
"path": "src/modules/sensors/Ds2423",
"active": true
"active": false
},
{
"path": "src/modules/sensors/Emon",
@@ -174,7 +174,7 @@
},
{
"path": "src/modules/sensors/Ntc",
"active": true
"active": false
},
{
"path": "src/modules/sensors/Pzem004t",
@@ -194,7 +194,7 @@
},
{
"path": "src/modules/sensors/Scd40",
"active": true
"active": false
},
{
"path": "src/modules/sensors/Sds011",
@@ -242,6 +242,10 @@
"path": "src/modules/exec/EspCam",
"active": false
},
{
"path": "src/modules/exec/Ftp",
"active": false
},
{
"path": "src/modules/exec/HttpGet",
"active": false
@@ -318,7 +322,7 @@
},
{
"path": "src/modules/display/Smi2_m",
"active": false
"active": true
},
{
"path": "src/modules/display/Ws2812b",

View File

@@ -322,7 +322,7 @@
},
{
"path": "src/modules/display/Smi2_m",
"active": false
"active": true
},
{
"path": "src/modules/display/Ws2812b",

View File

@@ -463,11 +463,7 @@ lib_deps =
adafruit/Adafruit BMP280 Library
beegee-tokyo/DHT sensor library for ESPx
https://github.com/milesburton/Arduino-Temperature-Control-Library
https://github.com/jbechter/arduino-onewire-DS2423
paulstoffregen/OneWire @ ^2.3.7
https://github.com/tremaru/iarduino_RTC
Sensirion I2C SCD4x @0.4.0
Sensirion Core @0.6.0
robtillaart/SHT2x@^0.1.1
WEMOS SHT3x@1.0.0
plerup/EspSoftwareSerial
@@ -494,13 +490,10 @@ build_src_filter =
+<modules/sensors/Bmp280>
+<modules/sensors/Dht1122>
+<modules/sensors/Ds18b20>
+<modules/sensors/Ds2423>
+<modules/sensors/Impulse>
+<modules/sensors/Ntc>
+<modules/sensors/Pzem004t>
+<modules/sensors/RTC>
+<modules/sensors/S8>
+<modules/sensors/Scd40>
+<modules/sensors/Sht20>
+<modules/sensors/Sht30>
+<modules/sensors/Sonar>
@@ -517,6 +510,7 @@ build_src_filter =
+<modules/exec/Pwm32>
+<modules/exec/TelegramLT>
+<modules/display/Lcd2004>
+<modules/display/Smi2_m>
[env:esp32s2_4mb_fromitems]
lib_deps =

View File

@@ -14,13 +14,10 @@ void* getAPI_Bme280(String subtype, String params);
void* getAPI_Bmp280(String subtype, String params);
void* getAPI_Dht1122(String subtype, String params);
void* getAPI_Ds18b20(String subtype, String params);
void* getAPI_Ds2423(String subtype, String params);
void* getAPI_Impulse(String subtype, String params);
void* getAPI_Ntc(String subtype, String params);
void* getAPI_Pzem004(String subtype, String params);
void* getAPI_RTC(String subtype, String params);
void* getAPI_S8(String subtype, String params);
void* getAPI_Scd40(String subtype, String params);
void* getAPI_Sht20(String subtype, String params);
void* getAPI_Sht30(String subtype, String params);
void* getAPI_Sonar(String subtype, String params);
@@ -37,6 +34,7 @@ void* getAPI_Pcf8574(String subtype, String params);
void* getAPI_Pwm32(String subtype, String params);
void* getAPI_TelegramLT(String subtype, String params);
void* getAPI_Lcd2004(String subtype, String params);
void* getAPI_Smi2_m(String subtype, String params);
void* getAPI(String subtype, String params) {
void* tmpAPI;
@@ -54,13 +52,10 @@ if ((tmpAPI = getAPI_Bme280(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Bmp280(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Dht1122(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Ds18b20(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Ds2423(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Impulse(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Ntc(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Pzem004(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_RTC(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_S8(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Scd40(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Sht20(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Sht30(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Sonar(subtype, params)) != nullptr) return tmpAPI;
@@ -77,5 +72,6 @@ if ((tmpAPI = getAPI_Pcf8574(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Pwm32(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_TelegramLT(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Lcd2004(subtype, params)) != nullptr) return tmpAPI;
if ((tmpAPI = getAPI_Smi2_m(subtype, params)) != nullptr) return tmpAPI;
return nullptr;
}

View File

@@ -1,175 +1,150 @@
#include "Modbus_master_for_Smi2_m.h"
#include "HardwareSerial.h"
#include <stdio.h>
Smi_display::Smi_display() {
}
// Modbus Master
void Smi_display:: modbus_update()
{
idle();
void Smi_display::modbus_update() {
idle();
}
void Smi_display:: idle()
{
static unsigned int packet_index;
unsigned int failed_connections = 0;
unsigned char current_connection;
do
{
if (packet_index == total_no_of_packets) // wrap around to the beginning
packet_index = 0;
// proceed to the next packet
packet = &packetArray[packet_index];
// get the current connection status
current_connection = packet->connection;
if (!current_connection)
{
// If all the connection attributes are false return
// immediately to the main sketch
if (++failed_connections == total_no_of_packets)
return;
}
packet_index++;
// if a packet has no connection get the next one
}while (!current_connection);
constructPacket();
void Smi_display::idle() {
static unsigned int packet_index;
unsigned int failed_connections = 0;
unsigned char current_connection;
do {
if (packet_index == total_no_of_packets) // wrap around to the beginning
packet_index = 0;
// proceed to the next packet
packet = &packetArray[packet_index];
// get the current connection status
current_connection = packet->connection;
if (!current_connection) {
// If all the connection attributes are false return
// immediately to the main sketch
if (++failed_connections == total_no_of_packets)
return;
}
packet_index++;
// if a packet has no connection get the next one
} while (!current_connection);
constructPacket();
}
void Smi_display:: constructPacket()
{
packet->requests++;
frame[0] = packet->id;
frame[1] = packet->function;
frame[2] = packet->address >> 8; // address Hi
frame[3] = packet->address & 0xFF; // address Lo
frame[4] = packet->data >> 8; // MSB
frame[5] = packet->data & 0xFF; // LSB
unsigned char frameSize;
// construct the frame according to the modbus function
if (packet->function == PRESET_MULTIPLE_REGISTERS)
frameSize = construct_F16();
else // else functions 1,2,3,4,5 & 6 is assumed. They all share the exact same request format.
frameSize = 8; // the request is always 8 bytes in size for the above mentioned functions.
unsigned int crc16 = calculateCRC(frameSize - 2);
frame[frameSize - 2] = crc16 >> 8; // split crc into 2 bytes
frame[frameSize - 1] = crc16 & 0xFF;
sendPacket(frameSize);
void Smi_display::constructPacket() {
packet->requests++;
frame[0] = packet->id;
frame[1] = packet->function;
frame[2] = packet->address >> 8; // address Hi
frame[3] = packet->address & 0xFF; // address Lo
frame[4] = packet->data >> 8; // MSB
frame[5] = packet->data & 0xFF; // LSB
unsigned char frameSize;
// construct the frame according to the modbus function
if (packet->function == PRESET_MULTIPLE_REGISTERS)
frameSize = construct_F16();
else // else functions 1,2,3,4,5 & 6 is assumed. They all share the exact same request format.
frameSize = 8; // the request is always 8 bytes in size for the above mentioned functions.
unsigned int crc16 = calculateCRC(frameSize - 2);
frame[frameSize - 2] = crc16 >> 8; // split crc into 2 bytes
frame[frameSize - 1] = crc16 & 0xFF;
sendPacket(frameSize);
}
unsigned char Smi_display::construct_F16() {
unsigned char no_of_bytes = packet->data * 2;
unsigned char Smi_display::construct_F16()
{
unsigned char no_of_bytes = packet->data * 2;
// first 6 bytes of the array + no_of_bytes + 2 bytes CRC
frame[6] = no_of_bytes; // number of bytes
unsigned char index = 7; // user data starts at index 7
unsigned char no_of_registers = packet->data;
/*unsigned*/ int temp;
for (unsigned char i = 0; i < no_of_registers; i++)
{
temp = register_array[packet->local_start_address + i]; // get the data
frame[index] = temp >> 8;
index++;
frame[index] = temp & 0xFF;
index++;
}
unsigned char frameSize = (9 + no_of_bytes); // first 7 bytes of the array + 2 bytes CRC + noOfBytes
return frameSize;
// first 6 bytes of the array + no_of_bytes + 2 bytes CRC
frame[6] = no_of_bytes; // number of bytes
unsigned char index = 7; // user data starts at index 7
unsigned char no_of_registers = packet->data;
/*unsigned*/ int temp;
for (unsigned char i = 0; i < no_of_registers; i++) {
temp = register_array[packet->local_start_address + i]; // get the data
frame[index] = temp >> 8;
index++;
frame[index] = temp & 0xFF;
index++;
}
unsigned char frameSize = (9 + no_of_bytes); // first 7 bytes of the array + 2 bytes CRC + noOfBytes
return frameSize;
}
void Smi_display::modbus_configure(HardwareSerial* SerialPort,
long baud,
unsigned char byteFormat,
int rx,
int tx,
unsigned int _TxEnablePin,
Packet* _packets,
unsigned int _total_no_of_packets,
/*unsigned*/ int* _register_array)
{
TxEnablePin = _TxEnablePin;
total_no_of_packets = _total_no_of_packets;
packetArray = _packets;
register_array = _register_array;
long baud,
unsigned char byteFormat,
int rx,
int tx,
unsigned int _TxEnablePin,
Packet* _packets,
unsigned int _total_no_of_packets,
/*unsigned*/ int* _register_array) {
TxEnablePin = _TxEnablePin;
total_no_of_packets = _total_no_of_packets;
packetArray = _packets;
register_array = _register_array;
ModbusPort = SerialPort;
(*ModbusPort).begin(baud, byteFormat, rx, tx);
ModbusPort = SerialPort;
(*ModbusPort).begin(baud, byteFormat,rx,tx);
pinMode(TxEnablePin, OUTPUT);
digitalWrite(TxEnablePin, LOW);
}
void Smi_display::modbus_construct (Packet *_packet,
unsigned char id,
unsigned char function,
unsigned int address,
unsigned int data,
unsigned int local_start_address)
{
_packet->id = id;
_packet->function = function;
_packet->address = address;
_packet->data = data;
_packet->local_start_address = local_start_address;
_packet->connection = 1;
pinMode(TxEnablePin, OUTPUT);
digitalWrite(TxEnablePin, LOW);
}
unsigned int Smi_display::calculateCRC(unsigned char bufferSize)
{
unsigned int temp, temp2, flag;
temp = 0xFFFF;
for (unsigned char i = 0; i < bufferSize; i++)
{
temp = temp ^ frame[i];
for (unsigned char j = 1; j <= 8; j++)
{
flag = temp & 0x0001;
temp >>= 1;
if (flag)
temp ^= 0xA001;
void Smi_display::modbus_construct(Packet* _packet,
unsigned char id,
unsigned char function,
unsigned int address,
unsigned int data,
unsigned int local_start_address) {
_packet->id = id;
_packet->function = function;
_packet->address = address;
_packet->data = data;
_packet->local_start_address = local_start_address;
_packet->connection = 1;
}
unsigned int Smi_display::calculateCRC(unsigned char bufferSize) {
unsigned int temp, temp2, flag;
temp = 0xFFFF;
for (unsigned char i = 0; i < bufferSize; i++) {
temp = temp ^ frame[i];
for (unsigned char j = 1; j <= 8; j++) {
flag = temp & 0x0001;
temp >>= 1;
if (flag)
temp ^= 0xA001;
}
}
}
// Reverse byte order.
temp2 = temp >> 8;
temp = (temp << 8) | temp2;
temp &= 0xFFFF;
// the returned value is already swapped
// crcLo byte is first & crcHi byte is last
return temp;
// Reverse byte order.
temp2 = temp >> 8;
temp = (temp << 8) | temp2;
temp &= 0xFFFF;
// the returned value is already swapped
// crcLo byte is first & crcHi byte is last
return temp;
}
void Smi_display::sendPacket(unsigned char bufferSize)
{
digitalWrite(TxEnablePin, HIGH);
for (unsigned char i = 0; i < bufferSize; i++)
(*ModbusPort).write(frame[i]);
(*ModbusPort).flush();
digitalWrite(TxEnablePin, LOW);
void Smi_display::sendPacket(unsigned char bufferSize) {
digitalWrite(TxEnablePin, HIGH);
for (unsigned char i = 0; i < bufferSize; i++)
(*ModbusPort).write(frame[i]);
(*ModbusPort).flush();
digitalWrite(TxEnablePin, LOW);
}

View File

@@ -1,34 +1,27 @@
#pragma once
#include "HardwareSerial.h"
#include <Arduino.h>
#define PRESET_MULTIPLE_REGISTERS 16
#define BUFFER_SIZE 64
#define PRESET_MULTIPLE_REGISTERS 16
#define BUFFER_SIZE 64
typedef struct
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
unsigned char id;
unsigned char function;
unsigned int address;
unsigned int data;
unsigned int local_start_address;
// modbus <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
unsigned int requests;
unsigned int successful_requests;
unsigned int failed_requests;
unsigned int exception_errors;
unsigned int retries;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
unsigned char connection;
}Packet;
unsigned char id;
unsigned char function;
unsigned int address;
unsigned int data;
unsigned int local_start_address;
unsigned int requests;
unsigned int successful_requests;
unsigned int failed_requests;
unsigned int exception_errors;
unsigned int retries;
unsigned char connection;
} Packet;
class Smi_display {
public:
@@ -36,46 +29,43 @@ class Smi_display {
~Smi_display();
public:
void modbus_update();
void modbus_construct(Packet* _packet,
unsigned char id,
unsigned char function,
unsigned int address,
unsigned int data,
unsigned _local_start_address);
public:
void modbus_update();
void modbus_construct(Packet *_packet,
unsigned char id,
unsigned char function,
unsigned int address,
unsigned int data,
unsigned _local_start_address);
void modbus_configure(HardwareSerial* SerialPort,
long baud,
unsigned char byteFormat,
int rx,
int tx,
unsigned int _TxEnablePin,
Packet* _packets,
unsigned int _total_no_of_packets,
/*unsigned*/ int* _register_array);
void modbus_configure(HardwareSerial* SerialPort,
long baud,
unsigned char byteFormat,
int rx,
int tx,
unsigned int _TxEnablePin,
Packet* _packets,
unsigned int _total_no_of_packets,
/*unsigned*/ int* _register_array);
private:
void idle();
void constructPacket();
unsigned char construct_F16();
unsigned int calculateCRC(unsigned char bufferSize);
void sendPacket(unsigned char bufferSize);
void idle();
void constructPacket();
unsigned char construct_F16();
unsigned int calculateCRC(unsigned char bufferSize);
void sendPacket(unsigned char bufferSize);
private:
unsigned char state;
unsigned char retry_count;
unsigned int TxEnablePin;
unsigned char frame[BUFFER_SIZE];
unsigned char frame[BUFFER_SIZE];
unsigned char buffer;
unsigned int T1_5; // <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
unsigned int frameDelay; // <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
unsigned int total_no_of_packets;
Packet* packetArray; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Packet* packet; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>
/*unsigned*/ int* register_array; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> master <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
unsigned int T1_5; //
unsigned int frameDelay; //
unsigned int total_no_of_packets;
Packet* packetArray; //
Packet* packet; //
/*unsigned*/ int* register_array; //
HardwareSerial* ModbusPort;
};

View File

@@ -5,9 +5,6 @@
#include "Modbus_master_for_Smi2_m.h"
#include "modules/sensors/UART/Uart.h"
#include <iostream>
#include <string>
// Общая сумма доступной памяти на ведущем устройстве, чтобы хранить данные
#define TOTAL_NO_OF_REGISTERS 4