Updating UI/Dataset from serialport
-
Ok, zipped, same location[^] but .zip on the end, thanks for finding that out about the
.cs
extensions, never knew about that. Best way to think of delegates is to just treat them as variables, they're nothing more anyway. The checkbox is thrown, it helps a lot because the debugger will break when an exception is thrown rather than when it is not handled, e.g. if you've got a try / catch block normally it'd pass through and execute the catch, but with this it breaks on the exception inside the try allowing you to find the source of the problem.Great info about the exception I'll give it a crack now, and check out your helper class. Super! :-D
-
Great info about the exception I'll give it a crack now, and check out your helper class. Super! :-D
-
I have an error when I compile. This is the code I updated:
private void _sp_DataReceived(object sender, SerialDataReceivedEventArgs e) { // run this method when the event fires InvocationHelper.Invoke(this.ChangeValue); }
Argument '1': cannot convert from 'method group' to 'System.Delegate' P.S If you prefer to have a fiddle yourself I can send you the source. Thanks for being so helpful. -
I have an error when I compile. This is the code I updated:
private void _sp_DataReceived(object sender, SerialDataReceivedEventArgs e) { // run this method when the event fires InvocationHelper.Invoke(this.ChangeValue); }
Argument '1': cannot convert from 'method group' to 'System.Delegate' P.S If you prefer to have a fiddle yourself I can send you the source. Thanks for being so helpful.Oops, forgot the delegate bit :doh:
private delegate void ChangeValueDelegate();
private void _sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
// run this method when the event fires
InvocationHelper.Invoke(new ChangedValueDelegate(this.ChangeValue));
} -
Oops, forgot the delegate bit :doh:
private delegate void ChangeValueDelegate();
private void _sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
// run this method when the event fires
InvocationHelper.Invoke(new ChangedValueDelegate(this.ChangeValue));
}Dang, didn't work. Does the block:
// Invoke delegate normally singleCastDelegate.DynamicInvoke(arguments);
Mean that it's not cross threaded? Curiously though, after I use your class then click on the button to change the value (just to see if it still works) it doesn't update the value in the label anymore... P.S I had to put a check in your code as I was getting an errorif (arguments != null) <- here { if (requiredParameters != arguments.Length) .... }
-
Dang, didn't work. Does the block:
// Invoke delegate normally singleCastDelegate.DynamicInvoke(arguments);
Mean that it's not cross threaded? Curiously though, after I use your class then click on the button to change the value (just to see if it still works) it doesn't update the value in the label anymore... P.S I had to put a check in your code as I was getting an errorif (arguments != null) <- here { if (requiredParameters != arguments.Length) .... }
Yes it's not cross-threading, I had a proper look through your code and now understand exactly what you're trying to do. Did you change the code in the button's event handler? That can stay the same, it's only the SerialPort stuff because of the way that Windows uses a seperate thread to read data internally. Re: error, you can actually scrap the first "wrapper" method completely, it's not needed at all (in fact I don't know why it's there at all), if it wasn't there then you wouldn't need your check because of the
params
declaration the arguments array would be zero length as opposed to null :doh:. Try setting a breakpoint on the line including theChangeValue
inside theDataReceived
handler, if that's hit then all handlers are setup properly for the SerialPort, then try F11 (in standard edition anyway) to do Debug -> Step Into, the debugger should then jump to theChangeValue
routine. Just step through the code and examine the variables to see if anything untoward is happening. -
Dang, didn't work. Does the block:
// Invoke delegate normally singleCastDelegate.DynamicInvoke(arguments);
Mean that it's not cross threaded? Curiously though, after I use your class then click on the button to change the value (just to see if it still works) it doesn't update the value in the label anymore... P.S I had to put a check in your code as I was getting an errorif (arguments != null) <- here { if (requiredParameters != arguments.Length) .... }
Now I'm confused, I just created a simple program to duplicate what you're doing and got some really bizarre behaviour. If running without the VS debugger attached it works fine, with the debugger attached it won't update the textbox, just as your experiencing. :~ If you're interested my code is here[^], all there is is a small dataset (one table, one column), a form which has a combo box for selecting the serial port, a button to open / close the port and a textbox which is bound to the dataset. I didn't create a seperate class like you did but experienced the same problem. But it works fine without the debugger :suss:
-
Yes it's not cross-threading, I had a proper look through your code and now understand exactly what you're trying to do. Did you change the code in the button's event handler? That can stay the same, it's only the SerialPort stuff because of the way that Windows uses a seperate thread to read data internally. Re: error, you can actually scrap the first "wrapper" method completely, it's not needed at all (in fact I don't know why it's there at all), if it wasn't there then you wouldn't need your check because of the
params
declaration the arguments array would be zero length as opposed to null :doh:. Try setting a breakpoint on the line including theChangeValue
inside theDataReceived
handler, if that's hit then all handlers are setup properly for the SerialPort, then try F11 (in standard edition anyway) to do Debug -> Step Into, the debugger should then jump to theChangeValue
routine. Just step through the code and examine the variables to see if anything untoward is happening.- I didn't change the code in the buttons event handler. - I scrapped the wrapper method. - The handler is firing ok, and I've stepped through the
ChangeValue
method, it's doing what it should - that is, it's changing the value in the dataset. I know this because whether theDataReceived
event handler or thebutton
event callsChangeValue
the value changes and I can see it as I step though that it's been changed from the previous value. What's not updating is the Text property of the Label control that the Dataset is bound to. P.S If I trigger theChangeValue
method via the button first, the label updates fine. But if I triggerChangeValue
via your delegate method and then try the button the label doesn't update. -
- I didn't change the code in the buttons event handler. - I scrapped the wrapper method. - The handler is firing ok, and I've stepped through the
ChangeValue
method, it's doing what it should - that is, it's changing the value in the dataset. I know this because whether theDataReceived
event handler or thebutton
event callsChangeValue
the value changes and I can see it as I step though that it's been changed from the previous value. What's not updating is the Text property of the Label control that the Dataset is bound to. P.S If I trigger theChangeValue
method via the button first, the label updates fine. But if I triggerChangeValue
via your delegate method and then try the button the label doesn't update. -
Now I'm confused, I just created a simple program to duplicate what you're doing and got some really bizarre behaviour. If running without the VS debugger attached it works fine, with the debugger attached it won't update the textbox, just as your experiencing. :~ If you're interested my code is here[^], all there is is a small dataset (one table, one column), a form which has a combo box for selecting the serial port, a button to open / close the port and a textbox which is bound to the dataset. I didn't create a seperate class like you did but experienced the same problem. But it works fine without the debugger :suss:
Holy Bible Batman you're right!!!! Arrgggh! I didn't test your code but just tried it on my own and it works... thanks to you and your tenacious approach. And not one delegate or generic in sight! My workmate says that if your over our neck of the woods sometime (NZ) we owe you a beer. Thanks very much Ed, you've been a real help on a prob that had me completely stumped. :cool:
-
Holy Bible Batman you're right!!!! Arrgggh! I didn't test your code but just tried it on my own and it works... thanks to you and your tenacious approach. And not one delegate or generic in sight! My workmate says that if your over our neck of the woods sometime (NZ) we owe you a beer. Thanks very much Ed, you've been a real help on a prob that had me completely stumped. :cool:
Hmm, might take you up on that offer if I ever go down that way. Confused though, you're in the States (according to bio) and you work in New Zealand? If it is the States I should hopefully be going from the 19th of April to 7th May, but that'll be Houston (7 days), San Antonio (7 days) and New York (4) days. But most of the time will be spent shooting I think, World Championships being the reason for the trip. Glad to have been of help.
-
Hmm, might take you up on that offer if I ever go down that way. Confused though, you're in the States (according to bio) and you work in New Zealand? If it is the States I should hopefully be going from the 19th of April to 7th May, but that'll be Houston (7 days), San Antonio (7 days) and New York (4) days. But most of the time will be spent shooting I think, World Championships being the reason for the trip. Glad to have been of help.
Ooops well, I guess I didn't update the default profile. I'm actually from NZ and live in NZ! So, we will have to have that beer another time! Shooting eh??! World champs! I guess you are competing, well if you dead eye with the pistola as you are with code you're bound to do well! Thanks again for you help, I really appreciated the time you spent. Good luck!
-
Ooops well, I guess I didn't update the default profile. I'm actually from NZ and live in NZ! So, we will have to have that beer another time! Shooting eh??! World champs! I guess you are competing, well if you dead eye with the pistola as you are with code you're bound to do well! Thanks again for you help, I really appreciated the time you spent. Good luck!
TheTinSoldier wrote:
dead eye with the pistola
Nah, bigger & better :-> The world champs are for Sporting which is a form of Clay Pigeon Shooting == Shotgun Haven't shot a pistol in years, my other shooting involves Air-Rifling (High-powered[^], i.e. it requires a firearms certificate here in Britain) with a bit of rifling, .22, .243 & .303.
-
TheTinSoldier wrote:
dead eye with the pistola
Nah, bigger & better :-> The world champs are for Sporting which is a form of Clay Pigeon Shooting == Shotgun Haven't shot a pistol in years, my other shooting involves Air-Rifling (High-powered[^], i.e. it requires a firearms certificate here in Britain) with a bit of rifling, .22, .243 & .303.
Now that sounds like fun! I was talking to my brother over the weekend about shooting guns and I must admit that I reakon is would be a real blast - pun intended. I've shot a .22 and a slug gun when I was a kid but that's about it. I've still to check off the shotgun! It's got to be on the list of things to do at least once! Well anyway Ed, good luck and bring back some silverware!:cool:
-
Now that sounds like fun! I was talking to my brother over the weekend about shooting guns and I must admit that I reakon is would be a real blast - pun intended. I've shot a .22 and a slug gun when I was a kid but that's about it. I've still to check off the shotgun! It's got to be on the list of things to do at least once! Well anyway Ed, good luck and bring back some silverware!:cool:
TheTinSoldier wrote:
good luck and bring back some silverware!
Thanks, I've finally been given the go-ahead by the doctors so I'm definitely off to America now :cool:, just wish I'd changed my gun before I went (I've been trying out a friend's gun and been shooting better with it)