araffin / arduino-robust-serial

A simple and robust serial communication protocol. It was designed for Arduino but can be used for other purposes (e.g. bluetooth, sockets). Implementation in C Arduino, C++, Python and Rust.
https://medium.com/@araffin/simple-and-robust-computer-arduino-serial-communication-f91b95596788
MIT License
135 stars 28 forks source link

C++ Implementation not communicating #5

Open danimasa opened 4 years ago

danimasa commented 4 years ago

I'm having a problem running code from the C++ implementation.

I copied robust_serial.hpp to my project and implemented a class to read and write information to the serial port using std::fstream.

std::fstream serial_port;
serial_port.open(device, ios::in|ios::out|ios::app|ios::binary);
write_order(serial_port, SEND);
write_i32(serial_port, id);

The implementation in my arduino is like that:

setup() {
    Serial.begin(SERIAL_BAUD);
}

loop() {
    if(Serial.available() > 0)
    {
        Order order_received = read_order();
        switch(order_received)
        {
            case SEND:
                 uint32_t ID = (uint32_t) read_i32();
        }
    }
}

So I adjust the stty from the terminal: stty 115200 -F /dev/ttyACM0

But the data is not arriving to the arduino board, but i noticed that if I use a sniffer like interceptty like that: interceptty /dev/ttyACM0

That create a bind to the /dev/pts/3, and if I connect to that port the data is Ok and is arriving to arduino. But if I remove the interceptty and connect direct to the /dev/ttyACM0 it stop working again. And if I start interceptty and kept it running I can communicate either, even with the /dev/ttyACM0 port.

What could be intercepting my communication with the board?

araffin commented 4 years ago

Hello,

I copied robust_serial.hpp to my project and implemented a class to read and write information to the serial port using std::fstream.

Why didn't you use the examples from https://github.com/araffin/cpp-arduino-serial/tree/2075fd8f203483af9c82caffba4bdbc84024ea63/examples ?

EDIT: your implementation looks like the same as in the examples

araffin commented 4 years ago

What could be intercepting my communication with the board?

To be honest, I have no idea... I usually had trouble reading packets coming from the Arduino, not the opposite way. Are you also using the arduino code from this repo?

danimasa commented 4 years ago

Hello @araffin

Why didn't you use the examples from https://github.com/araffin/cpp-arduino-serial/tree/2075fd8f203483af9c82caffba4bdbc84024ea63/examples ?

EDIT: your implementation looks like the same as in the examples

I used the code from file_read_write.cpp example.

Are you also using the arduino code from this repo?

Yes, I'm using exacly the same implementation used in slave.cpp. But I'm not keeping the connected flag, I'm receiving the messages direcly in arduino loop funcition, first receiving the order and then the data.

What I found weird is that if interceptty is running all works normal. It seems like some configuration of the serial port interfere in the communication, and that service applies it when running.

araffin commented 4 years ago

as a sanity check, can you send/receive something using Arduino IDE?

danimasa commented 4 years ago

Yes, if I send anything the Serial Monitor returns my error NotFound order (I couldn't manage to send data not encoded in ASCII using the Serial Monitor to see if it responds correctly send my data).