intermediate: use the arduino hwserial as idf does not receive data

This commit is contained in:
andreas 2021-11-03 17:11:40 +01:00
parent 9174f7d086
commit bec155fb4d
3 changed files with 33 additions and 41 deletions

View File

@ -1,30 +1,34 @@
#include "GwSerial.h"
class SerialWriter : public GwBufferWriter{
private:
uart_port_t num;
HardwareSerial *serial;
public:
SerialWriter(uart_port_t num){
this->num=num;
SerialWriter(HardwareSerial *serial){
this->serial=serial;
}
virtual ~SerialWriter(){}
virtual int write(const uint8_t *buffer,size_t len){
return uart_tx_chars(num,(const char *)buffer,len);
size_t numWrite=serial->availableForWrite();
if (numWrite < len) len=numWrite;
if (!len) return 0;
return serial->write(buffer,len);
}
};
GwSerial::GwSerial(GwLog *logger, uart_port_t num, int id,bool allowRead)
GwSerial::GwSerial(GwLog *logger, int num, int id,bool allowRead)
{
LOG_DEBUG(GwLog::DEBUG,"creating GwSerial %p port %d",this,(int)num);
this->id=id;
this->logger = logger;
this->num = num;
this->buffer = new GwBuffer(logger,GwBuffer::TX_BUFFER_SIZE);
this->writer = new SerialWriter(num);
this->allowRead=allowRead;
if (allowRead){
this->readBuffer=new GwBuffer(logger, GwBuffer::RX_BUFFER_SIZE);
}
this->serial=new HardwareSerial(num);
this->writer = new SerialWriter(serial);
}
GwSerial::~GwSerial()
@ -32,32 +36,14 @@ GwSerial::~GwSerial()
delete buffer;
delete writer;
if (readBuffer) delete readBuffer;
delete serial;
}
int GwSerial::setup(int baud, int rxpin, int txpin)
{
uart_config_t config = {
.baud_rate = baud,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
};
if (uart_driver_install(num, bufferSize, 0, 0, NULL, 0) != ESP_OK)
{
return -1;
}
// Configure UART parameters
if (uart_param_config(num, &config) != ESP_OK)
{
return -2;
}
if (uart_set_pin(num, txpin, rxpin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE) != ESP_OK)
{
return -3;
}
serial->begin(baud,SERIAL_8N1,rxpin,txpin);
buffer->reset();
initialized = true;
return true;
return 0;
}
bool GwSerial::isInitialized() { return initialized; }
size_t GwSerial::enqueue(const uint8_t *data, size_t len)
@ -83,9 +69,13 @@ void GwSerial::loop(bool handleRead){
if (! isInitialized()) return;
if (! handleRead) return;
char buffer[10];
int rt=uart_read_bytes(num,(uint8_t *)(&buffer),10,0);
size_t available=serial->available();
if (! available) return;
if (available > 10) available=10;
int rt=serial->readBytes(buffer,available);
if (allowRead && rt > 0){
readBuffer->addData((uint8_t *)(&buffer),rt,true);
size_t wr=readBuffer->addData((uint8_t *)(&buffer),rt,true);
LOG_DEBUG(GwLog::DEBUG+2,"GwSerial read %d bytes, to buffer %d bytes",rt,wr);
}
}
bool GwSerial::readMessages(GwBufferWriter *writer){

View File

@ -1,6 +1,6 @@
#ifndef _GWSERIAL_H
#define _GWSERIAL_H
#include "driver/uart.h"
#include "HardwareSerial.h"
#include "GwLog.h"
#include "GwBuffer.h"
class SerialWriter;
@ -10,16 +10,17 @@ class GwSerial{
GwBuffer *readBuffer=NULL;
GwLog *logger;
SerialWriter *writer;
uart_port_t num;
int num;
bool initialized=false;
bool allowRead=true;
GwBuffer::WriteStatus write();
int id=-1;
int overflows=0;
size_t enqueue(const uint8_t *data, size_t len);
HardwareSerial *serial;
public:
static const int bufferSize=200;
GwSerial(GwLog *logger,uart_port_t num,int id,bool allowRead=true);
GwSerial(GwLog *logger,int num,int id,bool allowRead=true);
~GwSerial();
int setup(int baud,int rxpin,int txpin);
bool isInitialized();

View File

@ -92,7 +92,7 @@ GwConfigInterface *receiveSerial=config.getConfigItem(config.receiveSerial,true)
GwConfigInterface *sendSerial=config.getConfigItem(config.sendSerial,true);
GwConfigInterface *n2kFromSerial=config.getConfigItem(config.serialToN2k,true);
GwSerial *usbSerial = new GwSerial(NULL, UART_NUM_0, USB_CHANNEL_ID);
GwSerial *usbSerial = new GwSerial(NULL, 0, USB_CHANNEL_ID);
GwSerial *serial1=NULL;
@ -275,8 +275,8 @@ void setup() {
}
logger.logDebug(GwLog::LOG,"config: %s", config.toString().c_str());
#ifdef GWSERIAL_MODE
int serialrx=UART_PIN_NO_CHANGE;
int serialtx=UART_PIN_NO_CHANGE;
int serialrx=-1;
int serialtx=-1;
#ifdef GWSERIAL_TX
serialtx=GWSERIAL_TX;
#endif
@ -289,17 +289,18 @@ void setup() {
if (serialMode != String("UNI")){
serialDirection=String("");
}
if (serialDirection == "receive" || serialDirection == "off") serialtx=UART_PIN_NO_CHANGE;
if (serialDirection == "send" || serialDirection == "off") serialrx=UART_PIN_NO_CHANGE;
//if (serialDirection == "receive" || serialDirection == "off") serialtx=UART_PIN_NO_CHANGE;
//if (serialDirection == "send" || serialDirection == "off") serialrx=UART_PIN_NO_CHANGE;
logger.logDebug(GwLog::DEBUG,"serial set up: mode=%s,direction=%s,rx=%d,tx=%d",
serialMode.c_str(),serialDirection.c_str(),serialrx,serialtx
);
if (serialtx != UART_PIN_NO_CHANGE || serialrx != UART_PIN_NO_CHANGE){
if (serialtx != -1 || serialrx != -1){
logger.logDebug(GwLog::LOG,"creating serial interface rx=%d, tx=%d",serialrx,serialtx);
serial1=new GwSerial(&logger,UART_NUM_1,SERIAL1_CHANNEL_ID,true);
serial1=new GwSerial(&logger,1,SERIAL1_CHANNEL_ID,true);
}
if (serial1){
serial1->setup(config.getInt(config.serialBaud,115200),serialrx,serialtx);
int rt=serial1->setup(config.getInt(config.serialBaud,115200),serialrx,serialtx);
logger.logDebug(GwLog::LOG,"starting serial returns %d",rt);
}
#endif
@ -442,7 +443,7 @@ void SendNMEA0183Message(const tNMEA0183Msg &NMEA0183Msg, int sourceId) {
void handleReceivedNmeaMessage(const char *buf, int sourceId){
if ((sourceId == USB_CHANNEL_ID && n2kFromUSB->asBoolean())||
(sourceId >= MIN_TCP_CHANNEL_ID && n2kFromTCP->asBoolean())||
(sourceId == SERIAL1_CHANNEL_ID && n2kFromSerial)
(sourceId == SERIAL1_CHANNEL_ID && n2kFromSerial->asBoolean())
)
toN2KConverter->parseAndSend(buf,sourceId);
sendBufferToChannels(buf,sourceId);