Solved

sending messages to and receiving messages from device

Posted on 2004-09-22
12
292 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
Comment Utility
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
Comment Utility
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
Comment Utility
There seems to be quite a lot of good examples behind Svetlins link. The sample on the MSDN page is quite excessive.

mikrotuho
0
 
LVL 9

Author Comment

by:jhshukla
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:Svetlin_Panayotov
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks :)
0
 
LVL 6

Expert Comment

by:Svetlin_Panayotov
Comment Utility
Good luck with your project :)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now