Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6869273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:32:16+00:00 2026-05-27T03:32:16+00:00

I can capture data from serial device via pyserial, at this time I can

  • 0

I can capture data from serial device via pyserial, at this time I can only export data to text file, the text file has format like below, it’s have 3 columns

>21 21 0 
>
>41 41 0.5
>
>73 73 1
>    
....
>2053 2053 5
>
>2084 2084 5.5
>
>2125 2125 6

Now I want to use matplotlib to generate live graph has 2 figure (x,y) x,y are second and third column, first comlumn,’>’, and lines don’t have data can be remove

thank folks!

============================

Update : today, after follow these guides from

http://www.blendedtechnologies.com/realtime-plot-of-arduino-serial-data-using-python/231
http://eli.thegreenplace.net/2008/08/01/matplotlib-with-wxpython-guis
pyserial – How to read the last line sent from a serial device

now I can live plot with threading but eliben said that this Guis only plot single value each time, that lead to me the very big limitation, beacause my purpose is plotting 2 or 3 column, here is the code was modified from blendedtechnologies

Here is serial handler :
from threading import Thread

import time
import serial

last_received = ''
def receiving(ser):
    global last_received
    buffer = ''
    while True:
        buffer = buffer + ser.read(ser.inWaiting())
        if '\n' in buffer:
            lines = buffer.split('\n') # Guaranteed to have at least 2 entries
            last_received = lines[-2]
            #If the Arduino sends lots of empty lines, you'll lose the
            #last filled line, so you could make the above statement conditional
            #like so: if lines[-2]: last_received = lines[-2]
            buffer = lines[-1]


class SerialData(object):
    def __init__(self, init=50):
        try:
            self.ser = ser = serial.Serial(
                port='/dev/ttyS0',
                baudrate=9600,
                bytesize=serial.EIGHTBITS,
                parity=serial.PARITY_NONE,
                stopbits=serial.STOPBITS_ONE,
                timeout=0.1,
                xonxoff=0,
                rtscts=0,
                interCharTimeout=None
            )
        except serial.serialutil.SerialException:
            #no serial connection
            self.ser = None
        else:
            Thread(target=receiving, args=(self.ser,)).start()

    def next(self):
        if not self.ser:
            return 100  #return anything so we can test when Arduino isn't connected
                #return a float value or try a few times until we get one
        for i in range(40):
            raw_line = last_received[1:].split(' ').pop(0)
            try:
                return float(raw_line.strip())
            except ValueError:
                print 'bogus data',raw_line
                time.sleep(.005)
        return 0.
    def __del__(self):
        if self.ser:
            self.ser.close()

if __name__=='__main__':
    s = SerialData()
    for i in range(500):
        time.sleep(.015)
        print s.next()

For me I modified this segment so it can grab my 1st column data

for i in range(40):
                raw_line = last_received[1:].split(' ').pop(0)
                try:
                    return float(raw_line.strip())
                except ValueError:
                    print 'bogus data',raw_line
                    time.sleep(.005)
            return 0.

and generate graph base on these function on the GUI file

from Arduino_Monitor import SerialData as DataGen
def __init__(self):
        wx.Frame.__init__(self, None, -1, self.title)

        self.datagen = DataGen()
        self.data = [self.datagen.next()]

…………………………………………

def init_plot(self):
        self.dpi = 100
        self.fig = Figure((3.0, 3.0), dpi=self.dpi)

        self.axes = self.fig.add_subplot(111)
        self.axes.set_axis_bgcolor('black')
        self.axes.set_title('Arduino Serial Data', size=12)

        pylab.setp(self.axes.get_xticklabels(), fontsize=8)
        pylab.setp(self.axes.get_yticklabels(), fontsize=8)

        # plot the data as a line series, and save the reference 
        # to the plotted line series
        #
        self.plot_data = self.axes.plot(
            self.data, 
            linewidth=1,
            color=(1, 1, 0),
            )[0]

So my next question is how to realtime grab at least 2 column and passing 2 columns’data to the GUIs that it can generate graph with 2 axis.

self.plot_data.set_xdata(np.arange(len(self.data)))  #my 3rd column data
self.plot_data.set_ydata(np.array(self.data))        #my 2nd column data
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:32:17+00:00Added an answer on May 27, 2026 at 3:32 am

    Well, this reads your string and converts the numbers to floats. I assume you’ll be able to adapt this as needed.

    import numpy as np
    import pylab as plt
    
    str = '''>21 21 0 
    >
    >41 41 0.5
    >
    >73 73 1
    >
    >2053 2053 5
    >
    >2084 2084 5.5
    >
    >2125 2125 6'''
    nums = np.array([[float(n) for n in sub[1:].split(' ') if len(n)>0] for sub in str.splitlines() if len(sub)>1])
    
    fig = plt.figure(0)
    ax = plt.subplot(2,1,1)
    ax.plot(nums[:,0], nums[:,1], 'k.')
    ax = plt.subplot(2,1,2)
    ax.plot(nums[:,0], nums[:,2], 'r+')
    plt.show()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically just looking to see if you can capture an image from the webcam
I would like to make a php script that can capture a page from
I'm trying to figure out how to capture data from a form using EditText
I'm migrating data from a legacy database that has many tables with primary keys
I'd like to capture submit action from Ajax.BeginForm (...) and asynchronously fetch some data
For an application we capture certain form data. The user can include the various
I capture data with tshark and save certain data from the packet header to
The project I'm working on is to handle data capture from scan guns (Pocket
I'm trying to extract a set of data from some (large) text files. Basically,
I am supposed to capture data from a form and send the data to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.