CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2001
    Location
    my room in the Hague, Netherlands
    Posts
    12

    how to use a commport

    Heya all,

    my problem is as follows.
    I want to find the comports on a computer and send data to it.

    i was looking for hyperterminal source code, but unlikely that i'll find that.
    So i was hoping that one of you had a little program that could detect the commport and write and receive data from it.
    detection stuff and all that is not neccesary at this moment. i'll hack that later to it.

    if anyone has it or can point me somewhere, then i would be very gratefull.

  2. #2
    Join Date
    Jun 2002
    Posts
    224
    See the attachment.

    Regards,
    Attached Files Attached Files
    ZDF

    What is good is twice as good if it's simple.
    "Make it simple" is a complex task.

  3. #3
    Join Date
    Jul 2002
    Location
    Marseille, France
    Posts
    4
    HI ...

    Found in MSDN :

    char *pcCommPort = "COM2";

    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
    );

    Opens commport COM2 ..

    Hope this helps

    See u. Cliff

  4. #4
    Join Date
    Jul 2001
    Location
    UK
    Posts
    329
    I was looking into this a few months ago when writing a serial port handling class. There is actually quite a lot of stuff out there. Look in the VisualC++ forum archives. It is all slightly different, and one class that EVERYONE was quoting was FULL of bugs.

    I am not sure I can give you my code since I wrote it on company time, but if you are really, really desparate, drop me an email at [email protected] for some tips/chunks of code.

  5. #5
    Join Date
    Feb 2001
    Location
    my room in the Hague, Netherlands
    Posts
    12
    thanks for all the advice.

    The tty.zip is a very long source code file with a lot of windows handle source. I sort of understand the parts of writing, reading, opening, closing the ports. A similar piece of the MSDN that Clifford also appears in the tty.zip.
    But i can't compile the file, cause it needs a windows form, i believe, I can't make heads or tails out of all those windows handle names.

    I am trying to filter out a simple way to write and read to a commport and offcourse detect commport 2. (or is comport to always at the fixed address. Is there some way you can use the static address of comport 2?

    I also have the rs232.c and rs232.h file at my disposal. It should be very easy to use them, but i can not really figure out how.
    I welcome any insight in understanding this kind of material. in the sdk of win32 it isnt all that clear and doenst give me the big picture of all the elements i should take into account.


    ps. i am looking in the VC++ at this moment, hoping to find some
    stuff.
    Last edited by Defiant; August 1st, 2002 at 04:18 AM.

  6. #6
    Join Date
    Jul 2001
    Location
    UK
    Posts
    329
    You're right, MSDN is absolutely cr&p and confusing when it comes to COM ports.

    Ok, here are some points to make;

    Com ports don't have addresses as such. To open com port 2, you just use the string "COM2" in the function call. Presumably if there is no com port 2 it returns an error.

    Com ports can be opened using overlapped IO or not. I don't know how much you know about overlapped IO. It is quite a neat idea in principle, but it sucks in many respects, not least is it's sheer complexity. If your app is a console app, or you are only reading or only writing, you may be able to get away with not using overlapped IO. Otherwise, you need to spawn a separate thread just to handle the com port, AND use overlapped IO.

    If you email me at [email protected], I will send you some sample code that is neatly and clearly written. When I was looking into this, I couldn't find much that was clearly written, and one class I found was SOOOOOO full of obvious bugs. It was quite clearly written though

  7. #7
    Join Date
    Jul 2002
    Location
    Marseille, France
    Posts
    4
    Originally posted by Defiant
    thanks for all the advice.
    I am trying to filter out a simple way to write and read to a commport and offcourse detect commport 2. (or is comport to always at the fixed address. Is there some way you can use the static address of comport 2?
    Ok ... can't imagine a simpliest way than using CreateFile ..
    What's the problem with that?
    Just CreateFile, then you can use ReadFile, WriteFile, etc ...
    and CloseHandle to close it.
    You don't need to use the static address. This address is defined in your system and you just have to use "COM1", "COM2" (i think it works for "LPT1", etc ...

    ps. i am looking in the VC++ at this moment, hoping to find some
    stuff.
    I found everything in the MSDN, in the VC++ section actually.. You'll find what you want really soon

    Cheers

  8. #8
    Join Date
    Apr 2002
    Posts
    14

    MSDN ReadFile Documentation

    Can someone please have a look at the MSDN ReadFile documentation and let me know what they mean by

    'If the return value is nonzero and the number of bytes read is zero, the file pointer was beyond the current end of the file at the time of the read operation.'

    This was taken from the Return Values section in ReadFile.

    I am working in WinCE and I am always getting a return value of non zero, yet with the number of bytes read 0.

    Any help please?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured