Skip to content

blekenbleu/Fake8

Repository files navigation


Fake8:  SimHub 8-bit serial plugin

As noted in Arduino for STM32 Black 'n Blue Pills, ESP32-S[2,3] ,
SimHub's Custom Serial devices plugin has limitations

  • SimHub plugin Javascript is relatively inefficient, hard to debug and maintain.
  • Plugin can log but not process received serial port messages e.g. from Arduino.
  • Serial data is limited to 7 bits per character.

This Fake8 SimHub C# plugin connects to an STM32 Arduino USB COM port,
using 8 bit characters and SimHub properties,
some of which are from SimHub's Custom Serial device plugin messages
via a second serial port and signed virtual com0com Null-modem.
This leverages the SimHub Custom Serial devices plugin user interface:

... while most heavy lifting gets done by this Fake8 plugin.
Sadly, Custom Serial devices user interface Settings are local to that plugin and inaccessible elsewhere.
Consequently, a Custom Serial devices profile sends control settings for processing by this plugin,
via a COM8 com0com null modem Serial port.
Overhead is minimized by using simple NCalc expressions to generate setting change messages.
Unlike JavaScript, NCalc Update messages repeat even if unchanged unless explicitly conditional by change().
Incoming COM8 serial data to SimHub's Custom Serial plugin may combine Arduino and Fake8 strings.

Inspired by MIDI, Fake8 to Arduino 8-bit protocol supports 73 commands:

  • For [re]synchronization, only the first byte of each command has msb == 1
  • For 63 of 73 commands. 5 lsb of first byte is an Arduino sketch-specific command:
    • setting e.g. PWM parameters:  (frequency, % range, predistortion, pin or clock number)
    • second byte is either 7-bit data or count for appended 7-bit byte array of values.
      • One array command echoes that array.
      • One non-array command echoes that second byte.
  • For 3-byte commands, 2 lsb of first byte are 2 msb of 16-bit data.
  • For a single 2-byte command, 5 lsb of first byte are 5 msb of 12-bit data.
  • A single 1-byte command restarts Arduino run-time sketch.

Status 3 Mar 2023

  • plugin communicates both with SimHub Custom Serial plugin (via com0com) and STM32 Arduino
    • current Arduino sketch merely echos ASCII hex for received bytes, confirming 8-bit communications
  • next step will be adding configurable PWM to the Arduino sketch
    for e.g. PC fans and Direct Drive harness tension testing.

Status 8 Mar 2023

Status 9 Mar 2023

  • unable to get both COM ports working robustly in a single class.
    Simply create properties from Custom Serial port messages in Fake7 plugin;
    then, use those properties in Fake8 plugin for Arduino.
    This will not impact game latency, since telemetry will not come thru Custom Serial plugin.
  • look into building both plugins in a single project
    search results

Status 10 Mar 2023

  • The problem is Fake7 hanging on write back to Custom Serial via com0com;
    Read works ok, and and both Read and Write work to e.g. Arduino Serial Monitor.
    Changed F8.ini Fake8rcv setting to f9 from Arduino,
    allowing Fake7 to read a property that changes without Fake8.

Status 11 Mar 2023

  • Confirmed that Custom Serial Incoming serial data and com0com are by default incompatible;
    FWIW, Arduino serial terminal works fine on COM8 instead of SimHub's Custom Serial device...??!!
    finally got beyond CustomSerial.Write(prop); timeout by forcing com0com setting:
    change CNCB0 PortName=COM2,EmuOverrun=yes,ExclusiveMode=no,cts=on,dsr=on,dcd=on
    EmuOverrun=yes by itself did not suffice;  isolating essential setting is low priority.
  • Both ports work;  Fake8receiver() can call Fake7.CustomSerial.Write(),
    but needs exception handling (e.g. reopen)

Status 12 Mar 2023

  • Recover Arduino USM COM disconnect/reconnect events; similar code for com0com implemented but untested.

Status 20 Mar 2023

  • Delegate fork merged to main.

Status 21 Mar 2023

  • Write() timeout recovery for Custom Serial
    • Arduino queues a power-on message
    • SimHub initializes Fake8 before Custom Serial
      • Fake8 Arduino message Write() to Custom Serial times out.
      • could increase Write() timeout, but recovery is generally wanted.
    • Custom Serial port now gets checked at each DataUpdate()

Problems encountered

  • SourceForge's com0com virtual null modem package does not work on recent Windows 10 versions.
  • Trying to use com0com virtual COM ports in C# fails unless its PortName begins with COM.
    • Free 'busy' COM port numbers using COM Name Arbiter Tool
      Run as Admin, uncheck wanted and currently unused ports:
  • Arduino.DtrEnable = true; is required for C# to read from Arduino, but not for com0com.
  • Unable to restart Arduino sketch by toggling Arduino.DtrEnable and Arduino.RtsEnable.
  • Unable to get both COM ports working robustly in a single class.
  • Working example using delegate for data from C# Serial Port DataReceived thread to invoking thread.
  • SimHub Custom Serial device receiving (Incoming serial data) seems uniquely incompatible with com0com.
  • Run as Adminstrator com0com\setupc.exe:   (see com0com ReadMe for instructions)
command> change CNCB0 PortName=COM2
       CNCA8 PortName=-
       CNCB8 PortName=-
       CNCA0 PortName=FAKE8
       CNCB0 PortName=SIM8
change CNCB0 PortName=COM2
Restarted CNCB0 com0com\port \Device\com0com20
ComDB: COM2 - logged as "in use"
command> change CNCB0 ExclusiveMode=yes
       CNCA8 PortName=-
       CNCB8 PortName=-
       CNCA0 PortName=FAKE8
       CNCB0 PortName=COM2
change CNCB0 PortName=COM2,ExclusiveMode=yes
Restarted CNCB0 com0com\port \Device\com0com20
command> change CNCA0 PortName=COM8
       CNCA8 PortName=-
       CNCB8 PortName=-
       CNCA0 PortName=FAKE8
change CNCA0 PortName=COM8
Restarted CNCA0 com0com\port \Device\com0com10
       CNCB0 PortName=COM2,ExclusiveMode=yes
ComDB: COM8 - logged as "in use"
command> change CNCA0 PlugInMode=yes
       CNCA8 PortName=-
       CNCB8 PortName=-
       CNCA0 PortName=COM8
change CNCA0 PortName=COM8,PlugInMode=yes
Restarted CNCA0 com0com\port \Device\com0com10
       CNCB0 PortName=COM2,ExclusiveMode=yes
command> list
       CNCA8 PortName=-
       CNCB8 PortName=-
       CNCA0 PortName=COM8,PlugInMode=yes
       CNCB0 PortName=COM2,ExclusiveMode=yes

Seemingly, PlugInMode=yes and ExclusiveMode=yes make no difference..

marginally useful C# serial port references

C# delegate and callback words of wisdom

  • callback()s run in other threads, can set only statics while invoked
    • have some static class for handling callback updates asynchronously

Future madness

  • STM32 Blue Pills are relatively cheap and easy by BluePill
    • configuring as other than COM port, not so much
  • Merge Fake8 with MIDIio, so that e.g. asynchronous serial reads from Blue Pill
    can be directly written to e.g. vJoy or MIDI

About

SimHub Custom Serial port 8-bit helper plugin sharing data between 2 COM ports

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published