usb communication

hi, I'm a newbie in arduino programation en C too (done it 20 years ago at school lol). I've made a plugins for X-plane to send data over usb and the arduino have to pick up this info and screen on lcd information.
That works but for that I need to popin the serial monitor. indeed if I do not start it, the information not work !!! why ?

For exemple I send over usb from mac to arduino these words :"Speedbrake Arm" or "Speedbrake Full".

the result should on my lcd 20*2 Speedbrake Arm or Speedbrake Full, depend on what are send to usb.

It works but I have to start the arduino screen usb monitor.

Indeed I must start he serial monitor screen of the arduino, then send Speedbrake Arm; the lcd shows Arm well.

But then to show Full I must close the serial monitor and restart it then when I send Speedbrake Full then full is print on my lcd correctly.

do you know why? do you have a clue ?

thanks in advance

ounet

Here we are my code

#include <stdio.h>
#include <string.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int ledPin = 13;
char indata[30];
int count = 0;

void setup ()
{
pinMode(ledPin,OUTPUT);
Serial.begin(115200);
lcd.begin(20, 2);
lcd.setCursor(0, 0);
lcd.print("Speed Brake Value");
}

void loop()
{20
digitalWrite(ledPin,LOW);
while (Serial.available() > 0)
{
char incomingByte = Serial.read ();
if(count < 29)
{
indata[count] = incomingByte;
count ++;
indata[count] = '\0';
}
}
if ( strcmp (indata,"Speedbrake Full") == 0)
{
//Serial.println("full");
lcd.setCursor(0, 1);
lcd.print("Speedbrake Full");
digitalWrite(ledPin,HIGH);
delay(2000);
digitalWrite(ledPin,LOW);
count =0;
indata[count] = '\0';
}
if ( strcmp (indata,"Speedbrake Arm") == 0)
{
//Serial.println("arm");
lcd.setCursor(0, 1);
lcd.print("Speedbrake Arm");
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay(1000);
digitalWrite(ledPin,HIGH);
count =0;
indata[count] = '\0';
}
indata[count] = '\0';
}

I've made a plugins for X-plane to send data over usb and the arduino have to pick up this info and screen on lcd information.
That works but for that I need to popin the serial monitor.

What is X-plane? How does it communicate with the serial port?

When you open the serial monitor, the com port that the Arduino listens to is bound to. No other application can then use that same com port.

These are the last two lines of your code.

indata[count] = '\0';
}

The same value is being overwritten many times per second. The statement is completely unnecessary.

Dear Pauls,

X-plane is a flight simulator like microsoft FS. I use a C code to create plugins and send throught fopen fonction to my arduino usb card some information when I use my home cockpit. Like push bouton to arm the speedbrake of the plane. When I press the bouton, the plugins shouls send over the usb arduino the string "speedbrake arm" and I compare what I receive on my arduino. I open the usb like :
file * fd;
fr = fopen("/dev/cu.usbcardardunio","w");
fprintf(fd,"speedbrake");

it work but my arduino need to have the serial monitor to work and do not know why ?

thanks for your help

ounet

The name you are using for the serial port does not look right. Is it the same name that you see in the Arduino IDE list of ports? That is, is it the same port that you use to upload the sketch to the Arduino?

Are you opening a new connection (a new call to fopen) to send each command?

fprintf(fd,"speedbrake");

if ( strcmp (indata,"Speedbrake Full") == 0)

if ( strcmp (indata,"Speedbrake Arm") == 0)

Neither of these matches what you say you are sending.

the exact line is fd = fopen("/dev/cu.usbserial-A6008bcO","w"); and it's the same name on my arduino ide list port.

Are you opening a new connection (a new call to fopen) to send each command?

Yes every time I call my sub routine from my plugins I use fopen and fclose. so

if ( strcmp (indata,"Speedbrake Full") == 0)

fd = fopen("/dev/cu.usbserial-A6008bcO","w");
fprintf("Speedbrake Full");
fclose(fd);

if ( strcmp (indata,"Speedbrake arm") == 0)

fd = fopen("/dev/cu.usbserial-A6008bcO","w");
fprintf("Speedbrake Arm");
fclose(fd);

as I'm a newbie in C I do not know if its the right code.

Thanks

ounet

Every time you open the serial port, the Arduino resets. Just so you know...

Every time you open the serial port, the Arduino resets. Just so you know...

ok so I move and now I open only one time the usbport. And now it still works but I have to push the reset card buton for having my screen information. One time I get Speedbrake Full......reset card .....After Speedbrake Arm.....reset card.....

do you know why because now I open only one time the usb.

ounet

Use the lcd to show debug messages, too.

Add a lcd.print in setup to show when it is called.
Add an lcd.print in the while(Serial.available() > 0) loop, to show that more serial data has arrived.

Print each character received. Surely, something will become obvious.

hello Pauls, now it works fine. The problem was on my plugins. it was sending information over my usb other information.

thanks you very much for your help.

ounet

Dear Pauls here we are the result. Thanks for your help.

in french sorry about ;D
jean-marc