Serial Communication project.

I have the opportunity to add Arduino support to a video game for use as a modable custom controller.
I am planning to support up to 4-6 devices with detection and COM mapping in the games options screens.

This is not going to be a keyboard/joystick replacement. Rather, it is going to take internal data from the game (health, ammo counts, etc. ) and output them to the serial interface for processing and external display with arduinos. In other words, HUD data. I also plan to have external controls tied to un-keymappable game functions being sent back to the game through the game's scripting support.

What I have planned so far is this:

  • One class in the game to stream game data to the appropriate COM port
  • Another class to receive data from the COM port and buffer it until the in-game script checks for data
  • The Game's API script will gather data to send and check for incoming data to process
  • Incoming and outgoing data will probably be handled with separate Arduinos

The main problem I'm having is determining a data format for the serial streams.
I may send integers/floats larger than 1,000,000,000,000(trillion)
I may send arrays of data to the arduino containing string Identifiers, MinVal, Max Val, and CurrentVal that need to be processed together so I know which LEDs to light in a bar, display on 7 segment LED banks etc.

Receiving a single datatype over serial seems straight forward and easy enough to do, however, I am hitting a wall on how to deal with multiple data types, and how to approach encoding/decoding bytes.

I saw one approach that sent everything as strings with a start and stop character and processed the message a single char at a time, however this seems like it would have sub-par performance having to cast the string values into their respective types for math processing. The perceived benefit to this approach was that the various read methods wouldn't block the port and would allow incoming data to flow more freely.

The second issue is dealing with data I don't want, clogging up the buffer.
The game script might send Health, Ammo, and Energy to the COM port, but in my project I might only want to use One or Two of the values being sent. I need a way to purge anything I don't explicitly need from the incoming buffer while keeping any messages I havent processed yet, in tact. I know that sounds insane, but this project is about letting users build custom hardware UIs for gaming. Having to deal with API scripting is a level of complexity I can remove by publishing an API script that grabs and sends everything and lets the user focus on Hardware builds and sketch writing.

Oh, I forgot one other thing I'd like to do for future proofing the serial classes in the game itself.
There is a possibility I can access an external camera source, For example a rear view camera, and stream the rendered frame to a COM port as well. I know the Arduinos can't handle the desired resolution, however, other project controllers can(Pis for instance). The important thing is that the serial format I implement will need to accommodate a video stream as well as the other data above.

Hopefully, this project will open up a whole new project line for experimenters to play with. Especially if more game developers decide to support devices in this manner.

You need to devise a protocol so that you can identify the nature of the bytes recieved and so decode them accordingly.
This could involve converting all numbers into the same format, or always sending the same data type sequence or tagging individual types of data.

This sort of thing is very common in computing and many diffrent ways have been used in the past. I would advise that you look at some. I have been doing some work with Lego Mindstorms and they have lots of diffrent ways to do this, you might want to look at their protocols for some examples.

But document what you do, don't try and remember it and leave some scope for future expansion.

Note an Arduino can not handle any sort of video information.