Whilst working on creating a library rDCCEX (based on DCC-EX) to control model railway using the DCC protocol, seeking advice how to convert C/C++ to B4R library h & cpp code.
Function parse
Q: How to convert this function, esp. the Print stream?
Redirect Serial Stream to String
DCCEX and DCCEXParser send log infos to the standard serial line stream - which can be monitored with the Arduino IDE Serial Monitor or B4J Serial Connector.
Q: How to redirect this serial info to a string, which is then used to take any actions like handling errors.
Note
Using Inline C, the B4R application is running fine, i.e. all API commands are parsed properly.
Inline C Snippet
Attached is the B4R project (BETA stage).
Function parse
B4X:
void DCCEXParser::parse(Print *stream, byte *com, bool blocking)
Redirect Serial Stream to String
DCCEX and DCCEXParser send log infos to the standard serial line stream - which can be monitored with the Arduino IDE Serial Monitor or B4J Serial Connector.
Q: How to redirect this serial info to a string, which is then used to take any actions like handling errors.
Note
Using Inline C, the B4R application is running fine, i.e. all API commands are parsed properly.
Inline C Snippet
B4X:
' Inline C code setting up & use DCCEX API functions
#If C
#include "DCCEX.h"
// Create parser for <> commands coming from keyboard or JMRI on the USB connection.
DCCEXParser serialParser;
// Init the DCC engine
int dcc_init (B4R::Object* o) {
// ::Serial.println("dccex_init: start");
int rc = 1;
// Responsibility 1: Start the usb connection for diagnostics and possible JMRI input
Serial.begin(115200);
// Responsibility 2: Start the DCC engine with information about your Motor Shield.
DCC::begin(STANDARD_MOTOR_SHIELD);
return rc;
}
// DCC Looper
int dcc_loop (B4R::Object* o) {
DCC::loop();
return 1;
}
// DCCEX Parser for API command strings, i.e. <1 MAIN> (which is power on)
// Usage in B4R Code Asyncstreams NewData
// Dim cmd As String = bcConv.StringFromBytes(Buffer)
// RunNative("dcc_parse", JoinStrings(Array As String("<", cmd, ">")))
int dcc_parse (B4R::Object* o) {
const char* str = (const char*)o->data.PointerField;
// Print the command to serial
::Serial.println(str);
// void DCCEXParser::parse(Print *stream, byte *com, bool blocking)
serialParser.parse(&Serial, str, false);
return 1;
}
#End If
Attached is the B4R project (BETA stage).
Attachments
Last edited: