Home>

Rasppie and PC are connected via serial communication
I'm trying to transfer images from Rasppie to a PC with Python3,
The following error has occurred.

Error message
File "C: ~ \ bytes_receive_1.py",
line 16, in bytes_receive
    img_bytes = "" .join (img_bytes_list) #list → character string
TypeError: sequence item 0: expected str instance, bytes found
Applicable source code
Python3
PC side
server_main_0_1.py
#-*-coding: utf-8-*-
from bytes_receive_1 import bytes_receive
from bytes_convert_img_0_2 import convert_img
img_bytes = bytes_receive ()
img = convert_img (img_bytes)
img.save ('./ images/decoded_img.jpg', quality = 75)

bytes_receive_1.py
#-*-coding: utf-8-*-
import serial
def bytes_receive ():
    ser = serial.Serial (
            port = "COM6",
            baudrate = 115200,
            parity = serial.PARITY_NONE,
            stopbits = serial.STOPBITS_ONE,
            bytesize = serial.EIGHTBITS,
            timeout = 5
            )
    img_bytes_list = ser.readlines ()
    print (img_bytes_list)
    img_bytes = "" .join (img_bytes_list) #list → character string
    print (img_bytes)
    ser.close ()
    print ("End")
    return img_bytes

Rasp pie side
client_main_1.py
#-*-coding: utf-8-*-
from img_convert_string_1_1 import convert_string
from string_transfer_0 import transfer_to_pc
img_gray = "../images/smile_img.jpg"
img_string = convert_string (img_gray)
transfer_to_pc (img_string)

img_convert_string_1_1.py
#-*-coding: utf-8-*-
#import numpy as np
from PIL import Image
def convert_string (img_gray = "../images/smile_img.jpg"):
    #img = np.array (Image.open (img_gray)) #Image is converted to ndarray
    img = Image.open (img_gray)
    (width, height) = img.size
    print ((width, height))
    #img_string = img.tobytes () + b "\ n"
    img_string = img.tobytes () #Convert to byte array
    return img_string

string_transfer_0.py
#-*-coding: utf-8-*-
import serial
def transfer_to_pc (string):
    port = serial.Serial (
                port = "/ dev/ttyS0",
                baudrate = 115200,
                parity = serial.PARITY_NONE,
                stopbits = serial.STOPBITS_ONE,
                bytesize = serial.EIGHTBITS,
                timeout = 3.0
                )
    port.write (string)
    port.close ()
Tried

Server_main_0_1.py on the PC side
Run client_main_1.py on the rasp pie side.

Supplemental information (FW/tool version etc.)

Please provide more detailed information here.

  • Answer # 1

    That's fine if you follow tachikoma's advice.
    Specifically,
    Changed as follows in bytes_receive_1.py on the PC side.

    img_bytes ="".join (img_bytes_list) #list → string


    img_bytes = b"".join (img_bytes_list) #list → string

  • Answer # 2

      

    img_string = img.tobytes () #Convert to byte array

    A byte string, not a byte string?