Home>

Read the serial communication information from arduino with python.

Please write your question in detail here.
I want to receive information from COM3 using python's jupter notebook.
However, the following message does not work.
Where should I change it?

Error message
ValueError Traceback (most recent call last)
<ipython-input-1-d6ee45546a66>in<module>()
     62
     63 if __name__ == '__main__':
--->64 main ()
<ipython-input-1-d6ee45546a66>in main ()
     28
     29
--->30 xdeg = float (xdeg)
     31 ydeg = float (ydeg)
     32 # Update temperature data list
ValueError: could not convert string to float: "'55"
Applicable source code
import numpy as np
import matplotlib.pyplot as plt
import pygame
from pygame.locals import *
import serial
import sys
def main ():
    ser = serial.Serial ("COM3") # COM port (Arduino connection)
    xdegs = [0] * 100 # Store temperature
    ydegs = [0] * 100 # Store temperature
    t = np.arange (0,100,1)
    plt.ion ()
    # Pygame settings
    pygame.init () # initialization
    screen = pygame.display.set_mode ((200, 200)) # Create screen (100 × 100)
    pygame.display.set_caption ("tilt angle") # title bar
    font = pygame.font.Font (None, 30)
    while True:
        data = ser.readline (). rstrip () # Read up to \ n (\ n removed
        data = str (data) .replace ('"', '')
        (xdeg, ydeg) = data.split (",")
        xdeg = xdeg.replace ('b', '')
        ydeg = ydeg.replace ('b', '')

        xdeg = float (xdeg)
        ydeg = float (ydeg)
               # Update temperature data list
        xdegs.pop (99)
        xdegs.insert (0, float (xdeg))
        ydegs.pop (99)
        ydegs.insert (0, float (ydeg))
        # Set graph display
        line, = plt.plot (t, xdegs, 'r-', label = "X-axis [deg]") # Y-axis update
        line, = plt.plot (t, ydegs, 'b-', label = "Y-axis [deg]") # Y-axis update
        line.set_ydata (xdegs)
        line.set_ydata (ydegs)
        plt.title ("Real-time inclination angle")
        plt.xlabel ("Time [s]")
        plt.ylabel ("Inclination angle [deg]")
        plt.legend ();plt.grid ()
        plt.xlim ([1,100]);plt.ylim ([-90,90])
        plt.draw ();plt.clf ()
        # Pygame processing
        screen.fill ((0,0,0)) # clear screen
        text = font.render ("(X, Y) = (" + xdeg + "," + ydeg + ")", False, (255,255,255))
        screen.blit (text, (10, 10)) # render, display position
        pygame.display.flip () # update screen to reflect changes
        # Pygame event processing
        for event in pygame.event.get ():
            # End processing when end button is pressed
            if event.type == QUIT:
                pygame.quit ()
                ser.close ()
                plt.close ()
                sys.exit ()

if __name__ == '__main__':
    main ()
Tried

In python3, the letter b is included? When the character b is removed with the replace function, the symbol'remains.
I think that the number'55 is the information sent from arudino via serial communication.

Supplemental information (FW/tool version etc.)

Please provide more detailed information here.

  • Answer # 1

    b'***'is a notation for REPL (interactive environment) and debugging that indicates that the data is bytes type.

    data = str (data) .replace ('"', '')

    is wrong andstr (data)does not convertdatatostrtype. See below for details on operation.

    4. Built-in types — Python 3.6.5 documentation #str

    Is the intended conversion obtained withdata.decode ()?

Related articles