mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-27 14:42:18 +03:00
исправление модуля Smi2_m
This commit is contained in:
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
};
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user