Serial interfacing from Windows using C

The following code connects to an mbed via the serial interface (use the default serial code sample to test)

compile using MINGW gcc

#define WIN32_LEAN_AND_MEAN
#define NOSOUND
#include <windows.h>
#include <assert.h>
static int main(void){
    // the backslash jungle: see http://support.microsoft.com/kb/115831
  HANDLE hSerial = CreateFile
    ("\\\\.\\COM17",      
     GENERIC_READ | GENERIC_WRITE,    
     0,                  
     NULL,               
     OPEN_EXISTING,      
     0,                  
     NULL                
     );
  
  assert(hSerial != INVALID_HANDLE_VALUE);
  
  DWORD tmp = 0;
  int r = WriteFile(hSerial, "uuuuuuuuuu", 10, &tmp, NULL);
  assert(r);

  Sleep(1000);
  r = WriteFile(hSerial, "dddddddddddddd", 10, &tmp, NULL);
  assert(r);
  CloseHandle(hSerial);
  return 0;
}


Please log in to post comments.