• Advertisement
Sign in to follow this  

[.net] simple textbox and floating point manipulations?!

This topic is 3235 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So, I'm trying to write a small application in C# that has a simple form interface for the user to enter some floating point values into a few text boxes (e.g. txb_Kp, etc) and then, as needed, spit them out a serial port. The serial port portion of the application works fine, but I'm having a difficult time getting the data I want sent in the right format. I'm rather new to C#... Below is a snippet where I'm using the Convert method to get the text box value into floating point. In one place it works, and in one place (denoted with **ToDecimal**) it doesn't. Any suggestions? Thanks in advance, Walt ---- private void SendData() { //buf = Convert.ToBase64CharArray(trk_thrust.Value.ToString(),String(trk_thrust.Value.ToString())); if (CurrentDataMode == DataMode.Text) { // Send the user's text straight out the port //comport.Write(byte.trk_thrust.Value(), 0, 3); comport.Write(trk_thrust.Value.ToString()); comport.Write(txb_Kp.Text.ToCharArray(),0,3); comport.Write(Convert.ToDecimal(txb_Ki.Text),0,sizeof(Convert.**ToDecimal**(txb_Ki.Text)); comport.Write(txb_Kd.Text); comport.Write(txtSendData.Text); Log(LogMsgType.Outgoing, "\n\nUpdating Parameters:\n"); Log(LogMsgType.Outgoing, trk_thrust.Value.ToString() + "\n"); Log(LogMsgType.Outgoing, txb_Kp.Text + "\n"); Log(LogMsgType.Outgoing, txb_Ki.Text + "\n"); Log(LogMsgType.Outgoing, txb_Kd.Text + "\n"); Log(LogMsgType.Outgoing, trk_thrust.Value.ToString() + "\n"); // Show in the terminal window the user's text Log(LogMsgType.Outgoing, txtSendData.Text + "\n"); } else { ....

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Magos
You're doing something really strange with sizeof.
Try decimal.Parse() and/or decimal.TryParse.


Indeed, in C# you don't use sizeof to get the length of something. The best best way would be to use Decimal.TryParse() as Magos said. To get the length of the data you would then convert it to a string and then get the length.

Eg.
Decimal result = 0;

if (Decimal.TryParse(txb_Ki.Text, out result))
{
int resultLen = result.ToString().Length;

comport.Write(result, 0, resultLen);
}

Share this post


Link to post
Share on other sites
This solution looks right on - Thank you!


I have something relitivly close to what you wrote. I'm proficent in C but just starting C#. Although I've increased my abilities in C# an order of magnitude since I wrote that original post, I still have a lot of learning to do...

Thanks again for the help.

Walt


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement