• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

writing o a com port

Can I use the WriteToFile Method to write to a com port. Similarly how can I read the data from the com port?
Thanx in advance
0
Abbas071098
Asked:
Abbas071098
  • 3
  • 2
1 Solution
 
nil_dibCommented:
This answer is only for microsoft environments. If you don't work under/with MS reject
my answer.
You have to use the WriteFile function!
First of all you have to open the serail port with the CreateFile function.
Then call SetupComm, SetCommState, SetCommMask, SetCommTimeouts ....
After that call WriteFile and ReadFile to write and read from the port.
A good sample how to open/initialize/read/write serial ports is mttty.                                    
For more information look at http://www.microsoft.com/win32dev/base/serial.htm
or at http://www.codeguru.com/ (search for "serialport") where you can find a class which handles the serail port comunication.

0
 
Abbas071098Author Commented:
Thanx nil_dib. I accept your answer and am awarding you the deserving points. but is it possible to get the handle if the serial port is exclusively owned by some driver say if the mouse is connected to the serial port? I haven't tried it but I think this would cause some problems
0
 
nil_dibCommented:
I think you are right. Once I tried to open/read from a com port which was used by
another application ... and that didn't work( CreateFile returned port allready
open or something..). But you can try to get the port handle not from CreateFile
(which creates the handle) but from another function which should catch the handle
to the open port.
0
 
Abbas071098Author Commented:
Do u know nil_dib what can be the other function to get a handle to the port?...
0
 
nil_dibCommented:
mmhhh,
I believe the method whow to get the handle, depends on the type
of driver owned the port (exe,dll,system,vxd ...) and on the
way the port was opened
( read http://www.microsoft.com/win32dev/base/serial.htm
chapter: Overlapped I/O).
But: this is only a suggestion ... I'm not sure whether it
is possible to write/read to a port which is owned by another
appliction or driver.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now