473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help routing hardware response to correct method call

I have a piece of hardware connected via serial port. I need to send a
variety of commands to the box and route the responses back from the
receive thread to the correct method call.

So e.g. I have a method call something like:

SomeCommand(inParam1, inParam2, outParam1, outParam2)
{
Command cmd = new Command( inParam1, inParam2);
HwCommLine.Send( cmd);
// something happens
Response resp = new Response( outputFromBox );
outParam1 = resp.outValue1;
outParam2 = resp.outValue2;
}

I'm not sure how best to design the "something happens" step to get the
response (outputFromBox) from the receive thread back to the method.

It's ok for the method to block/wait for the response to be available
but there may be many requests being processed at any given time.

Any help/suggestions would be appreciated.

Thanks
Dave

Nov 17 '05 #1
3 1487
Dave,

It sounds like you can have multiple commands and responses going over
the line at the same time. In order to do this, you will have to have a
separate thread which handles the receiving of the responses from the serial
port (it would try to constantly read from the port).

When you send the command, you would have to create a unique identifier
that relates to the command (you could use the thread id that the call is
being made on, since you said you can block the thread while waiting for the
response, but if you made this an async operation, you have to use another
id which is unique). You would then create a ManualResetEvent which you
store in a hashtable, keyed on that id.

You send your message, then have the calling thread wait on that
ManualResetEvent.

In your thread that processes responses, when you get the response that
corresponds to the request that was sent, you take the ManualResetEvent from
the hashtable and then set it, which will release the thread that called
WaitOne. Of course, right before calling this, you would place the details
of the response in a queue somewhere that can be accessed by the thread that
made the request.

While this will work, it won't scale well, and if you have a huge number
of responses, you might want to consider using the thread pool to break up
the code so that the request and the handling of the response are performed
on threads in the thread pool (instead of making threads wait).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dave" <pi******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
I have a piece of hardware connected via serial port. I need to send a
variety of commands to the box and route the responses back from the
receive thread to the correct method call.

So e.g. I have a method call something like:

SomeCommand(inParam1, inParam2, outParam1, outParam2)
{
Command cmd = new Command( inParam1, inParam2);
HwCommLine.Send( cmd);
// something happens
Response resp = new Response( outputFromBox );
outParam1 = resp.outValue1;
outParam2 = resp.outValue2;
}

I'm not sure how best to design the "something happens" step to get the
response (outputFromBox) from the receive thread back to the method.

It's ok for the method to block/wait for the response to be available
but there may be many requests being processed at any given time.

Any help/suggestions would be appreciated.

Thanks
Dave

Nov 17 '05 #2
Thanks Nicholas, you pretty much nailed the approach I was intending to
take. I don't want to end up with scaling issues but it isn't obvious
to me how the MSDN example of Thread Pooling works here.

To expand on my original post, this app will be a class derived from
MarshallByRef and hosted in a Windows service with methods accessed
over a tcp channel. So a client makes a method call that gets remoted
to the service. Going back to my original example (and leaving out a
few details) then:

SomeCommand(inParam1, inParam2, outParam1, outParam2)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(SomeCommandProc));
}

void SomeCommandProc(Object stateInfo)
{
Command cmd = new Command( inParam1, inParam2);
HwCommLine.Send( cmd);
// something happens
Response resp = new Response( outputFromBox );
outParam1 = resp.outValue1;
outParam2 = resp.outValue2;
}

I'm obviously missing something here because now I don't see how I can
get my output values back to the client. Should my client be providing
a callback delegate so the service can return the results from the
serial port receive thread?

Thanks
Dave

Nov 17 '05 #3
Thanks Nicholas, you pretty much nailed the approach I was intending to
take. I don't want to end up with scaling issues but it isn't obvious
to me how the MSDN example of Thread Pooling works here.

To expand on my original post, this app will be a class derived from
MarshallByRef and hosted in a Windows service with methods accessed
over a tcp channel. So a client makes a method call that gets remoted
to the service. Going back to my original example (and leaving out a
few details) then:

SomeCommand(inParam1, inParam2, outParam1, outParam2)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(SomeCommandProc));
}

void SomeCommandProc(Object stateInfo)
{
Command cmd = new Command( inParam1, inParam2);
HwCommLine.Send( cmd);
// something happens
Response resp = new Response( outputFromBox );
outParam1 = resp.outValue1;
outParam2 = resp.outValue2;
}

I'm obviously missing something here because now I don't see how I can
get my output values back to the client. Should my client be providing
a callback delegate so the service can return the results from the
serial port receive thread?

Thanks
Dave

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Morgan Leppink | last post by:
Hey all - We are running SQL 2000 with ALL available service packs, etc. applied. We just built a brand new database server, which has dual 2Ghz XEONs, 2GB memory, and the following disk...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
6
by: AppleBag | last post by:
I'm having the worst time trying to login to myspace through code. Can someone tell me how to do this? Please try it yourself before replying, only because I have asked this a couple of times in...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.