Communication via bash and serial port is not possible

Hi guys,

so I'am using an Arduino Pro Micro and macOS 10.15.3, connected via a C-microUSB cable and I try to send commands to the arduino with the terminal.

The code and the rest of the setup is from this tutorial: connect to Arduino from command line.

So when I echo (echo "ON">/dev/tty.usbmodem14401) something to the Arduino I never get a response at all, it just doesn't work.

String inData;

int ledPin = 17;

void setup() {
    pinMode(ledPin, OUTPUT);
    Serial.begin(9600);
    Serial.println("Serial conection started, waiting for instructions...");
}

 

void loop() {
    while (Serial.available() > 0)
    {
        char recieved = Serial.read();
        inData += recieved; 
        // Process message when new line character is recieved
        if (recieved == '\n')
        {
            Serial.print("Arduino Received: ");
            Serial.print(inData);
            digitalWrite(ledPin, HIGH);
            // You can put some if and else here to process the message juste like that:
            if(inData == "OFF\n"){ // DON'T forget to add "\n" at the end of the string.
              Serial.println("LED OFF");
              digitalWrite(ledPin, LOW);
            }
            inData = ""; // Clear recieved buffer
        }
    }
}

probably because you are not sending the newline character

.

Ah thank you, seems like a stupid mistake… I guess I got lost in all the echo, stty, screen, cat stuff…
So if I write “OFF\n” into my “command.bin” which is getting cat over, nothing is happening, but when I write “1\n” the LED is turning off.

Two things ...

When a PC program opens the Serial port it causes the Arduino to reset and the PC program needs to allow time for that to happen before sending data. In my programs I get the Arduino to send something like Serial.println("Arduino is ready"); from setup() and my PC program waits to receive that before continuing. Also the PC program should keep the serial port open until it is completely finished with the Arduino. This Simple Python - Arduino demo illustrates this.

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R