[SOLVED] Arduino can't read libserial data

Hi,

I’m trying to write to the Arduino UNO using libserial in ubuntu 12.04.
I can use libserial to read from the Arduino successfully.

Arduino Code:

#include <Arduino.h>
#include <string.h>

extern HardwareSerial Serial;

String buffer;
int ledPin = 2;

void setup() {
    Serial.begin(9600);
    Serial.flush();
    pinMode(ledPin, OUTPUT);
}

void loop() {
    buffer = "";
    if (Serial.available() > 0) 
    {
        buffer = Serial.readString();
    }
    if (buffer != "")
    {
        digitalWrite(ledPin,HIGH);
        delay(1000);
        digitalWrite(ledPin, LOW);
        Serial.write("received: ");
        Serial.write(buffer.c_str());
        Serial.write ("\n");
    }
}

PC code:
SendSerial is a class that kind of interfaces SerialPort/libserial, but not in the purest sense.
I have tried it without the class and it behaves the same way.

int main(int argc, char** argv) {

    string port = "/dev/ttyACM0";
    
    SendSerial sendSerial(port);
    
    sendSerial.Send("test\0");
    
    return 0;
}

sendSerial:

class SendSerial
{

    string serialPort;
    
    SerialPort * SP;

....

SendSerial::SendSerial(string port)
{
    SP = new SerialPort(port);
    
    baud = SerialPort::BAUD_9600;
    charSize = SerialPort::CHAR_SIZE_8;
    stopBits = SerialPort::STOP_BITS_1;
    parity = SerialPort::PARITY_NONE;
        
    SP->Open(baud, charSize, parity, stopBits);
    
    return;
}

void SendSerial::Send(string strToSend)
{
    if (SP->IsOpen())
    {
        SP->Write(strToSend);
    }
    
    
    return;
}

When I run, all I get is the RX light blinks, then the board’s LED blinks twice as if there is some kind of error, but I have no idea what it may be.

When I try it in the serial monitor it works perfectly, the LED on pin2 flashes and the string is echoed back.

Any help is appreciated.

Karl.

extern HardwareSerial Serial;

Why?

String buffer;

Why?

    Serial.flush();

You haven't sent anything. Why are you blocking waiting for it to be sent?

Opening the serial port resets the Arduino. You aren't waiting for it to reset before you write to it. Of course that data is going to be lost.

Hi PaulS

Thanks for the reply.

I'm using Netbeans to program the arduino. I can try the stock IDE and see whether that makes a difference I'll give that a go now. The extern was placed there by the plugin, unsure what it does exactly.

the buffer is to hold the input received from the PC.

flush is to clear the serial port.

Ok. So if I understand correctly, I need a delay to wait until the board is reset before I send?
Interestingly I had a Serial.write("ready") at the end of setup(), but it seemed never to be sent...

Thanks again.

the buffer is to hold the input received from the PC.

But using a String pisses away resources that are in short supply. Learn to use c-strings (NULL terminated arrays of chars) instead.

flush is to clear the serial port.

The outgoing port that held nothing. It does not clear the buffer. It simply blocks until the buffer is emptied as interrupts can be fired to shuffle the data out.

I need a delay to wait until the board is reset before I send?

Better would be to wait until the Arduino sent something, like "Ready".

Interestingly I had a Serial.write("ready") at the end of setup(), but it seemed never to be sent...

Because your application quits before the Arduino sends anything.

flush used to empty the read buffer in the pre-1.0.0 core. In the 1.0.0+ core it waits for the send buffer to be emptied by the hardware.

To emulate the old flush() operation, just do:

while (Serial.read() != -1);

thanks everyone, I removed flush.

I built on the stock IDE just to be sure, but it was working ok with serial monitor with Netbeans build.

Because your application quits before the Arduino sends anything.

tested with the serial port monitor same behaviour.

Also, I tried the other stub I have that can read from Arduino.

Will modify Arduino code to send "ready" at end of setup()
Will modify PC code to wait for input before writing after opening the port.

be a minute...

Worked.

Thank you everyone, especially PaulS.