我应该使用哪种数据结构来存储数据并从串行端口并发使用?

时间:2019-01-04 13:20:29

标签: python timer wxpython

我要从串行端口(对于Windows是'COM')获取数据,并想将其保存到csv文件中并同时绘制许多图形。我认为我应该为此使用数据结构,但不确定哪个结构适合我,如果可能的话,您可以提供一个示例吗?还有 buffer memoryview ,但不知道如何使用它。 我正在开发一个应用程序,这就是为什么我将wxpython与python 3一起使用的原因。为此,我只使用 wx.Timer ,而不是使用线程wxCallAfter或wxCallLater。但是,当我尝试使用列表中的数据结构时,在20-30秒后,应用程序冻结,他们并没有同时完成工作。任何帮助将不胜感激。

import wx
import time
import serial
import matplotlib.pyplot as plt
import matplotlib.pyplot
import numpy as np
import matplotlib.animation as animation



ser = serial.Serial('COM9', 9600)



class MyForm(wx.Frame):
    global ser,a,fig,ax,ax1,ax2
    ser = serial.Serial('COM9',9600)
    fig, ax = plt.subplots()
    ax = plt.subplot(221)      #sol ust GPS grafigi
    ax1 = plt.subplot(212)      #alt GPS-GLO grafigi
    ax2 = plt.subplot(222)          #sag ust GLO grafigi

    a=[]

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Timer Tutorial 2")

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)

        self.timer = wx.Timer(self, 1)
        self.Bind(wx.EVT_TIMER, self.update1, self.timer)
        self.timer2 = wx.Timer(self, 2)
        self.Bind(wx.EVT_TIMER, self.update2, self.timer2)
        self.timer3 = wx.Timer(self, 3)
        self.Bind(wx.EVT_TIMER, self.update3, self.timer3)
        self.timer4 = wx.Timer(self, 4)
        self.Bind(wx.EVT_TIMER, self.update4, self.timer4)

        self.toggleBtn = wx.Button(panel, wx.ID_ANY, "Start Timer 1")
        self.toggleBtn.Bind(wx.EVT_BUTTON, self.Timer111)
        self.toggleBtn2 = wx.Button(panel, wx.ID_ANY, "Start Timer 2")
        self.toggleBtn2.Bind(wx.EVT_BUTTON, self.Timer222)
        self.toggleBtn3 = wx.Button(panel, wx.ID_ANY, "Start Timer 3")
        self.toggleBtn3.Bind(wx.EVT_BUTTON, self.Timer333)
        self.toggleBtn4 = wx.Button(panel, wx.ID_ANY, "Start Timer 4")
        self.toggleBtn4.Bind(wx.EVT_BUTTON, self.Timer444)


        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.toggleBtn, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(self.toggleBtn2, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(self.toggleBtn3, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(self.toggleBtn4, 0, wx.ALL|wx.CENTER, 5)
        panel.SetSizer(sizer)        

    def Timer111(self, event):
        buttonObj = event.GetEventObject()
        btnLabel = buttonObj.GetLabel()
        timerNum = int(btnLabel[-1:])
        print (timerNum)
        if btnLabel == "Start Timer %s" % timerNum:
            print ("starting timer 1...")
            self.timer.Start(500)   
            buttonObj.SetLabel("Stop Timer %s" % timerNum)
        else:            
            self.timer.Stop()
            print ("timer 1 stopped!")      
            buttonObj.SetLabel("Start Timer %s" % timerNum)

    def Timer222(self, event):
        buttonObj = event.GetEventObject()
        btnLabel = buttonObj.GetLabel()
        timerNum = int(btnLabel[-1:])
        print (timerNum)
        if btnLabel == "Start Timer %s" % timerNum:       
            print ("starting timer 2...")
            self.timer2.Start(1000)
            buttonObj.SetLabel("Stop Timer %s" % timerNum)           
        else:
            self.timer2.Stop()
            plt.close()
            print ("timer 2 stopped!")
            buttonObj.SetLabel("Start Timer %s" % timerNum)



    def Timer333(self, event):
        buttonObj = event.GetEventObject()
        btnLabel = buttonObj.GetLabel()
        timerNum = int(btnLabel[-1:])
        print (timerNum)
        if btnLabel == "Start Timer %s" % timerNum:       
            print ("starting timer 3...")
            self.timer3.Start(2000)
            buttonObj.SetLabel("Stop Timer %s" % timerNum)           
        else:
            self.timer3.Stop()
            print ("timer 3 stopped!")
            buttonObj.SetLabel("Start Timer %s" % timerNum)

    def Timer444(self, event):
        buttonObj = event.GetEventObject()
        btnLabel = buttonObj.GetLabel()
        timerNum = int(btnLabel[-1:])
        print (timerNum)
        if btnLabel == "Start Timer %s" % timerNum:       
            print ("starting timer 3...")
            self.timer4.Start(3000)
            buttonObj.SetLabel("Stop Timer %s" % timerNum)           
        else:
            self.timer4.Stop()
            print ("timer 4 stopped!")
            buttonObj.SetLabel("Start Timer %s" % timerNum)


    def update1(self, event):
        global a
        timerId = event.GetId() 

        if timerId == self.timer.GetId():
            print("timer 1")                
            x=[]
            x1=[]
            x2=[]
            y=[]
            y1=[]
            y2=[]
            i=0           
            def altit(event):
                for line in ser:  
                    a.append(line)
                    #for item in a.__len__():      #orj: for line in ser           #a.__sizeof__()-1
                    #for k in range(1000)   
                    #for k in range(len(a)):
                    #print("item  :", item)
                    #print("line:                        ",a[k])
                    data=line.split(b",")
                    if data[0] == b"$GNGGA":
                        altitude=data[9]
                        timm=data[1]
                        tim=float(timm)
                        tim=tim+30000
                        #print(tim)

                        hour = tim//10000
                        minute = (tim//100)%100
                        second = tim%100
                        zaman = hour*3600 + minute*60 + second
                        #print(zaman)                        
                        altitude=float(altitude)
                        x.append(zaman)
                        y.append(altitude)                   
                        ax.plot(x,y)       

                plt.ylabel('meters')
                plt.xlabel('counts')
                plt.title('ALTITUDE')
            ani2 = animation.FuncAnimation(fig,altit)
            plt.show()
            print("hey")



    def update2(self, event):  
        global ser,a,fig,ax,ax1,ax2
        timerId = event.GetId()     
        if timerId == self.timer2.GetId():  
            print("timer 2")
            print(a)



    def update3(self, event):   
        timerId = event.GetId()     
        if timerId == self.timer3.GetId():
            print("timer 3")
            x=[]
            x1=[]
            x2=[]
            y=[]
            y1=[]
            y2=[]
            i=0           
            #fig = plt.figure()               
            def sV_sat(i):     
                for item in a:
                    data=item.split(b",")                    
                    if data[0] == b"$GPGSV":
                        print("c")
                        sView_GP = data[3]
                        sNumber_GP = data[4]
                        i=i+1
                        x.append(i)
                        #x=[i]                         
                        y.append(float(sView_GP))
                        ax.set_title("GPS")
                        ax1.set_title("GPS-GLO")
                        ax1.set_ylim(0,20)

                        ax.bar(i,y,color='green')        
                        ax1.plot(x,y,'g')     

            ani8 = animation.FuncAnimation(fig,sV_sat)
            plt.legend()
            #pyplot.show() 
            plt.show()

    def update4(self, event):
        timerId = event.GetId()     
        if timerId == self.timer4.GetId():
            print("timer 4")


        #print (time.ctime())

# Run the program
if __name__ == "__main__":

    app = wx.App()
    frame = MyForm().Show()
    app.MainLoop()

我从 def update1 读取并绘制数据,并使用 def update3 在屏幕上打印。

0 个答案:

没有答案