0

I'm building a flight simulator where i have a program called fsuipc that allows you to connect to microsoft flight simulator x and you can read and write data in c# - very handy!

I have built a vertical speed gauge that is simply a servo with a needle on it.This connects to an arduino which runs a modded version of c++. I want c# to send the current vertical speed to the arduino but have encountered issues and have tried for the past 2 days.

The issue is that what the arduino sends back is mostly -1 with the odd 49 thrown in when it should be sending back something like 1566 (feet per min).

Help!

here is the section of c# code that deals with the vertical speed (ignore airspeed that works). The value that shows the current airspeed works fine but the sent then received is totally wrong

private void timer1_Tick(object sender, EventArgs e)
{
    // Process the default group
    try
    {

        FSUIPCConnection.Process();


        // IAS - Simple integer returned so just divide as per the 
        // FSUIPC documentation for this offset and display the result.
        double airpeedKnots = ((double)airspeed.Value / 128d);
        this.txtIAS.Text = airpeedKnots.ToString("f1");

        double verticalspeedfpm = ((double)verticalspeed.Value * 60 * 3.28084 / 256); //gets vertical speed in meters per sec and converts it to feeet per min.
        this.txtvsi.Text = verticalspeedfpm.ToString("f1"); //outputs it to a test box so i can see what the vertical speed is.

        serialPort1.Open(); //opens serial port (com 4) - arduino
        serialPort1.WriteLine(verticalspeedfpm.ToString("f1")); //sends the vertical speed data
        read = (serialPort1.ReadLine()); // reads what the arduino sent back
        serialPort1.Close();//closes the serial port
        txtrecieved.Clear();//cleares the text box
        this.txtrecieved.Text = read;//writes what was recieved 
        // Avionics Master Switch
        this.chkAvionics.Checked = (avionics.Value > 0);  // 0 = Off, 1 = On.

here is the arduino code that simply pings back the message:

int vsint;
String vsstring;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  vsstring = Serial.read();
  delay(10);
  Serial.println(vsstring);
}

the windows form - the call back box should be equal to the vs box.

6
  • Thanks for the help, how do i use serial.available()? Commented Oct 8, 2017 at 14:00
  • Thankyou, tried that and its got rid of the -1's however the correct value is not returned its now a mix of 10's and 40-50;s? any idea whats going on? should be like 1750 Commented Oct 8, 2017 at 14:07
  • Thankyou for your help Commented Oct 8, 2017 at 14:08
  • added a picture Commented Oct 8, 2017 at 14:16
  • i tried with the airspeed value and it was exactly the same, it displayed seemingly random values. Commented Oct 8, 2017 at 14:20

1 Answer 1

1
void loop() {
  // put your main code here, to run repeatedly:
  vsstring = Serial.read();
  delay(10);
  Serial.println(vsstring);
}

In your loop you are reading from serial without first checking to see if there is anything to read (available()). When you call Serial.read() and there is nothing in the receive buffer it returns -1. That's where the -1 is coming from.

As for the 49, you're getting that because you are sending the number as ascii text. Look up the ascii table and see if the numbers you are getting don't suddenly make a ton of sense. You are reading from Serial into a String and not a char. read() returns an int. String does allow for an int on the RHS of an =. And when it sees one, it converts the int to ascii. So you get an ascii representation of the ascii code.

Sign up to request clarification or add additional context in comments.

1 Comment

As an aside, it is generally best to avoid the String class on a microcontroller with such limited memory. Not only is String wasteful of memory, but no garbage collection makes dynamic allocation dangerous. It is best practice to stick with char arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.