[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

sending messages to and receiving messages from device

Posted on 2004-09-22
12
Medium Priority
?
302 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
[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

656 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