arduino and raspberry pie communication version encoding problem

In a communication experiment between arduino and Raspberry Pi, I found a problem, that is, the versions of Python3 and Python2 have different methods for encoding. If you don't pay attention, it will cause communication errors or run errors. The author tried it. Found the version 3 code, python2 can also be achieved, the reverse will not work, the following to illustrate

arduino code

int val; // Define the variable val 
int ledpin = 13; // Define the digital interface 13 
void setup () 
{ 
  Serial.begin (9600); // Set the baud rate to 9600, which should be consistent with the software settings. When accessing a specific device (such as Bluetooth), we must also achieve the same baud rate as other devices. 
  pinMode (ledpin, OUTPUT); // Set the digital port 13 as the output interface, the I / O ports we use on the Arduino must be defined like this. 
} void loop () 
{ 
  val = Serial. read (); // Read the command or character sent from the PC to the Arduino, and assign the command or character to val if (val == 'R') // Judge the reception Whether the received instruction or character is "R". 
  { // If the received "R" character 
    Serial.println (" Hello World! "); // Display the string "Hello World!" 
    DigitalWrite (ledpin, HIGH);

  

// Light up the digital 13-port LED. 
    delay (500); 
    digitalWrite (ledpin, LOW); // turn off the digital 13 port LED 
    delay (500); 
  } 
}

Python2 normal running code

#-*-coding:UTF-8-*-
import serial
ser = serial.Serial('/dev/ttyACM0',9600,timeout=1);
try:
        while 1:
                ser.write('R');
                response = ser.readall();
                print response;
except:
        ser.close();

What is returned is

Hello World!

Hello World!


The normal running code of Python3

#-*-coding:UTF-8-*-
import serial
ser = serial.Serial('/dev/ttyACM0',9600,timeout=1);
try:
        while 1:
                ser.write('R'.encode());
                response = ser.readall();
                print (response);
except:
        ser.close();

The program returns this

b'Hello World!\r\n'
b'Hello World!\r\n'
And the comparison shows that the function syntax is different:

ser.write('R'.encode());
print (response);

Python3 conversion encoding code

#-*-coding:UTF-8-*-
import serial
ser = serial.Serial('/dev/ttyACM0',9600,timeout=1);
try:
	while 1:
		ser.write('R'.encode());
		response =ser.readall().decode();
		print (response);
except:
	ser.close();

The final effect is the same as 2. , And python2 can also run

Hello World!

Hello World!

to sum up

This is actually a very simple coding problem,

Python 2 treats strings as bytes.
Python 3 treats strings as unicode.

I have forgotten everything for a long time before learning. Such coding problems should be learned. It seems that it is still necessary to write more to deepen memory and facilitate recall.

Question reference

https://stackoverflow.com/questions/46685761/typeerror-unicode-strings-are-not-supported-please-encode-to-bytes-x08

Encoding prefix

https://blog.csdn.net/anlian523/article/details/80504699

Code switching

https://blog.csdn.net/Panda996/article/details/84780377

Guess you like

Origin www.cnblogs.com/nightowl/p/12758182.html