Thread: serial port communication using c++

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    4

    serial port communication using c++

    sir,
    i have been asked to do serial port programming using C++

    it should include opening port, close and view the status of the serial port. You should also be able to control speed, timeout,start bit,stop bit,parity bit and stuff like that

    please provide me some solution in the form of sample programs,books or techniques..
    since i am new to programming,i dont know from where and how should i start

    regards,
    mayuri

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well it all depends on which Operating system and compiler you're using.
    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
    Apr 2011
    Posts
    4
    see i am using win CE
    and microsoft visual studio
    pls provide a solution instead of making things difficult

  4. #4
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    Mayuri_608,

    I opened the free copy Visual C++ that Microsoft allowed me to download as part of their Express line of products. Then I went to the help menu, and I selected the Microsoft Developer Network (MSDN) Forums. I ran a search for "serial port ioctl," and this is the resulting page: serial port ioctl - MSDN Search.

    Due to personal goals, I genuinely prefer to do low level i/o in Linux when I can, but I have done some work with the Microsoft facilities...their documentation is often more complete and support is a lot more standard than Linux once was. Check out the search results, I think that Windows CE came up second or third in the results too. I don't mean to be difficult, but last time I checked the board was more like "...show and tell..." rather than "...would you like fries with that...." I would be delighted to work through your attempts with you.

    Best Regards,
    Kept the text books....
    Went interdisciplinary after college....
    Still looking for a real job since 2005....

    During the interim, I may be reached at ELance, vWorker, FreeLancer, oDesk and WyzAnt.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    4

    serial port communication using c++

    Code:
     
    // serial.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	return 0;
    }
    
    
    #undef UNICODE
    #include "stdafx.h"
    #include <windows.h>
    
    
    
    void PrintError( LPCSTR str)
    {
    LPVOID lpMessageBuffer;
    int error = GetLastError();
    FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER |
    FORMAT_MESSAGE_FROM_SYSTEM,
    NULL,
    error,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), //The user default language
    (LPTSTR) &lpMessageBuffer,
    0,
    NULL
    );
    printf("%s: (%d) %s\n\n",str,error,lpMessageBuffer);
    LocalFree( lpMessageBuffer );
    }
    
    int main(int argc, char* argv[])
    {
    // open port for I/O
    HANDLE h = CreateFile(argv[1],
    GENERIC_READ|GENERIC_WRITE,
    0,NULL,
    OPEN_EXISTING,0,NULL);
    
    if(h == INVALID_HANDLE_VALUE) {
    PrintError("E012_Failed to open port");
    } else {
    // set timeouts
    COMMTIMEOUTS cto = { 1, 100, 1000, 0, 0 };
    DCB dcb;
    if(!SetCommTimeouts(h,&cto))
    PrintError("E013_SetCommTimeouts failed");
    
    // set DCB
    memset(&dcb,0,sizeof(dcb));
    dcb.DCBlength = sizeof(dcb);
    dcb.BaudRate = 19200;
    dcb.fBinary = 1;
    dcb.fDtrControl = DTR_CONTROL_ENABLE;
    dcb.fRtsControl = RTS_CONTROL_ENABLE;
    dcb.fOutxCtsFlow = 1;
    dcb.fRtsControl = DTR_CONTROL_HANDSHAKE;
    
    dcb.Parity = NOPARITY;
    dcb.StopBits = ONESTOPBIT;
    dcb.ByteSize = 8;
    
    if(!SetCommState(h,&dcb))
    PrintError("E014_SetCommState failed");
    
    
    char buf[7];
    DWORD read = 0;
    DWORD write=1; // Number of bytes to write to serial port
    buf[0] = 72; // Decmial value to write to serial port
    WriteFile(h,buf,write,&write,NULL); // write is updated with the number of bytes written
    
    ReadFile(h,buf,sizeof(buf),&read,NULL); // read is updated with the number of bytes read
    DWORD i;
    for (i=0; i<read; i++)
    printf("%i ", (unsigned char)buf[i]);
    
    CloseHandle(h);
    }
    
    return 0;
    }
    hey dis is d program for serial port communication..i was able to compile it but i hav a problem wid o/p
    i am getting this error after compilation
    E012_Failed to open port: <5> Access is denied.

    i have connected serial port also to the pc and set all d parameters in COM1
    pls resolve my query..
    regards,
    mayuri

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    what are you passing as argv[1] to your program?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    4

    serial port communication using c++

    Quote Originally Posted by vart View Post
    what are you passing as argv[1] to your program?

    i m passng COM1 as argument..i m doong dat in project properties
    - Configuration Properties - Debugging - Command Arguments, in Visual Studio

    is it correct?
    if not den pls provide some solution instead of teasing me

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by mayuri_608 View Post
    i m passng COM1 as argument..
    should be ok

    but I would start with hardcoded value
    as in the sample Configuring a Communications Resource (Windows)

    to see if problem is in passing arguments to program or CreateFile itself


    have you use HyperTerminal to see if the port could be opened properly before using your program?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serial port Communication
    By vin_pll in forum C++ Programming
    Replies: 23
    Last Post: 01-07-2009, 09:32 AM
  2. Serial port communication timeout
    By johnfulgor in forum Linux Programming
    Replies: 1
    Last Post: 10-20-2008, 03:07 AM
  3. Serial Port Communication
    By maxorator in forum C++ Programming
    Replies: 11
    Last Post: 04-27-2006, 03:13 PM
  4. serial port communication from two Application
    By lsme in forum Networking/Device Communication
    Replies: 5
    Last Post: 12-12-2005, 11:32 PM
  5. Need help or info about serial port communication
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 01-08-2002, 01:48 PM