mirror of
https://github.com/anklimov/lighthub
synced 2025-12-09 21:29:49 +03:00
uartBridge refactored & cross-compiled. new Mega dev
This commit is contained in:
@@ -356,7 +356,7 @@ return is8bit;
|
||||
|
||||
//if (readModbus(registerFrom,MODBUS_HOLDING_REG_TYPE,registerTo-registerFrom+1))
|
||||
if (readModbus(registerFrom,regType,registerTo-registerFrom+1))
|
||||
{
|
||||
{ debugSerial<<endl;
|
||||
for(int i=registerFrom;i<=registerTo;i++)
|
||||
{
|
||||
findRegister(i,i-registerFrom,regType);
|
||||
@@ -382,7 +382,6 @@ if ((store->pollingRegisters || store->pollingIrs) && !modbusBusy && (Status() =
|
||||
modbusSerial.begin(store->baud, static_cast <USARTClass::USARTModes> (store->serialParam));
|
||||
#elif defined (ARDUINO_ARCH_ESP8266)
|
||||
modbusSerial.begin(store->baud, static_cast <SerialConfig>(store->serialParam));
|
||||
Serial1.begin()
|
||||
#elif defined (ESP32)
|
||||
//modbusSerial.begin(store->baud, store->serialParam);
|
||||
//delay(100);
|
||||
|
||||
@@ -7,71 +7,37 @@
|
||||
#include "Streaming.h"
|
||||
|
||||
#include "item.h"
|
||||
#include <ModbusMaster.h>
|
||||
//#include <ModbusMaster.h>
|
||||
#include "main.h"
|
||||
#include <HardwareSerial.h>
|
||||
|
||||
#include "main.h"
|
||||
|
||||
#if defined (ESP32)
|
||||
#include <AsyncUDP.h>
|
||||
AsyncUDP udp;
|
||||
AsyncUDPMessage udpMessageA;
|
||||
AsyncUDPMessage udpMessageB;
|
||||
|
||||
#include <Udp.h>
|
||||
#ifndef WIFI_ENABLE
|
||||
EthernetUDP udpClientA;
|
||||
EthernetUDP udpClientB;
|
||||
#else
|
||||
WiFiUDP udpClientA;
|
||||
WiFiUDP udpClientB;
|
||||
#endif
|
||||
|
||||
IPAddress targetIP;
|
||||
uint16_t targetPort=5555;
|
||||
#endif
|
||||
|
||||
short halfduplex=1;
|
||||
bool udpdump=false;
|
||||
uint32_t timerA = 0;
|
||||
uint32_t timerB = 0;
|
||||
uint16_t sizeA = 0;
|
||||
uint16_t sizeB = 0;
|
||||
|
||||
//extern aJsonObject *modbusObj;
|
||||
//extern ModbusMaster node;
|
||||
//extern short modbusBusy;
|
||||
//extern void modbusIdle(void) ;
|
||||
|
||||
extern aJsonObject *modbusObj;
|
||||
extern ModbusMaster node;
|
||||
extern short modbusBusy;
|
||||
extern void modbusIdle(void) ;
|
||||
|
||||
/*
|
||||
struct reg_t
|
||||
{
|
||||
const char verb[4];
|
||||
const uint8_t id;
|
||||
};
|
||||
|
||||
|
||||
#define PAR_I16 1
|
||||
#define PAR_I32 2
|
||||
#define PAR_U16 3
|
||||
#define PAR_U32 4
|
||||
#define PAR_I8H 5
|
||||
#define PAR_I8L 6
|
||||
#define PAR_U8H 7
|
||||
#define PAR_U8L 8
|
||||
#define PAR_TENS 9
|
||||
|
||||
const reg_t regSize_P[] PROGMEM =
|
||||
{
|
||||
{ "i16", (uint8_t) PAR_I16 },
|
||||
{ "i32", (uint8_t) PAR_I32 },
|
||||
{ "u16", (uint8_t) PAR_U16 },
|
||||
{ "u32", (uint8_t) PAR_U32 },
|
||||
{ "i8h", (uint8_t) PAR_I8H },
|
||||
{ "i8l", (uint8_t) PAR_I8L },
|
||||
{ "u8h", (uint8_t) PAR_U8H },
|
||||
{ "u8l", (uint8_t) PAR_U8L },
|
||||
{ "x10", (uint8_t) PAR_TENS }
|
||||
} ;
|
||||
#define regSizeNum sizeof(regSize_P)/sizeof(reg_t)
|
||||
|
||||
int str2regSize(char * str)
|
||||
{
|
||||
for(uint8_t i=0; i<regSizeNum && str;i++)
|
||||
if (strcmp_P(str, regSize_P[i].verb) == 0)
|
||||
return pgm_read_byte_near(®Size_P[i].id);
|
||||
return (int) PAR_I16;
|
||||
}
|
||||
*/
|
||||
|
||||
bool out_UARTbridge::getConfig()
|
||||
{
|
||||
@@ -106,11 +72,20 @@ bool out_UARTbridge::getConfig()
|
||||
|
||||
aJsonObject * debugIPObj=aJson.getObjectItem(item->itemArg, "ip");
|
||||
aJsonObject * debugPortObj=aJson.getObjectItem(item->itemArg, "port");
|
||||
aJsonObject * hdObj=aJson.getObjectItem(item->itemArg, "hd");
|
||||
|
||||
if (debugIPObj)
|
||||
{
|
||||
_inet_aton(debugIPObj->valuestring, targetIP);
|
||||
if (udpClientA.begin(SOURCE_PORT_A) && udpClientB.begin(SOURCE_PORT_B)) udpdump=true;
|
||||
else {
|
||||
udpClientA.stop(); udpClientB.stop(); udpdump=false;
|
||||
errorSerial<<F("No sockets available, udpdump disabled")<<endl;
|
||||
}
|
||||
}
|
||||
|
||||
if (debugPortObj) targetPort = debugPortObj->valueint;
|
||||
if (debugPortObj) targetPort = debugPortObj->valueint;
|
||||
if (hdObj) halfduplex = hdObj->valueint;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -125,7 +100,12 @@ if (!store)
|
||||
{ errorSerial<<F("UARTbridge: Out of memory")<<endl;
|
||||
return 0;}
|
||||
|
||||
//store->timestamp=millisNZ();
|
||||
|
||||
sizeA=0;
|
||||
sizeB=0;
|
||||
timerA=0;
|
||||
timerB=0;
|
||||
|
||||
if (getConfig())
|
||||
{
|
||||
infoSerial<<F("UARTbridge config loaded ")<< item->itemArr->name<<endl;
|
||||
@@ -143,6 +123,10 @@ else
|
||||
int out_UARTbridge::Stop()
|
||||
{
|
||||
Serial.println("UARTbridge De-Init");
|
||||
|
||||
udpClientA.stop();
|
||||
udpClientB.stop();
|
||||
udpdump=false;
|
||||
|
||||
delete store;
|
||||
item->setPersistent(NULL);
|
||||
@@ -157,11 +141,75 @@ if (store)
|
||||
return CST_UNKNOWN;
|
||||
}
|
||||
|
||||
void flushA()
|
||||
{
|
||||
if (sizeA)
|
||||
{
|
||||
if (lanStatus>=HAVE_IP_ADDRESS && udpdump) udpClientA.endPacket();
|
||||
debugSerial<<endl;
|
||||
}
|
||||
timerA=0;
|
||||
sizeA=0;
|
||||
}
|
||||
|
||||
void flushB()
|
||||
{
|
||||
if (sizeB)
|
||||
{
|
||||
if (lanStatus>=HAVE_IP_ADDRESS && udpdump) udpClientB.endPacket();
|
||||
debugSerial<<endl;
|
||||
}
|
||||
timerB=0;
|
||||
sizeB=0;
|
||||
}
|
||||
|
||||
int out_UARTbridge::Poll(short cause)
|
||||
{
|
||||
uint8_t chA;
|
||||
uint8_t chB;
|
||||
|
||||
while (MODULE_UATRBRIDGE_UARTA.available() || MODULE_UATRBRIDGE_UARTB.available())
|
||||
{
|
||||
|
||||
if (MODULE_UATRBRIDGE_UARTA.available())
|
||||
{
|
||||
if (!sizeA)
|
||||
{
|
||||
if (lanStatus>=HAVE_IP_ADDRESS && udpdump) udpClientA.beginPacket(targetIP, targetPort);
|
||||
if (halfduplex) flushB();
|
||||
}
|
||||
|
||||
timerA=millisNZ();
|
||||
////if (timerB) return 1;
|
||||
chA=MODULE_UATRBRIDGE_UARTA.read();
|
||||
MODULE_UATRBRIDGE_UARTB.write(chA);
|
||||
debugSerial<<F("<")<<((chA<16)?"0":"")<<_HEX(chA);
|
||||
if (lanStatus>=HAVE_IP_ADDRESS && udpdump) {udpClientA.write(chA);}
|
||||
sizeA++;
|
||||
}
|
||||
|
||||
if (MODULE_UATRBRIDGE_UARTB.available())
|
||||
{
|
||||
if (!sizeB)
|
||||
{
|
||||
if (lanStatus>=HAVE_IP_ADDRESS && udpdump) udpClientB.beginPacket(targetIP, targetPort);
|
||||
if (halfduplex) flushA();
|
||||
}
|
||||
timerB=millisNZ();
|
||||
////if (timerA) return 1;
|
||||
chB=MODULE_UATRBRIDGE_UARTB.read();
|
||||
MODULE_UATRBRIDGE_UARTA.write(chB);
|
||||
debugSerial<<F(">")<<((chB<16)?"0":"")<<_HEX(chB);
|
||||
if (lanStatus>=HAVE_IP_ADDRESS && udpdump) {udpClientB.write(chB);};
|
||||
sizeB++;
|
||||
}
|
||||
}
|
||||
|
||||
if ((timerA && (isTimeOver(timerA,millis(),PDELAY)) || sizeA>=MAX_PDU)) flushA();
|
||||
if ((timerB && (isTimeOver(timerB,millis(),PDELAY)) || sizeB>=MAX_PDU)) flushB();
|
||||
|
||||
|
||||
/*
|
||||
if ((lanStatus>=HAVE_IP_ADDRESS) && udpMessageA.length() && (isTimeOver(timerA,millis(),PDELAY) || timerB))
|
||||
{
|
||||
udp.sendTo(udpMessageA,targetIP,targetPort);
|
||||
@@ -201,19 +249,8 @@ if ((lanStatus>=HAVE_IP_ADDRESS) && udpMessageB.length() && (isTimeOver(timerB,m
|
||||
udpMessageB.write(chB);
|
||||
}
|
||||
}
|
||||
/*
|
||||
while (MODULE_UATRBRIDGE_UARTB.available())
|
||||
{
|
||||
chB=MODULE_UATRBRIDGE_UARTB.read();
|
||||
MODULE_UATRBRIDGE_UARTA.write(chB);
|
||||
debugSerial<<F(">")<<((chB<10)?"0":"")<<_HEX(chB);
|
||||
udpMessageB.write(chB);
|
||||
timerB=millisNZ();
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
return 1;//store->pollingInterval;
|
||||
};
|
||||
|
||||
|
||||
@@ -12,6 +12,10 @@
|
||||
#define serialParamType uint16_t
|
||||
#endif
|
||||
|
||||
#define SOURCE_PORT_A 5551
|
||||
#define SOURCE_PORT_B 5552
|
||||
#define MAX_PDU 1024
|
||||
|
||||
#ifndef MODULE_UATRBRIDGE_UARTA
|
||||
#define MODULE_UATRBRIDGE_UARTA Serial1
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user