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
readBytesUntil('n')is never going to return if you never send an!n.\nwould 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 useserial.write(), exactly as you did for the3.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.