MIDI messages are pretty simple, each message consists of an initial byte which defines the message type, generally followed by either one or two bytes which are the parameter values for that message. For example, a note on message will have the channel number (1-16, which IIRC is actually encoded as 0-15) in the message type byte, a second byte defining the note number, and a third defining the velocity. There's going to have to be some sort of queuing involved, as sending two MIDI messages simultaneously would be a recipe for disaster.
Unfortunately I can't be much help as my programming career has been 95% COBOL, with tiny bits of exposure to C, C++, C#, JavaScript, and Python.