LCC Fusion Project 1.0
LCC Automation
Loading...
Searching...
No Matches
LccNodeSlcanSerialBridge< SerialType > Class Template Reference

Bridges Python application serial communication with the CAN bus. More...

#include <LccNodeSlcanSerialBridge.h>

Inherits Executable.

Collaboration diagram for LccNodeSlcanSerialBridge< SerialType >:

Classes

class  WritePort
 State machine running in the OpenMRN cooperative executor, registered to receive data that needs to be sent to the port. More...
 

Public Member Functions

 LccNodeSlcanSerialBridge (SerialType *port, CanHubFlow *pCanHub)
 Constructor. More...
 
 ~LccNodeSlcanSerialBridge ()
 Destructor.
 
void run ()
 Called by the loop to check for possible serial or GridConnect packets to be read and processed. More...
 

Private Member Functions

void _loop_for_read ()
 
uint8_t _encodeCanFrameToGcPacket (const can_frame *pCanFrame, unsigned char buf[])
 Encode a CAN frame (struct) to a GridConnect packet in the following format: ':' (0x3A) start of packet 'X' (0x58) extended id (4 bytes) ID converted from 4-bytes to 8 byte ascii 'R' (0x52) remote indicator data bytes converted to 2-byte ascii ';' (0x3F) end of packet. More...
 
char nibble_to_ascii (const uint8_t nibble)
 Converts a nibble to its ASCII character representation. More...
 
String _readSlcanFrameFromSerial ()
 Read a CAN frame from the serial port. More...
 
bool _decodeSlcanFrameToCanFrame (const String *pSlcanFrame, can_frame *pCanFrame)
 Deocode a Slcan frame to CAN frame: More...
 
void _loop_for_write ()
 
bool _decodeGcPacketToCanFrame (can_frame *pCanFrame)
 Decodes a GridConnect packet in to a CAN frame (struc) Supports multiple frames of data within a packet, but only processes one frame of data per invocation (uses writeOfs_ to keep position within the packet) More...
 
String _encodeCanFrameAsSlcanFrame (const can_frame *pCanFrame)
 Encodes a CAN frame into a Slcan Frame. More...
 
void _printHex (const uint8_t *data, const uint8_t length)
 Print data in hexadecimal form for debugging. More...
 

Private Attributes

SerialType * pPort_
 Serial port object.
 
Service * pService_
 Access to the stack's executor.
 
HubFlow txtHub_ { pService_ }
 Hub for the textual data.
 
Buffer< HubData > * pWriteBuffer_ { nullptr }
 Buffer for outgoing data.
 
WritePort writePort_ { this, pService_ }
 State flow with queues for output frames generated by the stack.
 
size_t writeOfs_
 Offset into GC packet of data being processed (in case there is more than one frame within the packet)
 

Detailed Description

template<class SerialType>
class LccNodeSlcanSerialBridge< SerialType >

Bridges Python application serial communication with the CAN bus.

This class handles bridging between OpenMRN GridConnect packet format and the expected CAN frame format over used by Python CAN-libary with serial communication.

Definition at line 69 of file LccNodeSlcanSerialBridge.h.

Constructor & Destructor Documentation

◆ LccNodeSlcanSerialBridge()

template<class SerialType >
LccNodeSlcanSerialBridge< SerialType >::LccNodeSlcanSerialBridge ( SerialType *  port,
CanHubFlow *  pCanHub 
)
inline

Constructor.

Parameters
portSerial port object (HardwareSerial, SoftwareSerial, or BluetoothSerial).
pCanHubCore CAN frame router of the OpenMRN stack, usually from stack()->pCanHub().

Format of the CAN Frame over Serial is defined here: https://python-can.readthedocs.io/en/stable/interfaces/serial.html 0xAA start of frame, 4-byte timestamp, DLC byte, Arbitration ID - 4 byte, Payload, 0xBB end of frame.

Definition at line 80 of file LccNodeSlcanSerialBridge.h.

Member Function Documentation

◆ _decodeGcPacketToCanFrame()

template<class SerialType >
bool LccNodeSlcanSerialBridge< SerialType >::_decodeGcPacketToCanFrame ( can_frame *  pCanFrame)
inlineprivate

Decodes a GridConnect packet in to a CAN frame (struc) Supports multiple frames of data within a packet, but only processes one frame of data per invocation (uses writeOfs_ to keep position within the packet)

Parameters
pCanFramehold the decode GC packet's frame
Returns
True if a CAN frame was decode from the GC packet's data, otherwise false.

< Get start of a packet's CAN frame

< Check next character

< udpate offset as packet is processed

< Packet found?

< return whether or not CAN data was found and converted

Definition at line 322 of file LccNodeSlcanSerialBridge.h.

Here is the caller graph for this function:

◆ _decodeSlcanFrameToCanFrame()

template<class SerialType >
bool LccNodeSlcanSerialBridge< SerialType >::_decodeSlcanFrameToCanFrame ( const String *  pSlcanFrame,
can_frame *  pCanFrame 
)
inlineprivate

Deocode a Slcan frame to CAN frame:

Parameters
pSlcanFramestring holding slcan frame to be decoded.

Slcan frame start with either a 't' (standard id) or 'T' (extended ID); followed by the ID, DLC byte, and data.

Parameters
pCanFrameholds the decode CAN frame
Returns
True if slcan was decoded

Definition at line 271 of file LccNodeSlcanSerialBridge.h.

Here is the caller graph for this function:

◆ _encodeCanFrameAsSlcanFrame()

template<class SerialType >
String LccNodeSlcanSerialBridge< SerialType >::_encodeCanFrameAsSlcanFrame ( const can_frame *  pCanFrame)
inlineprivate

Encodes a CAN frame into a Slcan Frame.

Parameters
pCanFrameto hold the encoded Slcan frame
Returns
True if a Slcan frame was encode succesffuly, otherwise false.

Definition at line 344 of file LccNodeSlcanSerialBridge.h.

Here is the caller graph for this function:

◆ _encodeCanFrameToGcPacket()

template<class SerialType >
uint8_t LccNodeSlcanSerialBridge< SerialType >::_encodeCanFrameToGcPacket ( const can_frame *  pCanFrame,
unsigned char  buf[] 
)
inlineprivate

Encode a CAN frame (struct) to a GridConnect packet in the following format: ':' (0x3A) start of packet 'X' (0x58) extended id (4 bytes) ID converted from 4-bytes to 8 byte ascii 'R' (0x52) remote indicator data bytes converted to 2-byte ascii ';' (0x3F) end of packet.

Parameters
pCanFrameCAN pCanFrame to encode.
bufBuffer to store the encoded packet.
Returns
Length of the encoded packet.

Definition at line 203 of file LccNodeSlcanSerialBridge.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _loop_for_read()

template<class SerialType >
void LccNodeSlcanSerialBridge< SerialType >::_loop_for_read ( )
inlineprivate

Handles Slcan frame coming from serial port and going out to the OpenMRN Grid Connect Hub. Process Slcan frames from serial by:

  1. converting Slcan frame to CAN frame
  2. encode CAN frame to GC packet
  3. queuing the GC packet with GridConnect Hub

< Allocate a buffer to build the GridConnect frame and data in

< yes, adjust the buffer to the actual size of the packet

Definition at line 157 of file LccNodeSlcanSerialBridge.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _loop_for_write()

template<class SerialType >
void LccNodeSlcanSerialBridge< SerialType >::_loop_for_write ( )
inlineprivate

Handles processing OpenMRN GridConnect packet and sending Slcan frame towards the serial port. Process GC packets to Slcan frames and out to serial by:

  1. Get a single CAN frame from the GC packet
  2. Convert the GC (ascii data) to CAN frame
  3. Write CAN frame to the serial port
  4. Update offset into processing of the GC packet
  5. If done with packet, notify that we're ready for another packet, otherwise continue processing on next invocation of this code.

< Clear the buffer after writing

< Wake up state flow to release buffer and take next from the queue

Definition at line 299 of file LccNodeSlcanSerialBridge.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _printHex()

template<class SerialType >
void LccNodeSlcanSerialBridge< SerialType >::_printHex ( const uint8_t *  data,
const uint8_t  length 
)
inlineprivate

Print data in hexadecimal form for debugging.

Parameters
dataData to print.
lengthLength of the data.

Definition at line 362 of file LccNodeSlcanSerialBridge.h.

Here is the caller graph for this function:

◆ _readSlcanFrameFromSerial()

template<class SerialType >
String LccNodeSlcanSerialBridge< SerialType >::_readSlcanFrameFromSerial ( )
inlineprivate

Read a CAN frame from the serial port.

Returns
Slcan frame as a string, null if no slcan frame read from serial.

Definition at line 254 of file LccNodeSlcanSerialBridge.h.

Here is the caller graph for this function:

◆ nibble_to_ascii()

template<class SerialType >
char LccNodeSlcanSerialBridge< SerialType >::nibble_to_ascii ( const uint8_t  nibble)
inlineprivate

Converts a nibble to its ASCII character representation.

Parameters
nibbleNibble value to convert.
Returns
Converted ASCII character.

Definition at line 245 of file LccNodeSlcanSerialBridge.h.

Here is the caller graph for this function:

◆ run()

template<class SerialType >
void LccNodeSlcanSerialBridge< SerialType >::run ( )
inline

Called by the loop to check for possible serial or GridConnect packets to be read and processed.

< Send to serial any queued GridConnect CAN packets.

< Read from serial slcan frames, convert to GC packets, queue with GC hub.

Definition at line 92 of file LccNodeSlcanSerialBridge.h.

Here is the call graph for this function:

The documentation for this class was generated from the following file: