question for the experts

  • Thread starter Thread starter jayderk
  • Start date Start date
J

jayderk

I am writing an application that communicates over COM0: with the openCFNet
Serial.IO class. my problem is I do not know how best to go about collecting
the data that comes in from the port not knowing exactly how many bytes are
coming in ( if I knew that I would just set the threashhold to that
amount ). My first best guess is not working out so well. I am using a data
received function like so.

private void port_DataReceived()
{
byte[] data = port.Input;
this.RxMESSAGE += Encoding.ASCII.GetString(data,0,data.Length);
}

and I have another function that cycles a certain period of time searching
RxMESSAGE for data comming in as so.

private bool searchRxForMessage(string message, int secondsToSearch)
{
DateTime finish = new
DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,DateTime.Now.
Hour,DateTime.Now.Minute,DateTime.Now.Second + secondsToSearch);
DateTime current = DateTime.Now;
while( (((TimeSpan)(finish - current)).TotalSeconds >= 0) )
{
if(this.RxMESSAGE.Length >= message.Length)
{
if(this.RxMESSAGE.IndexOf(message) > 0)
return true;
}
current = DateTime.Now;
}
return false;
}

I have port.RThreashhold = 1 so every byte that comes in is added to
RxMESSAGE.
to me it seems like it is not behaving correctly because I am in a while
loop on the same form that is running the port_DataReceived delagate?
 
You could have continued this on the thread you started 5 days ago.

My recommendation is to merge the two functions you have. So, when you
receive input, append it to your buffer and check right there and then if it
meets your criteria (e.g. matching a string). If it does, raise an event to
the rest of your code that the string has arrived; if it doesn't, continue
aggregating bytes.

If you insist with the current approach, you will need to protect the
RxMESSAGE from being accessed by multiple threads (look up Threading.Monitor
or the lock keyword).

Cheers
Daniel
 
thanks dan,

it has been my experience that people respond faster to new threads :-).
your suggestion is to check the length of the message inthe
port_DataReceived event, if it is long enough then check if the string
matches.. if so call a delagate to process the job from there?

is there any adverse effects if this process takes takes a few seconds?
meaning does the port_DataReceived event try to fire for byte 2 when I am
process the first byte?

regards,
Jay


Daniel Moth said:
You could have continued this on the thread you started 5 days ago.

My recommendation is to merge the two functions you have. So, when you
receive input, append it to your buffer and check right there and then if it
meets your criteria (e.g. matching a string). If it does, raise an event to
the rest of your code that the string has arrived; if it doesn't, continue
aggregating bytes.

If you insist with the current approach, you will need to protect the
RxMESSAGE from being accessed by multiple threads (look up Threading.Monitor
or the lock keyword).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


jayderk said:
I am writing an application that communicates over COM0: with the openCFNet
Serial.IO class. my problem is I do not know how best to go about
collecting
the data that comes in from the port not knowing exactly how many bytes
are
coming in ( if I knew that I would just set the threashhold to that
amount ). My first best guess is not working out so well. I am using a
data
received function like so.

private void port_DataReceived()
{
byte[] data = port.Input;
this.RxMESSAGE += Encoding.ASCII.GetString(data,0,data.Length);
}

and I have another function that cycles a certain period of time searching
RxMESSAGE for data comming in as so.

private bool searchRxForMessage(string message, int secondsToSearch)
{
DateTime finish = new
DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,DateTime.Now.
Hour,DateTime.Now.Minute,DateTime.Now.Second + secondsToSearch);
DateTime current = DateTime.Now;
while( (((TimeSpan)(finish - current)).TotalSeconds >= 0) )
{
if(this.RxMESSAGE.Length >= message.Length)
{
if(this.RxMESSAGE.IndexOf(message) > 0)
return true;
}
current = DateTime.Now;
}
return false;
}

I have port.RThreashhold = 1 so every byte that comes in is added to
RxMESSAGE.
to me it seems like it is not behaving correctly because I am in a while
loop on the same form that is running the port_DataReceived delagate?
 
Inline

jayderk said:
thanks dan,

it has been my experience that people respond faster to new threads :-).
Some people don't respond at all to unnecessary new threads :-)
your suggestion is to check the length of the message inthe
port_DataReceived event, if it is long enough then check if the string
matches.. if so call a delagate to process the job from there?
Yes, do your checking/parsing in the event handler of the DataReceived and
when you've got a match raise your own event.
is there any adverse effects if this process takes takes a few seconds?
meaning does the port_DataReceived event try to fire for byte 2 when I am
process the first byte?
Looking at the current implementation of the opennetcf serial port, it uses
only 1 thread for notifying you of data received. So the scenario you fear
should not be possible (while you are processing the data it gave you, it
cannot call you back with more). Also note that just because you set the
threshold to 1 byte doesn't mean it will pass you back only 1 byte each time
(it can vary from 1 to many - this is a good thing).

Having said that, I wouldn't design for the event handler to be blocking for
*seconds*. I don't see anything in your code that will take seconds to
complete. If you have more info on that please share.
regards,
Jay


Daniel Moth said:
You could have continued this on the thread you started 5 days ago.

My recommendation is to merge the two functions you have. So, when you
receive input, append it to your buffer and check right there and then if it
meets your criteria (e.g. matching a string). If it does, raise an event to
the rest of your code that the string has arrived; if it doesn't,
continue
aggregating bytes.

If you insist with the current approach, you will need to protect the
RxMESSAGE from being accessed by multiple threads (look up Threading.Monitor
or the lock keyword).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


jayderk said:
I am writing an application that communicates over COM0: with the openCFNet
Serial.IO class. my problem is I do not know how best to go about
collecting
the data that comes in from the port not knowing exactly how many bytes
are
coming in ( if I knew that I would just set the threashhold to that
amount ). My first best guess is not working out so well. I am using a
data
received function like so.

private void port_DataReceived()
{
byte[] data = port.Input;
this.RxMESSAGE += Encoding.ASCII.GetString(data,0,data.Length);
}

and I have another function that cycles a certain period of time searching
RxMESSAGE for data comming in as so.

private bool searchRxForMessage(string message, int secondsToSearch)
{
DateTime finish = new
DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,DateTime.Now.
Hour,DateTime.Now.Minute,DateTime.Now.Second + secondsToSearch);
DateTime current = DateTime.Now;
while( (((TimeSpan)(finish - current)).TotalSeconds >= 0) )
{
if(this.RxMESSAGE.Length >= message.Length)
{
if(this.RxMESSAGE.IndexOf(message) > 0)
return true;
}
current = DateTime.Now;
}
return false;
}

I have port.RThreashhold = 1 so every byte that comes in is added to
RxMESSAGE.
to me it seems like it is not behaving correctly because I am in a
while
loop on the same form that is running the port_DataReceived delagate?
 
it is kind of like a command line messanger.
I send output to the port "Send message To"
it replys with "To?"
When I see "To?" I want to enter in the receiver id.(this may take a second)
only after I see the "To?"... receiver id = "Joe Smith"
it will then reply with "Joe Smith" as a valid receiver..
I send out "Enter Message"
it replys with ">"...
I then enter the message "Hello World"
then "Send Mesage"




Daniel Moth said:
Inline

jayderk said:
thanks dan,

it has been my experience that people respond faster to new threads :-).
Some people don't respond at all to unnecessary new threads :-)
your suggestion is to check the length of the message inthe
port_DataReceived event, if it is long enough then check if the string
matches.. if so call a delagate to process the job from there?
Yes, do your checking/parsing in the event handler of the DataReceived and
when you've got a match raise your own event.
is there any adverse effects if this process takes takes a few seconds?
meaning does the port_DataReceived event try to fire for byte 2 when I am
process the first byte?
Looking at the current implementation of the opennetcf serial port, it uses
only 1 thread for notifying you of data received. So the scenario you fear
should not be possible (while you are processing the data it gave you, it
cannot call you back with more). Also note that just because you set the
threshold to 1 byte doesn't mean it will pass you back only 1 byte each time
(it can vary from 1 to many - this is a good thing).

Having said that, I wouldn't design for the event handler to be blocking for
*seconds*. I don't see anything in your code that will take seconds to
complete. If you have more info on that please share.
regards,
Jay


Daniel Moth said:
You could have continued this on the thread you started 5 days ago.

My recommendation is to merge the two functions you have. So, when you
receive input, append it to your buffer and check right there and then
if
it
meets your criteria (e.g. matching a string). If it does, raise an
event
to
the rest of your code that the string has arrived; if it doesn't,
continue
aggregating bytes.

If you insist with the current approach, you will need to protect the
RxMESSAGE from being accessed by multiple threads (look up Threading.Monitor
or the lock keyword).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


I am writing an application that communicates over COM0: with the openCFNet
Serial.IO class. my problem is I do not know how best to go about
collecting
the data that comes in from the port not knowing exactly how many bytes
are
coming in ( if I knew that I would just set the threashhold to that
amount ). My first best guess is not working out so well. I am using a
data
received function like so.

private void port_DataReceived()
{
byte[] data = port.Input;
this.RxMESSAGE += Encoding.ASCII.GetString(data,0,data.Length);
}

and I have another function that cycles a certain period of time searching
RxMESSAGE for data comming in as so.

private bool searchRxForMessage(string message, int secondsToSearch)
{
DateTime finish = new
DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,DateTime.Now.
Hour,DateTime.Now.Minute,DateTime.Now.Second + secondsToSearch);
DateTime current = DateTime.Now;
while( (((TimeSpan)(finish - current)).TotalSeconds >= 0) )
{
if(this.RxMESSAGE.Length >= message.Length)
{
if(this.RxMESSAGE.IndexOf(message) > 0)
return true;
}
current = DateTime.Now;
}
return false;
}

I have port.RThreashhold = 1 so every byte that comes in is added to
RxMESSAGE.
to me it seems like it is not behaving correctly because I am in a
while
loop on the same form that is running the port_DataReceived delagate?
 
When I see "To?" I want to enter in the receiver id.(this may take a
Do you mean enter the id programmatically via code (e.g. retrieving it from
some file) or as a user? If you mean as a user (e.g. in a textbox or
console), then by that time the function has returned so there is no delay.
If you mean via code then try it and see. If it performs badly you can
optimise it.

If you do decide to optimise it, at the step where you raise your own event
(or after receiving the event when you are about to perform the lengthy
operation), you could do that asynchronously (e.g. using the ThreadPool or
another Thread).
Let us know if you have any problems with that when/if you try it.

Remember if you touch any GUI controls from the threadpool or from the event
handler of the DataReceived to use Control.Invoke

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


jayderk said:
it is kind of like a command line messanger.
I send output to the port "Send message To"
it replys with "To?"
When I see "To?" I want to enter in the receiver id.(this may take a
second)
only after I see the "To?"... receiver id = "Joe Smith"
it will then reply with "Joe Smith" as a valid receiver..
I send out "Enter Message"
it replys with ">"...
I then enter the message "Hello World"
then "Send Mesage"




Daniel Moth said:
Inline

jayderk said:
thanks dan,

it has been my experience that people respond faster to new threads
:-).
Some people don't respond at all to unnecessary new threads :-)
your suggestion is to check the length of the message inthe
port_DataReceived event, if it is long enough then check if the string
matches.. if so call a delagate to process the job from there?
Yes, do your checking/parsing in the event handler of the DataReceived
and
when you've got a match raise your own event.
is there any adverse effects if this process takes takes a few seconds?
meaning does the port_DataReceived event try to fire for byte 2 when I am
process the first byte?
Looking at the current implementation of the opennetcf serial port, it uses
only 1 thread for notifying you of data received. So the scenario you
fear
should not be possible (while you are processing the data it gave you, it
cannot call you back with more). Also note that just because you set the
threshold to 1 byte doesn't mean it will pass you back only 1 byte each time
(it can vary from 1 to many - this is a good thing).

Having said that, I wouldn't design for the event handler to be blocking for
*seconds*. I don't see anything in your code that will take seconds to
complete. If you have more info on that please share.
regards,
Jay


You could have continued this on the thread you started 5 days ago.

My recommendation is to merge the two functions you have. So, when you
receive input, append it to your buffer and check right there and then if
it
meets your criteria (e.g. matching a string). If it does, raise an event
to
the rest of your code that the string has arrived; if it doesn't,
continue
aggregating bytes.

If you insist with the current approach, you will need to protect the
RxMESSAGE from being accessed by multiple threads (look up
Threading.Monitor
or the lock keyword).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


I am writing an application that communicates over COM0: with the
openCFNet
Serial.IO class. my problem is I do not know how best to go about
collecting
the data that comes in from the port not knowing exactly how many bytes
are
coming in ( if I knew that I would just set the threashhold to that
amount ). My first best guess is not working out so well. I am using a
data
received function like so.

private void port_DataReceived()
{
byte[] data = port.Input;
this.RxMESSAGE += Encoding.ASCII.GetString(data,0,data.Length);
}

and I have another function that cycles a certain period of time
searching
RxMESSAGE for data comming in as so.

private bool searchRxForMessage(string message, int secondsToSearch)
{
DateTime finish = new

DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,DateTime.Now.
Hour,DateTime.Now.Minute,DateTime.Now.Second + secondsToSearch);
DateTime current = DateTime.Now;
while( (((TimeSpan)(finish - current)).TotalSeconds >= 0) )
{
if(this.RxMESSAGE.Length >= message.Length)
{
if(this.RxMESSAGE.IndexOf(message) > 0)
return true;
}
current = DateTime.Now;
}
return false;
}

I have port.RThreashhold = 1 so every byte that comes in is added to
RxMESSAGE.
to me it seems like it is not behaving correctly because I am in a
while
loop on the same form that is running the port_DataReceived
delagate?
 
here is my code to send ... it is basically commandline...




private void button1_Click(object sender, System.EventArgs e)
{
this.sendMessageToPort("set message to");
}




private void sendMessageToPort(string myString)
{
if(myString.Length <= 0)
return;

myString += "\r";
byte[] outputData = new byte[myString.Length];

for(int i = 0 ; i < myString.Length; i++)
{
outputData = Convert.ToByte(myString);
}

port.Output = outputData;

}



then according to what you said before I will just wait for the
port_DataReceived event to occur.
private void port_DataReceived()
{
byte[] data = port.Input;
this.RxMESSAGE += Encoding.ASCII.GetString(data,0,data.Length);

if(this.RxMESSAGE.Length >= searchMessage.Length)
{
if(this.RxMESSAGE.IndexOf(searchMessage) > 0)
//start event operation and take the next step after I see that the
"set message to" command returned "To?".
}
}



the code that I communicate with I do not have access to so I can not do
anything about the response time.


regards,
Jay


Daniel Moth said:
When I see "To?" I want to enter in the receiver id.(this may take a
second)
Do you mean enter the id programmatically via code (e.g. retrieving it from
some file) or as a user? If you mean as a user (e.g. in a textbox or
console), then by that time the function has returned so there is no delay.
If you mean via code then try it and see. If it performs badly you can
optimise it.

If you do decide to optimise it, at the step where you raise your own event
(or after receiving the event when you are about to perform the lengthy
operation), you could do that asynchronously (e.g. using the ThreadPool or
another Thread).
Let us know if you have any problems with that when/if you try it.

Remember if you touch any GUI controls from the threadpool or from the event
handler of the DataReceived to use Control.Invoke

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


jayderk said:
it is kind of like a command line messanger.
I send output to the port "Send message To"
it replys with "To?"
When I see "To?" I want to enter in the receiver id.(this may take a
second)
only after I see the "To?"... receiver id = "Joe Smith"
it will then reply with "Joe Smith" as a valid receiver..
I send out "Enter Message"
it replys with ">"...
I then enter the message "Hello World"
then "Send Mesage"




Daniel Moth said:
Inline

thanks dan,

it has been my experience that people respond faster to new threads
:-).
Some people don't respond at all to unnecessary new threads :-)

your suggestion is to check the length of the message inthe
port_DataReceived event, if it is long enough then check if the string
matches.. if so call a delagate to process the job from there?
Yes, do your checking/parsing in the event handler of the DataReceived
and
when you've got a match raise your own event.

is there any adverse effects if this process takes takes a few seconds?
meaning does the port_DataReceived event try to fire for byte 2 when
I
am
process the first byte?
Looking at the current implementation of the opennetcf serial port, it uses
only 1 thread for notifying you of data received. So the scenario you
fear
should not be possible (while you are processing the data it gave you, it
cannot call you back with more). Also note that just because you set the
threshold to 1 byte doesn't mean it will pass you back only 1 byte each time
(it can vary from 1 to many - this is a good thing).

Having said that, I wouldn't design for the event handler to be
blocking
for
*seconds*. I don't see anything in your code that will take seconds to
complete. If you have more info on that please share.


regards,
Jay


You could have continued this on the thread you started 5 days ago.

My recommendation is to merge the two functions you have. So, when you
receive input, append it to your buffer and check right there and
then
if
it
meets your criteria (e.g. matching a string). If it does, raise an event
to
the rest of your code that the string has arrived; if it doesn't,
continue
aggregating bytes.

If you insist with the current approach, you will need to protect the
RxMESSAGE from being accessed by multiple threads (look up
Threading.Monitor
or the lock keyword).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


I am writing an application that communicates over COM0: with the
openCFNet
Serial.IO class. my problem is I do not know how best to go about
collecting
the data that comes in from the port not knowing exactly how many bytes
are
coming in ( if I knew that I would just set the threashhold to that
amount ). My first best guess is not working out so well. I am
using
a
data
received function like so.

private void port_DataReceived()
{
byte[] data = port.Input;
this.RxMESSAGE += Encoding.ASCII.GetString(data,0,data.Length);
}

and I have another function that cycles a certain period of time
searching
RxMESSAGE for data comming in as so.

private bool searchRxForMessage(string message, int secondsToSearch)
{
DateTime finish = new
DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,DateTime.Now.
Hour,DateTime.Now.Minute,DateTime.Now.Second + secondsToSearch);
DateTime current = DateTime.Now;
while( (((TimeSpan)(finish - current)).TotalSeconds >= 0) )
{
if(this.RxMESSAGE.Length >= message.Length)
{
if(this.RxMESSAGE.IndexOf(message) > 0)
return true;
}
current = DateTime.Now;
}
return false;
}

I have port.RThreashhold = 1 so every byte that comes in is added to
RxMESSAGE.
to me it seems like it is not behaving correctly because I am in a
while
loop on the same form that is running the port_DataReceived
delagate?
 
This is a bad construct. Calling stuff in the event handler will block the
receive thread. You should do nothing but put it into an app buffer and
have some other thread looking at the buffer and handling logic.

--
<ctacke/>
www.OpenNETCF.org
Your CF searches start and end here


jayderk said:
here is my code to send ... it is basically commandline...




private void button1_Click(object sender, System.EventArgs e)
{
this.sendMessageToPort("set message to");
}




private void sendMessageToPort(string myString)
{
if(myString.Length <= 0)
return;

myString += "\r";
byte[] outputData = new byte[myString.Length];

for(int i = 0 ; i < myString.Length; i++)
{
outputData = Convert.ToByte(myString);
}

port.Output = outputData;

}



then according to what you said before I will just wait for the
port_DataReceived event to occur.
private void port_DataReceived()
{
byte[] data = port.Input;
this.RxMESSAGE += Encoding.ASCII.GetString(data,0,data.Length);

if(this.RxMESSAGE.Length >= searchMessage.Length)
{
if(this.RxMESSAGE.IndexOf(searchMessage) > 0)
//start event operation and take the next step after I see that the
"set message to" command returned "To?".
}
}



the code that I communicate with I do not have access to so I can not do
anything about the response time.


regards,
Jay


Daniel Moth said:
When I see "To?" I want to enter in the receiver id.(this may take a
second)
Do you mean enter the id programmatically via code (e.g. retrieving it from
some file) or as a user? If you mean as a user (e.g. in a textbox or
console), then by that time the function has returned so there is no delay.
If you mean via code then try it and see. If it performs badly you can
optimise it.

If you do decide to optimise it, at the step where you raise your own event
(or after receiving the event when you are about to perform the lengthy
operation), you could do that asynchronously (e.g. using the ThreadPool
or
another Thread).
Let us know if you have any problems with that when/if you try it.

Remember if you touch any GUI controls from the threadpool or from the event
handler of the DataReceived to use Control.Invoke

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


jayderk said:
it is kind of like a command line messanger.
I send output to the port "Send message To"
it replys with "To?"
When I see "To?" I want to enter in the receiver id.(this may take a
second)
only after I see the "To?"... receiver id = "Joe Smith"
it will then reply with "Joe Smith" as a valid receiver..
I send out "Enter Message"
it replys with ">"...
I then enter the message "Hello World"
then "Send Mesage"




Inline

thanks dan,

it has been my experience that people respond faster to new threads
:-).
Some people don't respond at all to unnecessary new threads :-)

your suggestion is to check the length of the message inthe
port_DataReceived event, if it is long enough then check if the string
matches.. if so call a delagate to process the job from there?
Yes, do your checking/parsing in the event handler of the DataReceived
and
when you've got a match raise your own event.

is there any adverse effects if this process takes takes a few seconds?
meaning does the port_DataReceived event try to fire for byte 2 when I
am
process the first byte?
Looking at the current implementation of the opennetcf serial port, it
uses
only 1 thread for notifying you of data received. So the scenario you
fear
should not be possible (while you are processing the data it gave you, it
cannot call you back with more). Also note that just because you set the
threshold to 1 byte doesn't mean it will pass you back only 1 byte
each
time
(it can vary from 1 to many - this is a good thing).

Having said that, I wouldn't design for the event handler to be blocking
for
*seconds*. I don't see anything in your code that will take seconds to
complete. If you have more info on that please share.


regards,
Jay


You could have continued this on the thread you started 5 days ago.

My recommendation is to merge the two functions you have. So, when you
receive input, append it to your buffer and check right there and then
if
it
meets your criteria (e.g. matching a string). If it does, raise an
event
to
the rest of your code that the string has arrived; if it doesn't,
continue
aggregating bytes.

If you insist with the current approach, you will need to protect the
RxMESSAGE from being accessed by multiple threads (look up
Threading.Monitor
or the lock keyword).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


I am writing an application that communicates over COM0: with the
openCFNet
Serial.IO class. my problem is I do not know how best to go about
collecting
the data that comes in from the port not knowing exactly how many
bytes
are
coming in ( if I knew that I would just set the threashhold to that
amount ). My first best guess is not working out so well. I am using
a
data
received function like so.

private void port_DataReceived()
{
byte[] data = port.Input;
this.RxMESSAGE += Encoding.ASCII.GetString(data,0,data.Length);
}

and I have another function that cycles a certain period of time
searching
RxMESSAGE for data comming in as so.

private bool searchRxForMessage(string message, int secondsToSearch)
{
DateTime finish = new


DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,DateTime.Now.
Hour,DateTime.Now.Minute,DateTime.Now.Second + secondsToSearch);
DateTime current = DateTime.Now;
while( (((TimeSpan)(finish - current)).TotalSeconds >= 0) )
{
if(this.RxMESSAGE.Length >= message.Length)
{
if(this.RxMESSAGE.IndexOf(message) > 0)
return true;
}
current = DateTime.Now;
}
return false;
}

I have port.RThreashhold = 1 so every byte that comes in is added to
RxMESSAGE.
to me it seems like it is not behaving correctly because I am in
a
while
loop on the same form that is running the port_DataReceived
delagate?
 
Chris, I am not sure I'd make a blanket statement like that. Although, as I
said in an earlier post to this thread, I wouldn't design an event handler
to be blocking for a second, if it is just checking a string then I see no
problem with it. Given the extra details the OP gave us, it sounds like
there is not much activity on the port anyway so the rx thread can happily
block for a few millis.

Again, if it turned out to be a bottleneck later:
at the step where you raise your own
Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Chris Tacke said:
This is a bad construct. Calling stuff in the event handler will block
the receive thread. You should do nothing but put it into an app buffer
and have some other thread looking at the buffer and handling logic.

--
<ctacke/>
www.OpenNETCF.org
Your CF searches start and end here


jayderk said:
here is my code to send ... it is basically commandline...




private void button1_Click(object sender, System.EventArgs e)
{
this.sendMessageToPort("set message to");
}




private void sendMessageToPort(string myString)
{
if(myString.Length <= 0)
return;

myString += "\r";
byte[] outputData = new byte[myString.Length];

for(int i = 0 ; i < myString.Length; i++)
{
outputData = Convert.ToByte(myString);
}

port.Output = outputData;

}



then according to what you said before I will just wait for the
port_DataReceived event to occur.
private void port_DataReceived()
{
byte[] data = port.Input;
this.RxMESSAGE += Encoding.ASCII.GetString(data,0,data.Length);

if(this.RxMESSAGE.Length >= searchMessage.Length)
{
if(this.RxMESSAGE.IndexOf(searchMessage) > 0)
//start event operation and take the next step after I see that the
"set message to" command returned "To?".
}
}



the code that I communicate with I do not have access to so I can not do
anything about the response time.


regards,
Jay


Daniel Moth said:
When I see "To?" I want to enter in the receiver id.(this may take a
second)
Do you mean enter the id programmatically via code (e.g. retrieving it from
some file) or as a user? If you mean as a user (e.g. in a textbox or
console), then by that time the function has returned so there is no delay.
If you mean via code then try it and see. If it performs badly you can
optimise it.

If you do decide to optimise it, at the step where you raise your own event
(or after receiving the event when you are about to perform the lengthy
operation), you could do that asynchronously (e.g. using the ThreadPool
or
another Thread).
Let us know if you have any problems with that when/if you try it.

Remember if you touch any GUI controls from the threadpool or from the event
handler of the DataReceived to use Control.Invoke

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


it is kind of like a command line messanger.
I send output to the port "Send message To"
it replys with "To?"
When I see "To?" I want to enter in the receiver id.(this may take a
second)
only after I see the "To?"... receiver id = "Joe Smith"
it will then reply with "Joe Smith" as a valid receiver..
I send out "Enter Message"
it replys with ">"...
I then enter the message "Hello World"
then "Send Mesage"




Inline

thanks dan,

it has been my experience that people respond faster to new threads
:-).
Some people don't respond at all to unnecessary new threads :-)

your suggestion is to check the length of the message inthe
port_DataReceived event, if it is long enough then check if the string
matches.. if so call a delagate to process the job from there?
Yes, do your checking/parsing in the event handler of the
DataReceived
and
when you've got a match raise your own event.

is there any adverse effects if this process takes takes a few seconds?
meaning does the port_DataReceived event try to fire for byte 2
when I
am
process the first byte?
Looking at the current implementation of the opennetcf serial port,
it
uses
only 1 thread for notifying you of data received. So the scenario you
fear
should not be possible (while you are processing the data it gave
you, it
cannot call you back with more). Also note that just because you set the
threshold to 1 byte doesn't mean it will pass you back only 1 byte
each
time
(it can vary from 1 to many - this is a good thing).

Having said that, I wouldn't design for the event handler to be blocking
for
*seconds*. I don't see anything in your code that will take seconds
to
complete. If you have more info on that please share.


regards,
Jay


You could have continued this on the thread you started 5 days
ago.

My recommendation is to merge the two functions you have. So, when you
receive input, append it to your buffer and check right there and then
if
it
meets your criteria (e.g. matching a string). If it does, raise an
event
to
the rest of your code that the string has arrived; if it doesn't,
continue
aggregating bytes.

If you insist with the current approach, you will need to protect the
RxMESSAGE from being accessed by multiple threads (look up
Threading.Monitor
or the lock keyword).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


I am writing an application that communicates over COM0: with the
openCFNet
Serial.IO class. my problem is I do not know how best to go
about
collecting
the data that comes in from the port not knowing exactly how
many
bytes
are
coming in ( if I knew that I would just set the threashhold to that
amount ). My first best guess is not working out so well. I am using
a
data
received function like so.

private void port_DataReceived()
{
byte[] data = port.Input;
this.RxMESSAGE += Encoding.ASCII.GetString(data,0,data.Length);
}

and I have another function that cycles a certain period of time
searching
RxMESSAGE for data comming in as so.

private bool searchRxForMessage(string message, int secondsToSearch)
{
DateTime finish = new


DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,DateTime.Now.
Hour,DateTime.Now.Minute,DateTime.Now.Second + secondsToSearch);
DateTime current = DateTime.Now;
while( (((TimeSpan)(finish - current)).TotalSeconds >= 0) )
{
if(this.RxMESSAGE.Length >= message.Length)
{
if(this.RxMESSAGE.IndexOf(message) > 0)
return true;
}
current = DateTime.Now;
}
return false;
}

I have port.RThreashhold = 1 so every byte that comes in is
added to
RxMESSAGE.
to me it seems like it is not behaving correctly because I am in
a
while
loop on the same form that is running the port_DataReceived
delagate?

 
I stand by my assertion. His code has a comment in the event handler:

//start event operation and take the next step after I see that the
"set message to" command returned "To?".

This indicates to me he's calling another method. This continues to block
the receiver thread until it's done. It may be calling other methods as
well. It's like putting logic in an interrupt handler. It's a bad idea and
I've never seen a case that called for it. Sure, maybe it will work in this
instance. Then he'll go to enhance the app, or worse someone else will
inherit it and have to extend it, and it will break because of a poor design
decision early on.

Best to do it right in the first place, even if it takes more time and code
to do.

--
<ctacke/>
www.OpenNETCF.org
Your CF searches start and end here


Daniel Moth said:
Chris, I am not sure I'd make a blanket statement like that. Although, as
I said in an earlier post to this thread, I wouldn't design an event
handler to be blocking for a second, if it is just checking a string then
I see no problem with it. Given the extra details the OP gave us, it
sounds like there is not much activity on the port anyway so the rx thread
can happily block for a few millis.

Again, if it turned out to be a bottleneck later:
at the step where you raise your own
Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Chris Tacke said:
This is a bad construct. Calling stuff in the event handler will block
the receive thread. You should do nothing but put it into an app buffer
and have some other thread looking at the buffer and handling logic.

--
<ctacke/>
www.OpenNETCF.org
Your CF searches start and end here


jayderk said:
here is my code to send ... it is basically commandline...




private void button1_Click(object sender, System.EventArgs e)
{
this.sendMessageToPort("set message to");
}




private void sendMessageToPort(string myString)
{
if(myString.Length <= 0)
return;

myString += "\r";
byte[] outputData = new byte[myString.Length];

for(int i = 0 ; i < myString.Length; i++)
{
outputData = Convert.ToByte(myString);
}

port.Output = outputData;

}



then according to what you said before I will just wait for the
port_DataReceived event to occur.
private void port_DataReceived()
{
byte[] data = port.Input;
this.RxMESSAGE += Encoding.ASCII.GetString(data,0,data.Length);

if(this.RxMESSAGE.Length >= searchMessage.Length)
{
if(this.RxMESSAGE.IndexOf(searchMessage) > 0)
//start event operation and take the next step after I see that
the
"set message to" command returned "To?".
}
}



the code that I communicate with I do not have access to so I can not do
anything about the response time.


regards,
Jay


When I see "To?" I want to enter in the receiver id.(this may take a
second)
Do you mean enter the id programmatically via code (e.g. retrieving it
from
some file) or as a user? If you mean as a user (e.g. in a textbox or
console), then by that time the function has returned so there is no
delay.
If you mean via code then try it and see. If it performs badly you can
optimise it.

If you do decide to optimise it, at the step where you raise your own
event
(or after receiving the event when you are about to perform the lengthy
operation), you could do that asynchronously (e.g. using the ThreadPool
or
another Thread).
Let us know if you have any problems with that when/if you try it.

Remember if you touch any GUI controls from the threadpool or from the
event
handler of the DataReceived to use Control.Invoke

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


it is kind of like a command line messanger.
I send output to the port "Send message To"
it replys with "To?"
When I see "To?" I want to enter in the receiver id.(this may take a
second)
only after I see the "To?"... receiver id = "Joe Smith"
it will then reply with "Joe Smith" as a valid receiver..
I send out "Enter Message"
it replys with ">"...
I then enter the message "Hello World"
then "Send Mesage"




Inline

thanks dan,

it has been my experience that people respond faster to new
threads
:-).
Some people don't respond at all to unnecessary new threads :-)

your suggestion is to check the length of the message inthe
port_DataReceived event, if it is long enough then check if the
string
matches.. if so call a delagate to process the job from there?
Yes, do your checking/parsing in the event handler of the
DataReceived
and
when you've got a match raise your own event.

is there any adverse effects if this process takes takes a few
seconds?
meaning does the port_DataReceived event try to fire for byte 2
when
I
am
process the first byte?
Looking at the current implementation of the opennetcf serial port,
it
uses
only 1 thread for notifying you of data received. So the scenario
you
fear
should not be possible (while you are processing the data it gave
you,
it
cannot call you back with more). Also note that just because you set
the
threshold to 1 byte doesn't mean it will pass you back only 1 byte
each
time
(it can vary from 1 to many - this is a good thing).

Having said that, I wouldn't design for the event handler to be
blocking
for
*seconds*. I don't see anything in your code that will take seconds
to
complete. If you have more info on that please share.


regards,
Jay


You could have continued this on the thread you started 5 days
ago.

My recommendation is to merge the two functions you have. So,
when
you
receive input, append it to your buffer and check right there and
then
if
it
meets your criteria (e.g. matching a string). If it does, raise
an
event
to
the rest of your code that the string has arrived; if it doesn't,
continue
aggregating bytes.

If you insist with the current approach, you will need to protect
the
RxMESSAGE from being accessed by multiple threads (look up
Threading.Monitor
or the lock keyword).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


I am writing an application that communicates over COM0: with
the
openCFNet
Serial.IO class. my problem is I do not know how best to go
about
collecting
the data that comes in from the port not knowing exactly how
many
bytes
are
coming in ( if I knew that I would just set the threashhold to
that
amount ). My first best guess is not working out so well. I am
using
a
data
received function like so.

private void port_DataReceived()
{
byte[] data = port.Input;
this.RxMESSAGE += Encoding.ASCII.GetString(data,0,data.Length);
}

and I have another function that cycles a certain period of
time
searching
RxMESSAGE for data comming in as so.

private bool searchRxForMessage(string message, int
secondsToSearch)
{
DateTime finish = new



DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,DateTime.Now.
Hour,DateTime.Now.Minute,DateTime.Now.Second +
secondsToSearch);
DateTime current = DateTime.Now;
while( (((TimeSpan)(finish - current)).TotalSeconds >= 0) )
{
if(this.RxMESSAGE.Length >= message.Length)
{
if(this.RxMESSAGE.IndexOf(message) > 0)
return true;
}
current = DateTime.Now;
}
return false;
}

I have port.RThreashhold = 1 so every byte that comes in is
added
to
RxMESSAGE.
to me it seems like it is not behaving correctly because I am
in a
while
loop on the same form that is running the port_DataReceived
delagate?


 
Chris, I see where you are coming from. I still don't see it as clear cut as
you assert it to be: I would not make a blanket statement like that.

Not sure what his comment translates to when it comes to code (which is why
I asked for more info in that area) but if for example it just sticks some
text on the GUI, I'd go for it. His scenario sounds very similar to a half
duplex situation: Unless he sends something back he shall not receive;
nothing bad with blocking the rx thread in that scenario. Why introduce
complexity (and in fact worst performance) where it is not needed? In fact,
I can't see how "it will break because of a poor design decision early on.".
It is hardly an irrevocable design decision: 3 lines of extra code turn it
into not blocking the rx thread (example at end of this post). BTW, I don't
treat any managed code as an interrupt handler for reasons that we both know
:-) When faced with such a decision, I recommend the simplest solution while
documenting the design choice (in this case the OP has been informed that
the event handler should not block for a seconds and has had the
straightforward workaround explained to him). If you still don't see my
point, we'll have to agree to slightly disagree on this one...

Should jayderk or someone else require it in their design:

//Instead of raising the event after checking the string, stick the
following line of code
ThreadPool.QueueUserWorkItem(new WaitCallback(asyncMethod), someState);

//then declare a method:
public void asyncMethod(object state){
//start event operation and take the next step after I see that the
// "set message to" command returned "To?".
}

Also I don't see why the sendMessageToPort method doesn't convert the string
to a byte array using the Encoding class (much like the receiving code does
for bytes->string)

Hope this helps

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Chris Tacke said:
I stand by my assertion. His code has a comment in the event handler:

//start event operation and take the next step after I see that the
"set message to" command returned "To?".

This indicates to me he's calling another method. This continues to block
the receiver thread until it's done. It may be calling other methods as
well. It's like putting logic in an interrupt handler. It's a bad idea
and I've never seen a case that called for it. Sure, maybe it will work
in this instance. Then he'll go to enhance the app, or worse someone else
will inherit it and have to extend it, and it will break because of a poor
design decision early on.

Best to do it right in the first place, even if it takes more time and
code to do.

--
<ctacke/>
www.OpenNETCF.org
Your CF searches start and end here


Daniel Moth said:
Chris, I am not sure I'd make a blanket statement like that. Although, as
I said in an earlier post to this thread, I wouldn't design an event
handler to be blocking for a second, if it is just checking a string then
I see no problem with it. Given the extra details the OP gave us, it
sounds like there is not much activity on the port anyway so the rx
thread can happily block for a few millis.

Again, if it turned out to be a bottleneck later:
at the step where you raise your own
event
(or after receiving the event when you are about to perform the
lengthy
operation), you could do that asynchronously (e.g. using the
ThreadPool or
another Thread).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Chris Tacke said:
This is a bad construct. Calling stuff in the event handler will block
the receive thread. You should do nothing but put it into an app buffer
and have some other thread looking at the buffer and handling logic.

--
<ctacke/>
www.OpenNETCF.org
Your CF searches start and end here


here is my code to send ... it is basically commandline...




private void button1_Click(object sender, System.EventArgs e)
{
this.sendMessageToPort("set message to");
}




private void sendMessageToPort(string myString)
{
if(myString.Length <= 0)
return;

myString += "\r";
byte[] outputData = new byte[myString.Length];

for(int i = 0 ; i < myString.Length; i++)
{
outputData = Convert.ToByte(myString);
}

port.Output = outputData;

}



then according to what you said before I will just wait for the
port_DataReceived event to occur.
private void port_DataReceived()
{
byte[] data = port.Input;
this.RxMESSAGE += Encoding.ASCII.GetString(data,0,data.Length);

if(this.RxMESSAGE.Length >= searchMessage.Length)
{
if(this.RxMESSAGE.IndexOf(searchMessage) > 0)
//start event operation and take the next step after I see that
the
"set message to" command returned "To?".
}
}



the code that I communicate with I do not have access to so I can not
do
anything about the response time.


regards,
Jay


When I see "To?" I want to enter in the receiver id.(this may take a
second)
Do you mean enter the id programmatically via code (e.g. retrieving it
from
some file) or as a user? If you mean as a user (e.g. in a textbox or
console), then by that time the function has returned so there is no
delay.
If you mean via code then try it and see. If it performs badly you can
optimise it.

If you do decide to optimise it, at the step where you raise your own
event
(or after receiving the event when you are about to perform the
lengthy
operation), you could do that asynchronously (e.g. using the
ThreadPool or
another Thread).
Let us know if you have any problems with that when/if you try it.

Remember if you touch any GUI controls from the threadpool or from the
event
handler of the DataReceived to use Control.Invoke

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


it is kind of like a command line messanger.
I send output to the port "Send message To"
it replys with "To?"
When I see "To?" I want to enter in the receiver id.(this may take a
second)
only after I see the "To?"... receiver id = "Joe Smith"
it will then reply with "Joe Smith" as a valid receiver..
I send out "Enter Message"
it replys with ">"...
I then enter the message "Hello World"
then "Send Mesage"




Inline

thanks dan,

it has been my experience that people respond faster to new
threads
:-).
Some people don't respond at all to unnecessary new threads :-)

your suggestion is to check the length of the message inthe
port_DataReceived event, if it is long enough then check if the
string
matches.. if so call a delagate to process the job from there?
Yes, do your checking/parsing in the event handler of the
DataReceived
and
when you've got a match raise your own event.

is there any adverse effects if this process takes takes a few
seconds?
meaning does the port_DataReceived event try to fire for byte 2
when
I
am
process the first byte?
Looking at the current implementation of the opennetcf serial port,
it
uses
only 1 thread for notifying you of data received. So the scenario
you
fear
should not be possible (while you are processing the data it gave
you,
it
cannot call you back with more). Also note that just because you
set
the
threshold to 1 byte doesn't mean it will pass you back only 1 byte
each
time
(it can vary from 1 to many - this is a good thing).

Having said that, I wouldn't design for the event handler to be
blocking
for
*seconds*. I don't see anything in your code that will take seconds
to
complete. If you have more info on that please share.


regards,
Jay


You could have continued this on the thread you started 5 days
ago.

My recommendation is to merge the two functions you have. So,
when
you
receive input, append it to your buffer and check right there
and
then
if
it
meets your criteria (e.g. matching a string). If it does, raise
an
event
to
the rest of your code that the string has arrived; if it
doesn't,
continue
aggregating bytes.

If you insist with the current approach, you will need to
protect
the
RxMESSAGE from being accessed by multiple threads (look up
Threading.Monitor
or the lock keyword).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


I am writing an application that communicates over COM0: with
the
openCFNet
Serial.IO class. my problem is I do not know how best to go
about
collecting
the data that comes in from the port not knowing exactly how
many
bytes
are
coming in ( if I knew that I would just set the threashhold to
that
amount ). My first best guess is not working out so well. I am
using
a
data
received function like so.

private void port_DataReceived()
{
byte[] data = port.Input;
this.RxMESSAGE +=
Encoding.ASCII.GetString(data,0,data.Length);
}

and I have another function that cycles a certain period of
time
searching
RxMESSAGE for data comming in as so.

private bool searchRxForMessage(string message, int
secondsToSearch)
{
DateTime finish = new



DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,DateTime.Now.
Hour,DateTime.Now.Minute,DateTime.Now.Second +
secondsToSearch);
DateTime current = DateTime.Now;
while( (((TimeSpan)(finish - current)).TotalSeconds >= 0) )
{
if(this.RxMESSAGE.Length >= message.Length)
{
if(this.RxMESSAGE.IndexOf(message) > 0)
return true;
}
current = DateTime.Now;
}
return false;
}

I have port.RThreashhold = 1 so every byte that comes in is
added
to
RxMESSAGE.
to me it seems like it is not behaving correctly because I am
in a
while
loop on the same form that is running the port_DataReceived
delagate?


 
Hello,

thanks for all of your interest in this matter, I think the best solution is
to send data on a seperate thread and let the port_DataReceived
have its own thread.. that way it is not waiting for anything to clear the
input buffer. What do you think?

regards,
Jay



Daniel Moth said:
Chris, I see where you are coming from. I still don't see it as clear cut as
you assert it to be: I would not make a blanket statement like that.

Not sure what his comment translates to when it comes to code (which is why
I asked for more info in that area) but if for example it just sticks some
text on the GUI, I'd go for it. His scenario sounds very similar to a half
duplex situation: Unless he sends something back he shall not receive;
nothing bad with blocking the rx thread in that scenario. Why introduce
complexity (and in fact worst performance) where it is not needed? In fact,
I can't see how "it will break because of a poor design decision early on.".
It is hardly an irrevocable design decision: 3 lines of extra code turn it
into not blocking the rx thread (example at end of this post). BTW, I don't
treat any managed code as an interrupt handler for reasons that we both know
:-) When faced with such a decision, I recommend the simplest solution while
documenting the design choice (in this case the OP has been informed that
the event handler should not block for a seconds and has had the
straightforward workaround explained to him). If you still don't see my
point, we'll have to agree to slightly disagree on this one...

Should jayderk or someone else require it in their design:

//Instead of raising the event after checking the string, stick the
following line of code
ThreadPool.QueueUserWorkItem(new WaitCallback(asyncMethod), someState);

//then declare a method:
public void asyncMethod(object state){
//start event operation and take the next step after I see that the
// "set message to" command returned "To?".
}

Also I don't see why the sendMessageToPort method doesn't convert the string
to a byte array using the Encoding class (much like the receiving code does
for bytes->string)

Hope this helps

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Chris Tacke said:
I stand by my assertion. His code has a comment in the event handler:

//start event operation and take the next step after I see that the
"set message to" command returned "To?".

This indicates to me he's calling another method. This continues to block
the receiver thread until it's done. It may be calling other methods as
well. It's like putting logic in an interrupt handler. It's a bad idea
and I've never seen a case that called for it. Sure, maybe it will work
in this instance. Then he'll go to enhance the app, or worse someone else
will inherit it and have to extend it, and it will break because of a poor
design decision early on.

Best to do it right in the first place, even if it takes more time and
code to do.

--
<ctacke/>
www.OpenNETCF.org
Your CF searches start and end here


Daniel Moth said:
Chris, I am not sure I'd make a blanket statement like that. Although, as
I said in an earlier post to this thread, I wouldn't design an event
handler to be blocking for a second, if it is just checking a string then
I see no problem with it. Given the extra details the OP gave us, it
sounds like there is not much activity on the port anyway so the rx
thread can happily block for a few millis.

Again, if it turned out to be a bottleneck later:
at the step where you raise your own
event
(or after receiving the event when you are about to perform the
lengthy
operation), you could do that asynchronously (e.g. using the
ThreadPool or
another Thread).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


This is a bad construct. Calling stuff in the event handler will block
the receive thread. You should do nothing but put it into an app buffer
and have some other thread looking at the buffer and handling logic.

--
<ctacke/>
www.OpenNETCF.org
Your CF searches start and end here


here is my code to send ... it is basically commandline...




private void button1_Click(object sender, System.EventArgs e)
{
this.sendMessageToPort("set message to");
}




private void sendMessageToPort(string myString)
{
if(myString.Length <= 0)
return;

myString += "\r";
byte[] outputData = new byte[myString.Length];

for(int i = 0 ; i < myString.Length; i++)
{
outputData = Convert.ToByte(myString);
}

port.Output = outputData;

}



then according to what you said before I will just wait for the
port_DataReceived event to occur.
private void port_DataReceived()
{
byte[] data = port.Input;
this.RxMESSAGE += Encoding.ASCII.GetString(data,0,data.Length);

if(this.RxMESSAGE.Length >= searchMessage.Length)
{
if(this.RxMESSAGE.IndexOf(searchMessage) > 0)
//start event operation and take the next step after I see that
the
"set message to" command returned "To?".
}
}



the code that I communicate with I do not have access to so I can not
do
anything about the response time.


regards,
Jay


When I see "To?" I want to enter in the receiver id.(this may take a
second)
Do you mean enter the id programmatically via code (e.g. retrieving it
from
some file) or as a user? If you mean as a user (e.g. in a textbox or
console), then by that time the function has returned so there is no
delay.
If you mean via code then try it and see. If it performs badly you can
optimise it.

If you do decide to optimise it, at the step where you raise your own
event
(or after receiving the event when you are about to perform the
lengthy
operation), you could do that asynchronously (e.g. using the
ThreadPool or
another Thread).
Let us know if you have any problems with that when/if you try it.

Remember if you touch any GUI controls from the threadpool or from the
event
handler of the DataReceived to use Control.Invoke

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


it is kind of like a command line messanger.
I send output to the port "Send message To"
it replys with "To?"
When I see "To?" I want to enter in the receiver id.(this may take a
second)
only after I see the "To?"... receiver id = "Joe Smith"
it will then reply with "Joe Smith" as a valid receiver..
I send out "Enter Message"
it replys with ">"...
I then enter the message "Hello World"
then "Send Mesage"




Inline

thanks dan,

it has been my experience that people respond faster to new
threads
:-).
Some people don't respond at all to unnecessary new threads :-)

your suggestion is to check the length of the message inthe
port_DataReceived event, if it is long enough then check if the
string
matches.. if so call a delagate to process the job from there?
Yes, do your checking/parsing in the event handler of the
DataReceived
and
when you've got a match raise your own event.

is there any adverse effects if this process takes takes a few
seconds?
meaning does the port_DataReceived event try to fire for byte 2
when
I
am
process the first byte?
Looking at the current implementation of the opennetcf serial port,
it
uses
only 1 thread for notifying you of data received. So the scenario
you
fear
should not be possible (while you are processing the data it gave
you,
it
cannot call you back with more). Also note that just because you
set
the
threshold to 1 byte doesn't mean it will pass you back only 1 byte
each
time
(it can vary from 1 to many - this is a good thing).

Having said that, I wouldn't design for the event handler to be
blocking
for
*seconds*. I don't see anything in your code that will take seconds
to
complete. If you have more info on that please share.


regards,
Jay


You could have continued this on the thread you started 5 days
ago.

My recommendation is to merge the two functions you have. So,
when
you
receive input, append it to your buffer and check right there
and
then
if
it
meets your criteria (e.g. matching a string). If it does, raise
an
event
to
the rest of your code that the string has arrived; if it
doesn't,
continue
aggregating bytes.

If you insist with the current approach, you will need to
protect
the
RxMESSAGE from being accessed by multiple threads (look up
Threading.Monitor
or the lock keyword).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


I am writing an application that communicates over COM0: with
the
openCFNet
Serial.IO class. my problem is I do not know how best to go
about
collecting
the data that comes in from the port not knowing exactly how
many
bytes
are
coming in ( if I knew that I would just set the threashhold to
that
amount ). My first best guess is not working out so well. I am
using
a
data
received function like so.

private void port_DataReceived()
{
byte[] data = port.Input;
this.RxMESSAGE +=
Encoding.ASCII.GetString(data,0,data.Length);
}

and I have another function that cycles a certain period of
time
searching
RxMESSAGE for data comming in as so.

private bool searchRxForMessage(string message, int
secondsToSearch)
{
DateTime finish = new



DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,DateTime.Now.
Hour,DateTime.Now.Minute,DateTime.Now.Second +
secondsToSearch);
DateTime current = DateTime.Now;
while( (((TimeSpan)(finish - current)).TotalSeconds >= 0) )
{
if(this.RxMESSAGE.Length >= message.Length)
{
if(this.RxMESSAGE.IndexOf(message) > 0)
return true;
}
current = DateTime.Now;
}
return false;
}

I have port.RThreashhold = 1 so every byte that comes in is
added
to
RxMESSAGE.
to me it seems like it is not behaving correctly because I am
in a
while
loop on the same form that is running the port_DataReceived
delagate?


 
Back
Top