Passing different variable types
-
Hi all, I have the following code that I need to return dblRx our of DisplayText in order to use it in another function. Problem I'm having is that if I try to return a double from DisplayText (private double DisplayText), I get the EventHandler error from this.Invoke that I have the wrong data type. Any help would be appreciated.
private void DisplayText(object sender, EventArgs e)
{
RxString = RxString.Remove(0, 3); //This removes the first 4 characters
//Need to convert RxString to double for formatting
double dblRx = double.Parse (RxString);
//Converting back to a string will result in leading zeros removed
RxString = dblRx.ToString();
textBox1.Text = RxString;}// End of DisplayText private void serialPort1\_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { RxString = ""; try { RxString = serialPort1.ReadTo("\\r"); serialPort1.WriteLine("\*X01\\r"); } catch (Exception Exception) { return; } this.Invoke(new EventHandler(DisplayText)); }//End of serialPort1\_DataReceived
-
Hi all, I have the following code that I need to return dblRx our of DisplayText in order to use it in another function. Problem I'm having is that if I try to return a double from DisplayText (private double DisplayText), I get the EventHandler error from this.Invoke that I have the wrong data type. Any help would be appreciated.
private void DisplayText(object sender, EventArgs e)
{
RxString = RxString.Remove(0, 3); //This removes the first 4 characters
//Need to convert RxString to double for formatting
double dblRx = double.Parse (RxString);
//Converting back to a string will result in leading zeros removed
RxString = dblRx.ToString();
textBox1.Text = RxString;}// End of DisplayText private void serialPort1\_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { RxString = ""; try { RxString = serialPort1.ReadTo("\\r"); serialPort1.WriteLine("\*X01\\r"); } catch (Exception Exception) { return; } this.Invoke(new EventHandler(DisplayText)); }//End of serialPort1\_DataReceived
Not sure I understand your question very well, however here are some remarks: 1.
mprice214 wrote:
RxString.Remove(0, 3); //This removes the first 4 characters
Not true. At best it removes 3 characters; and if there are fewer, expect an exception. 2.
mprice214 wrote:
double.Parse (RxString);
that will fail if the data isn't really a string representation of a double. Don't do this on external data, it will bite you. Either add try-catch or better yet use TryParse(). 3. why does DisplayText() have parameters which make it look like a real event handler, you are not using those parameters, instead you are using some class member. It would seem logical to give it one parameter: the text it should display. 4.
mprice214 wrote:
serialPort1.WriteLine("*X01\r");
very strange. You want a carriage return plus whatever the system uses for a newline (maybe "\r\n"). Either you like what the system gives you, or you don't; I would never mix the two. 5.
mprice214 wrote:
catch (Exception Exception) { return; }
This is not acceptable; whatever goes wrong, you ignore it. So when it goes wrong, you will have an impossible job to diagnose and fix the problem. And believe me, serial communication always goes wrong sooner or later. You should NOT swallow exceptions, either catch a very specific one and add a comment as to why you really don't care about it, or log it somewhere so it leaves a trail. 6.
mprice214 wrote:
this.Invoke(new EventHandler(DisplayText));
That does not work, as the parameter list does not match. First make sure which parameters the method should have, then make sure to provide them, using an overload of Invoke(). :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
Not sure I understand your question very well, however here are some remarks: 1.
mprice214 wrote:
RxString.Remove(0, 3); //This removes the first 4 characters
Not true. At best it removes 3 characters; and if there are fewer, expect an exception. 2.
mprice214 wrote:
double.Parse (RxString);
that will fail if the data isn't really a string representation of a double. Don't do this on external data, it will bite you. Either add try-catch or better yet use TryParse(). 3. why does DisplayText() have parameters which make it look like a real event handler, you are not using those parameters, instead you are using some class member. It would seem logical to give it one parameter: the text it should display. 4.
mprice214 wrote:
serialPort1.WriteLine("*X01\r");
very strange. You want a carriage return plus whatever the system uses for a newline (maybe "\r\n"). Either you like what the system gives you, or you don't; I would never mix the two. 5.
mprice214 wrote:
catch (Exception Exception) { return; }
This is not acceptable; whatever goes wrong, you ignore it. So when it goes wrong, you will have an impossible job to diagnose and fix the problem. And believe me, serial communication always goes wrong sooner or later. You should NOT swallow exceptions, either catch a very specific one and add a comment as to why you really don't care about it, or log it somewhere so it leaves a trail. 6.
mprice214 wrote:
this.Invoke(new EventHandler(DisplayText));
That does not work, as the parameter list does not match. First make sure which parameters the method should have, then make sure to provide them, using an overload of Invoke(). :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
Hi all, I have the following code that I need to return dblRx our of DisplayText in order to use it in another function. Problem I'm having is that if I try to return a double from DisplayText (private double DisplayText), I get the EventHandler error from this.Invoke that I have the wrong data type. Any help would be appreciated.
private void DisplayText(object sender, EventArgs e)
{
RxString = RxString.Remove(0, 3); //This removes the first 4 characters
//Need to convert RxString to double for formatting
double dblRx = double.Parse (RxString);
//Converting back to a string will result in leading zeros removed
RxString = dblRx.ToString();
textBox1.Text = RxString;}// End of DisplayText private void serialPort1\_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { RxString = ""; try { RxString = serialPort1.ReadTo("\\r"); serialPort1.WriteLine("\*X01\\r"); } catch (Exception Exception) { return; } this.Invoke(new EventHandler(DisplayText)); }//End of serialPort1\_DataReceived
mprice214 wrote:
this.Invoke(new EventHandler(DisplayText));
Has some paramter issues, to get around it without changing too much code try this
this.Invoke(new MethodInvoker(delegate{DisplayText(null, null);}));
-
If he wants "\r\n" then that is what he should set as SerialPort.NewLine One should not rely on Environment.NewLine for strings that go beyond the current system (files that get exported, serial communication, etc). BTW: MSDN is confusing about the default value of SerialPort.NewLine (it says it is "\n" but refers to Environment.NewLine) :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
Not sure I understand your question very well, however here are some remarks: 1.
mprice214 wrote:
RxString.Remove(0, 3); //This removes the first 4 characters
Not true. At best it removes 3 characters; and if there are fewer, expect an exception. 2.
mprice214 wrote:
double.Parse (RxString);
that will fail if the data isn't really a string representation of a double. Don't do this on external data, it will bite you. Either add try-catch or better yet use TryParse(). 3. why does DisplayText() have parameters which make it look like a real event handler, you are not using those parameters, instead you are using some class member. It would seem logical to give it one parameter: the text it should display. 4.
mprice214 wrote:
serialPort1.WriteLine("*X01\r");
very strange. You want a carriage return plus whatever the system uses for a newline (maybe "\r\n"). Either you like what the system gives you, or you don't; I would never mix the two. 5.
mprice214 wrote:
catch (Exception Exception) { return; }
This is not acceptable; whatever goes wrong, you ignore it. So when it goes wrong, you will have an impossible job to diagnose and fix the problem. And believe me, serial communication always goes wrong sooner or later. You should NOT swallow exceptions, either catch a very specific one and add a comment as to why you really don't care about it, or log it somewhere so it leaves a trail. 6.
mprice214 wrote:
this.Invoke(new EventHandler(DisplayText));
That does not work, as the parameter list does not match. First make sure which parameters the method should have, then make sure to provide them, using an overload of Invoke(). :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
1. mprice214 wrote: RxString.Remove(0, 3); //This removes the first 4 characters Not true. At best it removes 3 characters; and if there are fewer, expect an exception. Yes, understand. I originally was removing 4. 2. mprice214 wrote: double.Parse (RxString); that will fail if the data isn't really a string representation of a double. Don't do this on external data, it will bite you. Either add try-catch or better yet use TryParse(). Understand this too 3. why does DisplayText() have parameters which make it look like a real event handler, you are not using those parameters, instead you are using some class member. It would seem logical to give it one parameter: the text it should display. see #6 4. mprice214 wrote: serialPort1.WriteLine("*X01\r"); very strange. You want a carriage return plus whatever the system uses for a newline (maybe "\r\n"). Either you like what the system gives you, or you don't; I would never mix the two. The device only requires a cr and as result, it is fine with just \r or \r and \n. Why would a newline be necessary here? 5. mprice214 wrote: catch (Exception Exception) { return; } This is not acceptable; whatever goes wrong, you ignore it. So when it goes wrong, you will have an impossible job to diagnose and fix the problem. And believe me, serial communication always goes wrong sooner or later. You should NOT swallow exceptions, either catch a very specific one and add a comment as to why you really don't care about it, or log it somewhere so it leaves a trail. Understand. I am trying to get things working first and then am going back to clean up. As I'm relatively new to c#, I'm sure I'll miss some things. 6. mprice214 wrote: this.Invoke(new EventHandler(DisplayText)); That does not work, as the parameter list does not match. First make sure which parameters the method should have, then make sure to provide them, using an overload of Invoke(). When I run this, it works. Correct me if I am wrong, but doesn't this pass RxSring back to DisplayText? Are you saying there is a better way to do this? Regarding the initial question, I need to pass RxString out of DisplayText as a double to use for graphing purposes.
-
mprice214 wrote:
this.Invoke(new EventHandler(DisplayText));
Has some paramter issues, to get around it without changing too much code try this
this.Invoke(new MethodInvoker(delegate{DisplayText(null, null);}));
-
1. mprice214 wrote: RxString.Remove(0, 3); //This removes the first 4 characters Not true. At best it removes 3 characters; and if there are fewer, expect an exception. Yes, understand. I originally was removing 4. 2. mprice214 wrote: double.Parse (RxString); that will fail if the data isn't really a string representation of a double. Don't do this on external data, it will bite you. Either add try-catch or better yet use TryParse(). Understand this too 3. why does DisplayText() have parameters which make it look like a real event handler, you are not using those parameters, instead you are using some class member. It would seem logical to give it one parameter: the text it should display. see #6 4. mprice214 wrote: serialPort1.WriteLine("*X01\r"); very strange. You want a carriage return plus whatever the system uses for a newline (maybe "\r\n"). Either you like what the system gives you, or you don't; I would never mix the two. The device only requires a cr and as result, it is fine with just \r or \r and \n. Why would a newline be necessary here? 5. mprice214 wrote: catch (Exception Exception) { return; } This is not acceptable; whatever goes wrong, you ignore it. So when it goes wrong, you will have an impossible job to diagnose and fix the problem. And believe me, serial communication always goes wrong sooner or later. You should NOT swallow exceptions, either catch a very specific one and add a comment as to why you really don't care about it, or log it somewhere so it leaves a trail. Understand. I am trying to get things working first and then am going back to clean up. As I'm relatively new to c#, I'm sure I'll miss some things. 6. mprice214 wrote: this.Invoke(new EventHandler(DisplayText)); That does not work, as the parameter list does not match. First make sure which parameters the method should have, then make sure to provide them, using an overload of Invoke(). When I run this, it works. Correct me if I am wrong, but doesn't this pass RxSring back to DisplayText? Are you saying there is a better way to do this? Regarding the initial question, I need to pass RxString out of DisplayText as a double to use for graphing purposes.
mprice214 wrote:
The device only requires a cr
so use
serialPort1.Write("*X01\r");
instead of WriteLine() and you're in charge, now it doesn't matter what SerialPort.NewLine contains any more.mprice214 wrote:
get things working first
sure, so am I. By seeing exceptions right away. As a minimum, add
Console.WriteLine(exception.ToString());
. Either you don't have exceptions, then it makes no difference; or you have, and now you can see them.mprice214 wrote:
it works
Yes, I forgot: Invoke without parameters provides some defaults, and since you don't really use the parameters inside DisplayText, there is no problem. However, I wouldn't do it like that.
mprice214 wrote:
Regarding the initial question
OK, if you are not really interested in the string, why not use a class member "double Rx;" instead of "string RxString;". Have DataReceived read the string, convert it to double, and store the value in Rx; then have DisplayText and any other interested method just read the Rx variable. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
If he wants "\r\n" then that is what he should set as SerialPort.NewLine One should not rely on Environment.NewLine for strings that go beyond the current system (files that get exported, serial communication, etc). BTW: MSDN is confusing about the default value of SerialPort.NewLine (it says it is "\n" but refers to Environment.NewLine) :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
you're welcome. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
mprice214 wrote:
The device only requires a cr
so use
serialPort1.Write("*X01\r");
instead of WriteLine() and you're in charge, now it doesn't matter what SerialPort.NewLine contains any more.mprice214 wrote:
get things working first
sure, so am I. By seeing exceptions right away. As a minimum, add
Console.WriteLine(exception.ToString());
. Either you don't have exceptions, then it makes no difference; or you have, and now you can see them.mprice214 wrote:
it works
Yes, I forgot: Invoke without parameters provides some defaults, and since you don't really use the parameters inside DisplayText, there is no problem. However, I wouldn't do it like that.
mprice214 wrote:
Regarding the initial question
OK, if you are not really interested in the string, why not use a class member "double Rx;" instead of "string RxString;". Have DataReceived read the string, convert it to double, and store the value in Rx; then have DisplayText and any other interested method just read the Rx variable. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
Luc Pattyn wrote:
Yes, I forgot: Invoke without parameters provides some defaults, and since you don't really use the parameters inside DisplayText, there is no problem. However, I wouldn't do it like that.
Do you have any other suggestions?
Luc Pattyn wrote:
OK, if you are not really interested in the string, why not use a class member "double Rx;" instead of "string RxString;". Have DataReceived read the string, convert it to double, and store the value in Rx; then have DisplayText and any other interested method just read the Rx variable.
Thank you for this and also the the comment on the exceptions!
-
Luc Pattyn wrote:
Yes, I forgot: Invoke without parameters provides some defaults, and since you don't really use the parameters inside DisplayText, there is no problem. However, I wouldn't do it like that.
Do you have any other suggestions?
Luc Pattyn wrote:
OK, if you are not really interested in the string, why not use a class member "double Rx;" instead of "string RxString;". Have DataReceived read the string, convert it to double, and store the value in Rx; then have DisplayText and any other interested method just read the Rx variable.
Thank you for this and also the the comment on the exceptions!
you're welcome.
mprice214 wrote:
any other suggestions?
yes, if you need parameters, use the Invoke() overload that takes an array of objects. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
I use it for most things, but when communicating externally I tend to create a constant set to whatever value needs to be defined and use that. I've recently been writing an IMAP thing for work - it's in the RFC that it uses
\r\n
at the end of every command so that's what I use, just in case it ever ends up being ported to a system that doesn't use it!Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)