Thread: Basic Serial Communication

  1. #1
    Registered User
    Join Date
    Aug 2009

    Basic Serial Communication

    I'm fairly unfamiliar with using C++ but I need to put together a simple code to communicate with a microprocessor. This is being down on a Windows Xp/Vista platform since I believe that matters. The serial code is part of a larger project being done in C++ so I am pretty much tied to this language.

    Can anyone show me some simple code or point me to a thread with a simple example on how to read/write characters over RS232? The task is very basic in that I write a few commands and then continually read in from the microprocessor.

    Thanks in advance for any assistance.

  2. #2
    Registered User
    Join Date
    Mar 2003
    Lookup CreatFile(), ReadFile() and WriteFile(), for example:
    char Data[1];  // ? a byte or what ever size you need	etc...
    int BytesWritten, BytesRead;
    HANDLE hComPortHandle;
    // To read from the COM1 port:
    hComPortHandle = CreateFile("COM1", GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
    	ReadFile(hComPortHandle, &Data, sizeof(data), (unsigned long*)&BytesRead, NULL);
    	//Do something with Data
    // To write to the COM1 port:
    ComPortHandle = CreateFile("COM1", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
    	// setup Data then write to port...
    	WriteFile(hComPortHandle, Data, sizeof(Data), (unsigned long*)&BytesWritten, NULL);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic Serial Communication (Writing)
    By beavis601 in forum C Programming
    Replies: 1
    Last Post: 08-17-2009, 04:05 PM
  2. Serial port Communication
    By vin_pll in forum C++ Programming
    Replies: 23
    Last Post: 01-07-2009, 08:32 AM
  3. Please help with serial communication problem - Long
    By spdylude in forum Windows Programming
    Replies: 3
    Last Post: 04-06-2005, 09:41 AM
  4. Serial communication packets
    By Roaring_Tiger in forum C Programming
    Replies: 3
    Last Post: 04-26-2003, 08:33 AM