?
Solved

sending messages to and receiving messages from device

Posted on 2004-09-22
12
Medium Priority
?
301 Views
Last Modified: 2010-04-24
I am trying to communicate with a device and so far i have been able to reconfigure the serial port. actually i did not write that code, just copy pasted from msdn example and changed the port number.
I have a DCB and a handle to the CommPort (COM4). the code gets the port state using GetCommState and reconfigures using SetCommState. but this is only configuring the port no actually movement of relevant data is there. how do I read messages sent by the device?

do I use _read(int file_descriptor, void *buffer, uint count)? should I use this function, then where do i get file desriptor from? I used the file handle as file descriptor but I don't think I am doing the right thing here.

thanks

0
Comment
Question by:jhshukla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 1

Assisted Solution

by:mikrotuho
mikrotuho earned 80 total points
ID: 12128818
Dear Jhshukla,

MSDN has a quite extensive article on serial port programming for windows at:

http://msdn.microsoft.com/library/en-us/dnfiles/html/msdn_serial.asp?frame=true&_r=1 

Basically you need to use the ReadFile and WriteFile functions to read from, and write to, you serial port. You get the handle to the file by using the CreateFile function. At the end of communications it is important to close the port by deleting the file handle using the CloseHandle function.

mikrotuho
0
 
LVL 6

Accepted Solution

by:
Svetlin_Panayotov earned 240 total points
ID: 12130256
As mikrotuho writes - in MSDN there are good articles on serial programming.I'd also recommend looking at this link:
http://www.programmersheaven.com/zone15/cat622/index.htm - here you can find some source & libraries

Svetlin
0
 
LVL 1

Expert Comment

by:mikrotuho
ID: 12141825
There seems to be quite a lot of good examples behind Svetlins link. The sample on the MSDN page is quite excessive.

mikrotuho
0
New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

 
LVL 9

Author Comment

by:jhshukla
ID: 12142909
yup.
one more doubt. what am I doing wrong here?
while(1){
  ReadFile(port, inBuffer, BUFSIZE, &numRead, NULL);
  print_inBuffer();
}

should I do something like
while(1){
  wait_for_some_signal(); //periodic
  ReadFile(port, inBuffer, BUFSIZE, &numRead, NULL);
  print_inBuffer();
}
the signal would be sent by some other thread that just examines NUM_TICKS in a tight loop.
0
 
LVL 6

Assisted Solution

by:Svetlin_Panayotov
Svetlin_Panayotov earned 240 total points
ID: 12143750
The second loop is better - and maybe you should be doing all reading/writing in a separate thread.For wait_for_some_signal() you can use mutex or just golbal variable (as flag).Using such variable is not the best practice - but it's easier.

Svetlin
0
 
LVL 9

Author Comment

by:jhshukla
ID: 12144107
>> For wait_for_some_signal() you can use mutex or just golbal variable (as flag).
>> Using such variable is not the best practice - but it's easier
I don't want to use mutex because that's not what I am trying to do. I cannot have queues in this case. the reason is there are three devices to read from - camera, a GPS and a gyroscope - and I want to sync all of them. the way I plan to do it is to create 3 threads and one timer thread that sends periodic signals. all three devices then receive the signal and capture the data.
I don't want to use global flag of somekind because that sends my program into busy-wait and affects performance. important issue because handheld do not have as much processing power.

well but that answers my question. i should Read periodically.

do you think that the device could possibly overwrite the data while I am reading it? the device driver developers must have taken care of that. right?

about periodic signals what is a good way to determine if i should send a signal. i was thinking about
abs(NUM_TICKS % interval) < tolerance.
0
 
LVL 6

Expert Comment

by:Svetlin_Panayotov
ID: 12153992
Well, you can't be really sure what the driver developers took care of :)
And about the interval - it all depends on your device.
Svetlin
0
 
LVL 9

Author Comment

by:jhshukla
ID: 12156587
>> Well, you can't be really sure what the driver developers took care of :)
then how do you make sure that consequent reads from the data are not redundant?
0
 
LVL 6

Assisted Solution

by:Svetlin_Panayotov
Svetlin_Panayotov earned 240 total points
ID: 12157591
If that's what you're worried about - the port acts like stream.So there are no redundant reads :)
Svetlin
0
 
LVL 9

Author Comment

by:jhshukla
ID: 12157748
that simplifies a lot of things. thanks! and I am increasing the question's value to 80 points.
0
 
LVL 1

Expert Comment

by:mikrotuho
ID: 12158763
thanks :)
0
 
LVL 6

Expert Comment

by:Svetlin_Panayotov
ID: 12158882
Good luck with your project :)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question