Improving the SysEx reception thread

The main problem when receiving SysEx dumps is that I supposed that all the incoming data is always greater than 3 bytes. Therefore, could a system exclusive message with less than 3 bytes be received? I think so, and if it happens we will not be able to get it by the same overloaded function. 

An open forum is available to discuss about this issue 

In the next table it is shown the main thread for receiving MIDI messages that needs improving:

    
// Main thread for processing incoming MIDI events
   

DWORD CInputPort::WaitForEvent(LPVOID lpv)
{

  HRESULT hr;
  REFERENCE_TIME rt;	// System time when the event is received
  DWORD dwGroup;		// Channel group
  DWORD dwcb,dwMsg;	// Byte counter, Midi message 
  BYTE *pb;		// Pointer to the data

	CInputPort *ptrPort=(CInputPort*)(lpv);

 	while(ptrPort->m_ProcessActive)	// Check for active
	{
		WaitForMultipleObjects(1,&ptrPort->m_hEvent, FALSE, INFINITE); // Waits for an event

		while (ptrPort->m_ProcessActive)
		{
			hr = ptrPort->ReadBuffer();	// Read incoming midi messages

			if (hr == S_FALSE)
			{
				break;  // No more messages to read into the buffer
			} 

			ptrPort->ResetBuffer();// Set the buffer pointer to the start
      

			while (ptrPort->m_ProcessActive) 
			{

				hr = ptrPort->GetMidiEvent(&rt,&dwGroup,&dwcb,&pb); // Extract midi data

				if (hr == S_OK)
				{

					if (dwcb>MIDI_STRUCTURED)	// In case it is SysEx data
						ptrPort->m_pReceiver->RecvMidiMsg(rt,dwGroup,dwcb,pb);

					else
					{
						CopyMemory(&dwMsg,pb,dwcb); // Structured data (Standard format) 
						ptrPort->m_pReceiver->RecvMidiMsg(rt,dwGroup,dwMsg);
					}

					// pb points to the data structure for the message, and

				}	// you can do anything that you want with it
				else if (hr == S_FALSE)
				{
					break;  // No more messages in the buffer
				}
			}  // Done with the buffer

 		}  // Done reading pending events
	}

 	SetEvent(ptrPort->m_hTerminationEvent); 

	return 0;

}
  

These are some of the situations that could happen (sorted by relevance):

1) We recive a SysEx message shorter than 3 bytes

F0 43 F7              

2) We never receive the EOX

F0 43 25 .. .. .. .. .. .. !EOX

Forum link

Back