hey everyone,

we have an argument that figured up the angle in a double. We need to convert that double to a char and send it out the serial port. I've tried this:

char t1 = (char)A;
            comBobbySue.Write(A);

but i get errors. can someone give me a hand? :(

Would you be able to convert the double to a string, then loop through the string and pass that on to the com?

//Might want to use a format provider here
string s = A.ToString();
foreach(char c in s)
{
comBobbySue.Write(c);
}

yeah maybe. Is there any way to send an Int or Double to the COM port?

awesome, it's working great. Here's the only problem I've run into.

I'm sending angles to the controller. So when I get to angle 256, it errors out because of the maximum size of the Byte.parse

Can I use something larger than this? I just need something that will accept 360ish

You can use this method, might want to change your previous method before too.

That method takes both strings and doubles

byte[] buffer_2_send = BitConverter.GetBytes(256);

not sure what you mean by how many bits to take in, you can always make the byte buffer any size you want

the method above converted the int 256 into a byte buffer, so you didn't need to use Byte.parse with a fixed size of 1 for the byte buffer

Ok I'm a little confused.
Say I want to send the double A out the COM port. what would be the easiest way?

I've got it working to where it will parse the text box, but there's got to be a way to just convert the double itself.

Thanks for the help

BitConverter is overloaded to accept double, int, and string i believe.

you can parse a double out of the textbox

double d = Double.Parse(TextBox.Text);
byte[] buffer_2_send = BitConverter.GetBytes(d);

I think this is what you meant, but if not, what are you meaning by 'just convert the double itself'?

Ok, that answers my question. I'll try it when I start working on the program again.

We already have D computed from a previous section of code. I just wanted to know if there was a way to use D directly, without using the textbox.

You can pass in any value, i just made a simple example of getting a string from a textbox

if you calculate it, just pass in the calculated value and set d to the value

I can't get it to work that way. Here's the code I tried:

byte[] outBufferAngle = BitConverter.GetBytes(A);
            byte[] outBufferVelocity = BitConverter.GetBytes(V);

            comBobbySue.Write(outBufferAngle, 0, outBufferAngle.Length);
            comBobbySue.Write(outBufferVelocity, 0, outBufferVelocity.Length);

V and A are both doubles that have been calculated earlier. When I send the buffer out of the COM port, I don't get the correct output.

For example if I send

A = 90
V = 25

V is output as Hex 00 rather than Hex 19.

If I try to send

A = 160
V = 45

V is output as Hex 47 rather than Hex 2D.

The output is random for every time it's sent. Could this have anything to do with the unpredictability of the value after the decimal point? At any given point, A or V could have the value of x.329847 or some other non-integer number.

This article has been dead for over six months. Start a new discussion instead.