Go Down

Topic: Serial Communication with lua on flight simulator (Read 757 times) previous topic - next topic

DK7

Hello, I am trying to communicate between my flight simulator(P3D) and Arduino using LUA script.
Currently, I am monitoring the vertical speed of my aircraft and sending that data over to the Arduino. Also, using Lua script I am logging the data. 
My problem is that my Arduino seems to be receiving data as the RX pin is constantly blinking. Yet I don't see the LCD screen printing the values. Sometimes the data gets printed on to my LCD screen, but that is like a once every 100 tries situation.  I have attached below the Lua script and Arduino code. Can someone please help me out in figuring what is wrong here?
----------------------------LUA SCRIPT STARTS HERE------------------------------------------------------
-- LUA To Arduino Script File By Trevor Hale http://www.cockpitbuilders.com

-- Varialble Declare
foot =3.28083989501312
red_string = ""


-- Open Seial Port and connect to Arduino
speed = 115200
handshake = 0
serial_wait = 100
inited = 0
states = {}
dev = com.open("COM6", speed, handshake) -- Change Port Number for your Arduino
if dev == 0 then
    ipc.display("Could not open device port")
    ipc.exit()
end

-- Finish Serial Connect

function call_vertical (offset, value) -- function call to vertical speed

   vs = ipc.readSW(0x842)  --reading the offset
   value = (-1 * (vs*3.28084)) --conversion so that the values make sense
    --value = 10
   ipc.log (string.format("%d", value).. "\n") --logging data
   com.write(dev,string.format("%d", value) .. "\n") --writing to com port
   ipc.sleep(50)    --small delay

end
event.offset (0x0842, "SW", "call_vertical") -- aircraft Vertical Speed --event read
---------------------------------------LUA SCRIPT ENDS HERE-------------------------------------------


Thanking you
Dinesh

Robin2

It would have been a big help if you had included your Arduino code in your Post.

Code: [Select]


// ***********************BEGIN DECLARATIONS********************************************
#include <LiquidCrystal_I2C.h> //i2c interfaced lcd display
LiquidCrystal_I2C lcd(0x27, 16, 2);  //initialise parameters for lcd
#include <Wire.h>  //for i2c comm
char endmarker = '\n';  //end marker sent by lua so 
char vsi[30];  //initalise character array
int i = 0;   //initialise variable
//**********************************************************************
// ***********************BEGIN VOID SETUP*******************************
void setup()
{

  Serial.begin(115200); //baud rate
  Wire.begin(); //i2c comunications start
  lcd.begin();  //for the lcd to start
  lcd.backlight();   
  lcd.print("All Good"); //setup is finished

  // --------Increase PWM refresh rate to 240Hz
  // ---------------------------------------

}
//end void setup
//****************************************************************************
// **************BEGIN VOID LOOP********************************************************
void loop()
{
  delay(100); //just a delay
  if (Serial.available() > 0) {  //if there is a charactor in the serial receive buffer then ,,,,
    char rc = Serial.read();   // read the first cahracter
    while (rc != endmarker)  //check for the xent line character
    {
      vsi[i] = rc;     // store in the character array
      i++;  //increment i for the loop
    }


    if (rc == endmarker) //end marker is reached
    {
      lcd.print(vsi);  //print command for the lcd to print
      i = 0;           //reset i

    }
  }
}




//please ignore anything below this


//    vsi = "";
//    vsi += char(Serial.read());
//    vsi += char(Serial.read());
//    vsi += char(Serial.read());
//    vsi += char(Serial.read());
//    vsi = vsi.toInt();
//    lcd.clear();
//    lcd.print(vsi);
//    lcd.print(" ");




...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Robin2

It's not a good idea to have a delay() in a receiving program. It should be listening all the time.

It is also not a good idea to mix up your LCD code with your serial reception code - it makes debugging difficult.

Have a look at the 3rd example in Serial Input Basics. Note how it sets a flag to let the rest of the program know that a complete message has been received.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

DK7

Thank You for your reply. I changed the Arduino code a little and it works good enough, but only on a UNO. The moment I switch to MEGA the same problem occurs. This is something strange

I have added the updated program below. I have checked for any connection error, but couldn't find any and also the message' ALL GOOD' gets printed.

I will also look into the example that you suggested

Code: [Select]

#include <LiquidCrystal_I2C.h> //i2c interfaced lcd display
LiquidCrystal_I2C lcd(0x27, 16, 2);  //initialise parameters for lcd
#include <Wire.h>  //for i2c comm
char endmarker = '\n';  //end marker sent by lua so  
char vsi[30];  //initalise character array
int i = 0;   //initialise variable
//**********************************************************************
// ***********************BEGIN VOID SETUP*******************************
void setup()
{

 Serial.begin(57200); //baud rate
 Wire.begin(); //i2c comunications start
 lcd.begin();  //for the lcd to start
 lcd.backlight();    
 lcd.setCursor(0,0);
 lcd.print("All Good"); //setup is finished

 // --------Increase PWM refresh rate to 240Hz
 // ---------------------------------------

}
//end void setup
//****************************************************************************
// **************BEGIN VOID LOOP********************************************************
void loop()
{
   // delay(50); //just a delay
 if (Serial.available() > 0) {  //if there is a charactor in the serial receive buffer then ,,,,
   char rc = Serial.read();   // read the first cahracter
   while (rc != endmarker)  //check for the xent line character
   {
     vsi[i] = rc;     // store in the character array
     i++;  //increment i for the loop
     rc = Serial.read(); //added to the loop
   }


   if (rc == endmarker) //end marker is reached
   { lcd.clear();
     lcd.print(vsi);  //print command for the lcd to print
      i = 0;           //reset i

   }
 }
}



Robin2

Thank You for your reply. I changed the Arduino code a little
To make it easy for people to help you please modify your post and use the code button </>


Code: [Select]
so your code
looks like this
and is easy to copy to a text editor. See How to use the Forum

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up