B4R Question How to convert C/C++ Print & Serial to B4R Library h & cpp Code

rwblinn

Well-Known Member
Licensed User
Longtime User
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
B4X:
void DCCEXParser::parse(Print *stream, byte *com, bool blocking)
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
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

  • dccex-control.zip
    3.7 KB · Views: 234
Last edited:

rwblinn

Well-Known Member
Licensed User
Longtime User
Thanks for the steer = working fine. Full library following later when done = still a lot to convert.

Any advice to the 2nd question:
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.
How to redirect this serial info to a string, which is then used to take any actions like handling errors.

Solution (Snippet from the rDCCEX library working progress)
Header H

B4X:
/**
* Parse DCCEX API Command
* Parameter:
*    Stream Serial
*    String Command to parse (Note: command must be enclosed in arrows, i.e. <CMD>)
* Example (Private dcc As DCCEX):<code>
* Private serialLine As Serial
* Dim cmd As String = bcConv.StringFromBytes(Buffer)
* dcc.Parse(serialLine.Stream, cmd)
* </code>
**/
void Parse(B4RStream* stream, B4RString* cmd);

Code CPP
B4X:
void B4RDCCEX::Parse(B4RStream* stream, B4RString* cmd){
    // Assign the B4R stream to print
    Print* pstream = (Print*)stream->wrappedStream;
    ::Serial.println(cmd->data);
    // void DCCEXParser::parse(Print *stream, byte *com, bool blocking)
    dccExParser->parse(pstream, cmd->data, false);
}
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
OK will give a try.

YES being back = Have not used B4X for almost a year as focussed on my Domoticz Home Automation Workbook
(here, almost 800 pages, various development tools i.e. JavaScript, Lua, Python, HTML/CSS).

Noticed, of course, that B4X has been develop further greatly (=WELL DONE) . Esp. the B4X Language and B4XLib = will need to learn quite a bit (sweat).

This year want to work on developing a model railway digital controller using DCCEX & B4R (DCC Controller), B4J & B4A & Node-RED (DCC Clients).
Hardware: Arduino MEGA with Motor Shield and various indicators (LED, LCD, TM1637 ...) & sensors (TCRT5000, DHT22 ...) controlling Roco Model Railway.
The simple test track has a digital loco with 2 digital turnouts - shunting model.

Rather challenging project (named shunter63) = electronics, microcontroller, sensors, programming with several development tools.
State so far: Test track completed, Loco, turnouts, LEDs can be controlled. Next: TCRT5000 followed by LCD, TM, DHT and many more as the ideas will fly by.

Will share update soon here in the forum and via GitHub (description & full source).

Early stage picture
1610618842882.png
 
Upvote 0

JMB

Active Member
Licensed User
Longtime User
Looking forward to an update on your progress with this - I've just started looking at using B4R to program a DCC project...
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Looking forward to an update on your progress with this - I've just started looking at using B4R to program a DCC project...
Thanks for your request = Let me give a brief status update on this project named Shunter63.

A first solution is working, means can control a single loco and two turnouts. The controller is an Arduino MEGA with Arduino Motor Shield. Controlled is via Node-RED (serial line) or B4A (Bluetooth classic). An LCD dispay shows the status of the loco and the turnouts.

Have put this project on hold as became rather complex and therfor decided to work further later this year. Now and then I play a bit for fun and to think about new ideas.
Have not documented a lot yet, also wanted to checkout the newer version of the CommandStation-EX v3.1.0 Production Release.
Also want some time to rethink solutions esp with the B4R library rDCCEX.

You can sent me a PM if want to know more.

Screenshot Arduino Mega Controller+Arduino Motor Shield+Bluetooth Classic+Buffer Stop+LCD2004+LED RED,BLUE,YELLOW
1624208058672.png

RED Led ON=Power is ON, BLUE LED ON=Train is running (at 30kmh).

Screenshot B4A Client
1624208335780.png

The DCC Cmd enables to issue DCC commands interactive. The DCC Log shows DCC command responses.

Status
B4R library rDCCEX
- Partial wrapper for the CommandStation-Ex
Based on DCCEX version 3.0.0 Nov 2020.
Completed

B4R DCCEx Controller - Model railway DCC controller handling DCC-EX commands via serial line or bluetooth classic.
Completed.
B4X:
DCC-Commands are received (as text ending with CRLF) via Serial Line (USB) or Bluetooth Classic (Name SHUNTER63, MAC 98:D3:51:FD:A8:19, Pin 1234).
DCC-EX Command Examples (submitted without brackets < >):
- Power ON MAIN: 1 MAIN
- Drive loco ID 3 speed 10 direction right 1: t 1 03 10 1
- Drive loco ID 3 speed 20 direction left 0: t 1 03 20 1
- Emergency Stop train ID 3: t 1 03 -1 1
- In case of an error, the code returned (with backets): <X>
The DCCEx controller has additional functionality:
Buffer stop using TCRT sensor, LCD 2004 I2C to show status loco (speed, direction) & turnouts, LED indicators or stop, running, buffer stop

Controller Client Node-RED
Flow to test controlling, via serial line, one loco (speed, direction, lights,coupling) and two turnouts.
Completed

Controller Client B4A
Test, via Bluetooth classic, controlling one loco (speed, direction, lights,coupling) and two turnouts.
Completed
 
Upvote 0

JMB

Active Member
Licensed User
Longtime User
Rob, that is very much appreciated. I will DM you soon. Annoying stuff like my job keep getting in the way of me doing programming!

Thanks for your comprehensive reply.
 
Upvote 0
Top