Python serial communication with Arduino - cannot recognize carriage return

I am trying to establish a connection between the BeagleBone version with Ubuntu 10.04 and Arduino. So far, I can send the string in order, but I am having trouble recognizing a newline character.

From the BB side, in Python3 and using PySerial, I (edited only for the corresponding bits):

import serial
ser = serial.Serial('/dev/ttyACM0', 9600)

ser.write(chr(13).encode('ascii')) # establishes serial connection 
                                   # which will reset Arduino

delay(3000) # give time for reset. This method is from my own library

mesg = 'Beagle\n'
ser.write(mesg.encode('ascii'))

On the Arduino side, I have:

boolean newMsg = false;
String incomingMsg = "";

void setup()
{
   /* some stuff - not relevant */
   Serial.begin(9600);
}

void loop()
{
    if(newMsg) {
        lcd.print(incomingMsg);
        newMsg = false;
    }
}

void serialEvent()
{
    incomingMsg = "";
    while (Serial.available() > 0) {
        char inByte = Serial.read();
        if (inByte == '\n') {
            newMsg = true;
        } else {
            incomingMsg = incomingMsg + inByte;
        }
    }
}

The problem is that newMsg never gets true because it is if (inByte == '\n')never checked as true. I tried using '\ r' on both sides and even the characteristic type ('#'), but this test never works. The string "Beagle" does everything well, so it incomingMsgis created normally.

, Arduino Processing (Arduino , Bluetooth). , . ?

. , , , , .

if (inByte == '\n') . - true var newMsg . serialEvent(), (). var (, , Arduino). () :

if (messageReceived != "")

. , .

+3
4

newMsg , newMsg volatile:

volatile boolean newMsg = false;

, , .

Arduino Volatile

+4

Windows, :

Serial, :

ser = serial.Serial('/dev/ttyACM0', 9600) # mode = 'w'

.

ser = serial.Serial('/dev/ttyACM0', 9600, 'wb') # may not be correct

.

(... mode =...) . , "w" - , "wb".

windows\r IO posix. .

0

You are missing the appropriate code. The function serialEventcontains a loop whilethat will not work until all the data has been read and calls your function loop.

0
source

I had this problem when I used PARITY_ODD / bytesize = serial.SEVENBITS since I changed it to:

ser = serial.Serial(
    port='/dev/ttyUSB0',
    baudrate=9600,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_TWO,
    bytesize=serial.EIGHTBITS
)

works great.

0
source

All Articles