1

I've some problem writing serial bytes from a python code to arduino. The python code had to write to serial port a number that the arduino receive.

Python3 code:

import serial
import time
ser = serial.Serial ('/dev/ttyACM0',)
ser.baudrate = 115200
ser.write(str(3).encode()) #or (b'3')
ser.write(str('\n').encode())

Arduino code:

void setup(){
Serial.begin (115200); //Comunicazione seriale 115200 bit
servomotore.attach(3);
pinMode(2,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
digitalWrite(2,HIGH);
digitalWrite(12,HIGH);
servomotore.write(180);
}
/*Il loop comprende due funzioni; sensori e Mappa, attivate ogni 15 gradi di movimento del servomotore, 
sensori rileva le distanze, Mappa invia i valori al seriale, ogni ciclo del radar produce 24 valori in centimetri*/
void loop() {
  char buffer[] = {' ',' '};
if (Serial.available() > 0) {


  Serial.readBytesUntil('n', buffer, 2);
  int incremento = atoi(buffer);

If I run this code I can't see output, no error or print, I need to exit with ctrl+c. Arduino doesn't receive nothing. The Arduino code is longer, this is the only part that I can't understand in this moment, it's only a part of a most complex project

6
  • readBytesUntil('n') is never going to return if you never send a n! Commented Mar 30, 2018 at 21:18
  • Is this 'n' an EOF caracters? to indicate the end of line? How can I write this to serial in python? Commented Mar 30, 2018 at 21:31
  • 1
    You are specifying the literal letter n. \n would be the newline character, which is more commonly used as a read terminator - but you're not sending that from the Python side, either. To do so, you'd use serial.write(), exactly as you did for the 3. Commented Mar 30, 2018 at 21:35
  • I changed the code, but it doesn't work, even with the /n, I can't understand why Commented Mar 30, 2018 at 22:14
  • Did you call Serial.begin(115200) in your Arduino setup function? You should show a Minimal, Complete, Verifiable Example which means the setup part of the Arduino code as well as your loop code. Commented Mar 30, 2018 at 22:45

1 Answer 1

1

I cannot reproduce your problem. Here is how I tested it. On the Arduino Uno setup the following program:

void setup()
{
    Serial.begin(115200);
    Serial.println(F("Serial test"));
}

char buffer[80] = { 0 };

void loop()
{
    if (Serial.available() > 0)
    {
        Serial.readBytesUntil('\n', buffer, sizeof(buffer));
        Serial.print(F("read: ["));
        Serial.print(buffer);
        Serial.println("]");
        memset(buffer, 0, sizeof(buffer));
    }
}

The following python3 script successfully reads and writes to this Arduino firmware:

#!/usr/bin/env python3

import sys
from serial import Serial

def main():
    ser = Serial('/dev/ttyACM0',)
    ser.baudrate = 115200

    print(ser.readline())

    ser.write(str(3).encode())
    ser.write(str('\n').encode())

    print(ser.readline())
    return 0

if __name__ == '__main__':
    sys.exit(main())

I get the following output once the device is programmed and plugged in:

~ $ python3 so-check-serial.py 
b'Serial test\r\n'
b'read: [3]\r\n'

Alternative interrupt based serial input

I'd like to add that I would not normally write a serial handler like this in Arduino. The following uses the serial interrupt to buffer the input and set a flag when the line is read completely. In this example we can wait or do other things until a whole line has been receivied (ie: keep blinking at the right times):

#include <Arduino.h>

volatile String buffer;
volatile bool inputComplete = false;

void serialEvent()
{
    while (Serial.available())
    {
        char c = (char)Serial.read();
        if (c == '\n')
            inputComplete = true;
        else
            buffer += c;
    }
}
void setup()
{
    Serial.begin(115200);
    Serial.println(F("Serial test"));
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);
}

void loop()
{
    if (inputComplete) {
        inputComplete = false;
        Serial.println(buffer.c_str());
        buffer = "";
    }
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
    delay(500);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I solved the problem looking at your code, the wrong code was in the arduino side, everything works properly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.