Thread: accessing my com port, writing and reading data

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    108

    accessing my com port, writing and reading data

    i´m writing a little bit of data to a device connected to COM2 of my computer.

    then i have to wait and collect all the data that comes through it.

    from reading the FAQ´s and so on, i have gathered that i need to

    A) open the port
    B) handshake and set the correct parameter
    C) send my message through the port
    D) read all messages that come through.

    i wanted to know if i have all the steps down correctly or did i miss any out?

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    
    {
       DCB dcb;
       HANDLE hCom;
       BOOL fSuccess;
       char *pcCommPort = "COM2";  //Set at com 2 for moment
    
       hCom = CreateFile( pcCommPort,
                        GENERIC_READ | GENERIC_WRITE,
                        0,    // must be opened with exclusive-access
                        NULL, // no security attributes
                        OPEN_EXISTING, // must use OPEN_EXISTING
                        0,    // not overlapped I/O
                        NULL  // hTemplate must be NULL for comm devices
                        );
    
       if (hCom == INVALID_HANDLE_VALUE) 
       {
           // Handle the error.
           printf ("CreateFile failed with error %d.\n", GetLastError());
           return (1);
       }
    
       // Build on the current configuration, and skip setting the size
       // of the input and output buffers with SetupComm.
    
       fSuccess = GetCommState(hCom, &dcb);
    
       if (!fSuccess) 
       {
          // Handle the error.
          printf ("GetCommState failed with error %d.\n", GetLastError());
          return (2);
       }
    
       // Fill in DCB: 9,600 bps, 8 data bits, no parity, and 1 stop bit.
    
       dcb.BaudRate = CBR_9600;     // set the baud rate
       dcb.ByteSize = 8;             // data size, xmit, and rcv
       dcb.Parity = NOPARITY;        // no parity bit
       dcb.StopBits = ONESTOPBIT;    // one stop bit
    
       fSuccess = SetCommState(hCom, &dcb);
    
       if (!fSuccess) 
       {
          // Handle the error.
          printf ("SetCommState failed with error %d.\n", GetLastError());
          return (3);
       }
    
       printf ("Serial port %s successfully reconfigured.\n", pcCommPort);
       return (0);
    }
    so far i have only this code to show for it and would like to know if the problem i have is complex and if i´m doing it right?

    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Seems OK so far....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    My application freezes b/c I forget to connect a device to the port. Is there way i can check if the com port is open or connected to a device?
    Last edited by NewGuy100; 09-16-2005 at 08:27 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The error status return might say something to that effect.
    Try RTFM
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    Can you elaborate more on that? Whats RTFM?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Read The Fantastic Manual

    http://msdn.microsoft.com/library/de...createfile.asp
    Just in case....
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    Can you give me an example?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Of reading?

    Oh, you mean
    The error status return might say something to that effect.
    Well, like this:

    Code:
    int result;
    
    result = thefunction();
    
    if(result == ERRORCODE) report_error();
    You know. Like fgetc() (or getc()).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. serial port to poll on request
    By infineonintern in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 06:52 AM
  2. reading and writing from a COM port
    By abq_guy in forum C Programming
    Replies: 1
    Last Post: 04-20-2009, 04:57 PM
  3. Reading and writing to a serial port
    By SwarfEye in forum C Programming
    Replies: 2
    Last Post: 08-18-2006, 12:28 AM
  4. Help with Reading and writing binary data
    By Yasir_Malik in forum C Programming
    Replies: 3
    Last Post: 12-12-2004, 09:24 AM
  5. reading and writing data
    By chrismiceli in forum C Programming
    Replies: 1
    Last Post: 09-14-2003, 11:48 AM