Solved

sending messages to and receiving messages from device

Posted on 2004-09-22
12
296 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
  • 5
  • 4
  • 3
12 Comments
 
LVL 1

Assisted Solution

by:mikrotuho
mikrotuho earned 20 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 60 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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 60 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 60 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

860 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