Like Tree2Likes
  • 1 Post By kuphryn
  • 1 Post By iain

Read/Write Com Port :: C++

This is a discussion on Read/Write Com Port :: C++ within the C++ Programming forums, part of the General Programming Boards category; Hi. Is it easy to read from and write to the com port using C++? Is there a library for ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Read/Write Com Port :: C++

    Hi.

    Is it easy to read from and write to the com port using C++? Is there a library for it?

    Thanks,
    Kuphryn
    Abhinav Ravi likes this.

  2. #2
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    no real need for a library you can do it with basic file functions.
    THis will open the com port, just open it for read and write, you can write characters to it using fputc

    int main (void)
    {
    transfer();
    }

    int transfer ()
    {


    FILE *comport;

    if ((comport = fopen("COM1", "wt")) == NULL)
    {
    printf("Failed to open the communication port COM2\n");
    printf("The port may be disabled or in use\n");
    int wait=getch();
    return 1;
    }
    printf("COM2 opended successfully\n");


    fputc(CTRLZ, comport);
    fflush(comport);
    fclose(comport);
    return 0;
    }
    Abhinav Ravi likes this.
    Monday - what a way to spend a seventh of your life

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. Thanks.

    Some members recommended MSDN's Create, Read and Write functions.

    Kuphryn

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    C++ Style:

    ifstream readCOM;
    readCOM.open(com1, ios::in | ios::binary)

    char *ptrStore;

    try
    {
    ptrStore = new char[SIZE];
    }
    catch (bad_alloc error)
    {
    cerr << error.what();
    exit(1);
    }

    readCOM.read(reinterpret_cast<char *> (ptrStore), SIZE);

    How do you get the exact size of whatever you're reading from the COM port? It is not a measurable storage container.

    Do I have to use an MFC class? What is the major difference if I were to use the ifstream and ofstream instead of an MFC class? I have no experience with MFC or windows programming.

    Kuphryn
    Last edited by kuphryn; 12-27-2001 at 12:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FTP program
    By jakemott in forum Linux Programming
    Replies: 14
    Last Post: 10-06-2008, 02:58 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 06:49 PM
  3. Segmentation Fault - Trying to access parallel port
    By tvsinesperanto in forum C Programming
    Replies: 3
    Last Post: 05-24-2006, 04:28 AM
  4. How to read/write to parallel port?
    By Pooyae in forum C Programming
    Replies: 9
    Last Post: 03-10-2005, 09:21 PM
  5. how to control pinter port read/write using C++
    By lwong in forum Windows Programming
    Replies: 4
    Last Post: 11-25-2003, 09:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21