0

I'm new to RP3, but fairly comfortable with Arduino. Maybe this is an easy one.

I am trying to use pySerial to send a single byte from a Raspberry Pi 3 to an Arduino Mega 2560, like so:

#RP3 python code
import serial  
s = serial.Serial('/dev/ttyACM0', 115200)  
s.write(b'\x50')  


//Arduino code
byte data;
void setup() {
    Serial.begin(115200); 
}

void loop() {  
        if (Serial.available() > 0) {  
                data = Serial.read();  
        }  
        Serial.println(data);  
}  

When I run the python script and enter, say, '\x50' to the Arduino, the Arduino serial monitor will show something like (80 being 0x50 in decimal):

80
121

or

80
106

The second byte is usually 106 or 121, but I've seen other values.

My pySerial and python are up-to-date. I'm using the simple Thonny python editor that comes with RP3 (just pressing the Run Script button to send the data) and the latest release of the Arduino IDE for RP.

Any idea what is going on here?

  • You seem to be opening the port, sending one character, and then closing the port. Perhaps a spurious character on port close? What happens if you leave the port open and loop sending a character every second? – joan Oct 16 '17 at 12:58
  • What happens if you try to declare your data variable as an integer? Does it throw an error? – LecauseAndThePi Oct 16 '17 at 15:10
  • I think it's more to do with encoding and decoding rather than the serial port itself. Also if you are using Python 2, your b prefix will be ignored. – LecauseAndThePi Oct 16 '17 at 15:27
  • LecauseAndThePi-- No error is thrown when I switch data from byte to int. I appear to get the same result. – schadjo Oct 16 '17 at 23:26
  • LecauseAndThePi-- python --version says I'm using 2.7.13, but if I remove the b, I get an error: unicode strings are not supported please encode to bytes: 'a' – schadjo Oct 16 '17 at 23:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.