question on wxTimer (sorry for accidental sending )

i'm making a GUI on BOA 0.3.1 and wxPython 2.4.2.4
this is what the program do:
1. the user browses for a text file containing the
amount of delay needed to be sent serially:

    def OnBrowseTextFileButton(self, event):
         dlg = wxFileDialog(self, "Choose a file",
".",
           "", "*.txt", wxOPEN)
         try:
             if dlg.ShowModal() == wxID_OK:
                 filename = dlg.GetPath()
                 # Your code
                 self.BrowseFile.SetValue(filename)
         finally:
             dlg.Destroy()

2. the text file is read and the delays are put into
a list:
     def OnOKButton(self, event):
        
        if self.UserMode.GetValue() == True:
            data_file = open(os.path.normpath
(self.BrowseFile.GetValue()), 'r')
            data = data_file.readlines()

            self.irradianceStrings = map(str, data)
            self.irradianceIntegers = map(int, data)
            self.Irradiance_Execute.SetValue
(''.join(self.irradianceStrings))
        
        else:
            self.irradianceIntegers = [10000 for x in
range(96)] #'10000', 96 times
            self.Irradiance_Execute.SetValue
('10000')

3. the irradianceIntegers is then sent to
the external hardware through a serial port.
then after transmitting data, the GUI should then
retrieve data coming from the hardware, and it will do
this perpetually until the user stops the process or
the GUI has finished transmitting the contents of the
irradianceInteger[] list:

     def OnStartButton(self, event):

         self.ser.open()
         tx_command = 67
         tx_no_databyte = 2
         tx_message_no = 1
         tx_len = len (self.irradianceIntegers)
         
         for j in range (tx_len) :
            
             temp1 = []
             temp2 = []
             pyra1 = []
             pyra2 = []
             voltage = []
             current = []
            
             data_hi, data_lo =
divmod(self.irradianceIntegers[j], 0x100)
             tx_checksum = -(data_hi + data_lo +
tx_command + tx_message_no + tx_no_databyte) & 0xff
             self.ser.write(pack('6B', tx_command,
tx_message_no, tx_no_databyte, data_lo, data_hi,
tx_checksum))
   
             rx_data = self.ser.read(19)
             rx_len = len(rx_data)
             byte = [ord(x) for x in rx_data]
            
             if rx_len < 10:
                 sys.exit(1)
    
             for k in range (rx_len-9):
                 if byte[k] == 70 and byte [k+2] == 6
and sum(byte[k:k+10]) & 0xff == 0:
                    
                    temp11 = float(byte[k+3])* 5/255
                    temp22 = float(byte[k+4])* 5/255
                    pyra11 = float(byte[k+5])* 5/255
                    pyra22 = float(byte[k+6])* 5/255
                    voltage11 = float(byte[k+7])*
5/255
                    current11 = float(byte[k+8])*
5/255
                    
                    temp1.append(temp11)
                    temp2.append(temp22)
                    pyra1.append(pyra11)
                    pyra2.append(pyra22)
                    voltage.append(voltage11)
                    current.append(current11)
                    
                    filename = "%s%s.txt"
%(self.Save_Session.GetValue(),
time.strftime("%Y%m%d%H%M"))
                    table_file = open(filename,"a")
                    lendata = len(temp1)
                      
                    for ii in xrange(lendata):
                    
table_file.write('%f\t'%temp1[ii])
                    
table_file.write('%f\t'%temp2[ii])
                    
table_file.write('%f\t'%pyra1[ii])
                    
table_file.write('%f\t'%pyra2[ii])
                     table_file.write('%f\t'
%voltage[ii])
                     table_file.write('%f\t'
%current[ii])
                     table_file.write('\n')
                        
                    table_file.close()
        
         self.ser.close()

----the problem is how will i make the transmit part
do the event every five minutes while just keeping the
receive part perpetually running. The transmit and
receive part is running smoothly.

----will doing this solve the problem:

self.timer1 = wxTimer(evtHandler=self,
id=wxID_WXMDIPARENTFRAME1TIMER1)

EVT_TIMER(self, wxID_WXMDIPARENTFRAME1TIMER1,
self.OnTimer1Timer)

def OnTimer1Timer(self, event):
    #put serial transmit part of the code here #

def OnStartButton(self, event):
    self.timer1.Start(30000) #5 minutes
    #put serial receive part of the code here #

----thanks for the help you'll extend to me!

···

__________________________________
Do you Yahoo!?
Yahoo! Mail - Find what you need with new enhanced search.
http://info.mail.yahoo.com/mail_250

simply put, if i do this:

def OnTimer1Timer(self, event):
     Transmit() #put serial transmit part of the
code here #

def OnStartButton(self, event):
     self.timer1.Start(30000) #5 minutes
     Receive() #put serial receive part of the code
here #

will the receive part still be executed or the program
will just wait for the timer to stop first before
executing the Receive()

···

--- jan rienyer gadil <jrlen97_2@yahoo.com> wrote:

i'm making a GUI on BOA 0.3.1 and wxPython 2.4.2.4
this is what the program do:
1. the user browses for a text file containing the
amount of delay needed to be sent serially:

    def OnBrowseTextFileButton(self, event):
         dlg = wxFileDialog(self, "Choose a file",
".",
           "", "*.txt", wxOPEN)
         try:
             if dlg.ShowModal() == wxID_OK:
                 filename = dlg.GetPath()
                 # Your code
                 self.BrowseFile.SetValue(filename)
         finally:
             dlg.Destroy()

2. the text file is read and the delays are put
into
a list:
     def OnOKButton(self, event):
        
        if self.UserMode.GetValue() == True:
            data_file = open(os.path.normpath
(self.BrowseFile.GetValue()), 'r')
            data = data_file.readlines()

            self.irradianceStrings = map(str, data)
            self.irradianceIntegers = map(int, data)
            self.Irradiance_Execute.SetValue
(''.join(self.irradianceStrings))
        
        else:
            self.irradianceIntegers = [10000 for x
in
range(96)] #'10000', 96 times
            self.Irradiance_Execute.SetValue
('10000')

3. the irradianceIntegers is then sent to
the external hardware through a serial port.
then after transmitting data, the GUI should then
retrieve data coming from the hardware, and it will
do
this perpetually until the user stops the process or
the GUI has finished transmitting the contents of
the
irradianceInteger list:

     def OnStartButton(self, event):

         self.ser.open()
         tx_command = 67
         tx_no_databyte = 2
         tx_message_no = 1
         tx_len = len (self.irradianceIntegers)
         
         for j in range (tx_len) :
            
             temp1 =
             temp2 =
             pyra1 =
             pyra2 =
             voltage =
             current =
            
             data_hi, data_lo =
divmod(self.irradianceIntegers[j], 0x100)
             tx_checksum = -(data_hi + data_lo +
tx_command + tx_message_no + tx_no_databyte) & 0xff
             self.ser.write(pack('6B', tx_command,
tx_message_no, tx_no_databyte, data_lo, data_hi,
tx_checksum))
   
             rx_data = self.ser.read(19)
             rx_len = len(rx_data)
             byte = [ord(x) for x in rx_data]
            
             if rx_len < 10:
                 sys.exit(1)
    
             for k in range (rx_len-9):
                 if byte[k] == 70 and byte [k+2] ==
6
and sum(byte[k:k+10]) & 0xff == 0:
                    
                    temp11 = float(byte[k+3])* 5/255
                    temp22 = float(byte[k+4])* 5/255
                    pyra11 = float(byte[k+5])* 5/255
                    pyra22 = float(byte[k+6])* 5/255
                    voltage11 = float(byte[k+7])*
5/255
                    current11 = float(byte[k+8])*
5/255
                    
                    temp1.append(temp11)
                    temp2.append(temp22)
                    pyra1.append(pyra11)
                    pyra2.append(pyra22)
                    voltage.append(voltage11)
                    current.append(current11)
                    
                    filename = "%s%s.txt"
%(self.Save_Session.GetValue(),
time.strftime("%Y%m%d%H%M"))
                    table_file = open(filename,"a")
                    lendata = len(temp1)
                      
                    for ii in xrange(lendata):
                    
table_file.write('%f\t'%temp1[ii])
                    
table_file.write('%f\t'%temp2[ii])
                    
table_file.write('%f\t'%pyra1[ii])
                    
table_file.write('%f\t'%pyra2[ii])
                     table_file.write('%f\t'
%voltage[ii])
                     table_file.write('%f\t'
%current[ii])
                     table_file.write('\n')
                        
                    table_file.close()
        
         self.ser.close()

----the problem is how will i make the transmit part
do the event every five minutes while just keeping
the
receive part perpetually running. The transmit and
receive part is running smoothly.

----will doing this solve the problem:

self.timer1 = wxTimer(evtHandler=self,
id=wxID_WXMDIPARENTFRAME1TIMER1)

EVT_TIMER(self, wxID_WXMDIPARENTFRAME1TIMER1,
self.OnTimer1Timer)

def OnTimer1Timer(self, event):
    #put serial transmit part of the code here #

def OnStartButton(self, event):
    self.timer1.Start(30000) #5 minutes
    #put serial receive part of the code here #

----thanks for the help you'll extend to me!

__________________________________
Do you Yahoo!?
Yahoo! Mail - Find what you need with new enhanced
search.
http://info.mail.yahoo.com/mail_250

---------------------------------------------------------------------

To unsubscribe, e-mail:
wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail:
wxPython-users-help@lists.wxwidgets.org

__________________________________
Do you Yahoo!?
Yahoo! Mail - Find what you need with new enhanced search.
http://info.mail.yahoo.com/mail_250

This may be slightly off topic for you, but I had a similar issue with some industrial automation software I wrote.

I handled the asynchronous interaction with the GUI by using the Queue module and threads.

In this program I was only reading from the serial port.

I created a separate thread to read from the serial port, and then put any received data into the Queue.

In the GUI I used a timer to check for data every few seconds. (Remember to stop it before you close your window or it will explode on some versions of windows, IE 9X).

By using the a Queue object I managed to avoid race conditions with the GUI, and seriously simplified the design.

In your case, you could have two Queue objects, one to send data to the serial port, and another one for the data coming back. You don't need to send it as character data, you can send a Python object. Using a logical object would make your design more robust because you could change the backend later if you wanted to (device driver).

You could block on the Queue if it made sense in the serial port routine (thread).

From the GUI you would simply add items as needed to the outgoing Queue without waiting, and use a timer to check the incoming Queue (ie non blocking queue call)..

In a industrial automation program I wrote before I discovered the Queue module, I still used separate threads, but I sent events to the GUI, this caused me various race conditions. I really feel that using the Queue has saved me a lot of headaches. Particularly because it handles the mutex locking and so on for me.

I created a *device driver* to handle communication with the device (a scale), and the thread (using the device driver) handled all interaction, IE poking the scale for data, and then reading it in. Because we didn't run the scale in continuous broadcast mode, we would poke it for a data value, and then wait for the result. The data coming back from the scale was logical data rather than the raw data from the scale, that way the scale manufacturer or connection method could be changed without changing the rest of the program.

If you could design your serial interaction to send/wait/receive, you would probably really simplify your serial port interaction (if you used one thread to service the device, using two threads could cause problems in the serial port routines)..

The programs I wrote were not *open source*, but the owner is very pro Open Source, if someone wanted to see the design or code, I don't expect it would be a big problem.

Have Fun
Rick

jan rienyer gadil wrote:

···

simply put, if i do this:

def OnTimer1Timer(self, event):
    Transmit() #put serial transmit part of the
code here #

def OnStartButton(self, event):
    self.timer1.Start(30000) #5 minutes
    Receive() #put serial receive part of the code
here #

will the receive part still be executed or the program
will just wait for the timer to stop first before
executing the Receive()

--- jan rienyer gadil <jrlen97_2@yahoo.com> wrote:

i'm making a GUI on BOA 0.3.1 and wxPython 2.4.2.4
this is what the program do:
1. the user browses for a text file containing the
amount of delay needed to be sent serially:

   def OnBrowseTextFileButton(self, event):
        dlg = wxFileDialog(self, "Choose a file",
".",
          "", "*.txt", wxOPEN)
        try:
            if dlg.ShowModal() == wxID_OK:
                filename = dlg.GetPath()
                # Your code
                self.BrowseFile.SetValue(filename)
        finally:
            dlg.Destroy()

2. the text file is read and the delays are put
into
a list:
    def OnOKButton(self, event):
              if self.UserMode.GetValue() == True:
           data_file = open(os.path.normpath
(self.BrowseFile.GetValue()), 'r')
           data = data_file.readlines()

           self.irradianceStrings = map(str, data)
           self.irradianceIntegers = map(int, data)
           self.Irradiance_Execute.SetValue
(''.join(self.irradianceStrings))
              else: self.irradianceIntegers = [10000 for x
in
range(96)] #'10000', 96 times
           self.Irradiance_Execute.SetValue
('10000')

3. the irradianceIntegers is then sent to
the external hardware through a serial port. then after transmitting data, the GUI should then retrieve data coming from the hardware, and it will
do
this perpetually until the user stops the process or
the GUI has finished transmitting the contents of
the
irradianceInteger list:

    def OnStartButton(self, event):

        self.ser.open()
        tx_command = 67
        tx_no_databyte = 2
        tx_message_no = 1
        tx_len = len (self.irradianceIntegers)
                for j in range (tx_len) :
                       temp1 =
            temp2 =
            pyra1 =
            pyra2 =
            voltage =
            current =
                       data_hi, data_lo =
divmod(self.irradianceIntegers[j], 0x100)
            tx_checksum = -(data_hi + data_lo +
tx_command + tx_message_no + tx_no_databyte) & 0xff
            self.ser.write(pack('6B', tx_command,
tx_message_no, tx_no_databyte, data_lo, data_hi,
tx_checksum))
  
            rx_data = self.ser.read(19)
            rx_len = len(rx_data)
            byte = [ord(x) for x in rx_data]
                       if rx_len < 10:
                sys.exit(1)
               for k in range (rx_len-9):
                if byte[k] == 70 and byte [k+2] ==
6
and sum(byte[k:k+10]) & 0xff == 0:
                                      temp11 = float(byte[k+3])* 5/255
                   temp22 = float(byte[k+4])* 5/255
                   pyra11 = float(byte[k+5])* 5/255
                   pyra22 = float(byte[k+6])* 5/255
                   voltage11 = float(byte[k+7])*
5/255
                   current11 = float(byte[k+8])*
5/255
                                      temp1.append(temp11)
                   temp2.append(temp22)
                   pyra1.append(pyra11)
                   pyra2.append(pyra22)
                   voltage.append(voltage11)
                   current.append(current11)
                   
                        filename = "%s%s.txt"
%(self.Save_Session.GetValue(),
time.strftime("%Y%m%d%H%M")) table_file = open(filename,"a")
                   lendata = len(temp1)
                                        for ii in xrange(lendata):
                   table_file.write('%f\t'%temp1[ii])
                   table_file.write('%f\t'%temp2[ii])
                   table_file.write('%f\t'%pyra1[ii])
                   table_file.write('%f\t'%pyra2[ii]) table_file.write('%f\t'
%voltage[ii]) table_file.write('%f\t'
%current[ii]) table_file.write('\n')
                                          table_file.close()
               self.ser.close()

----the problem is how will i make the transmit part
do the event every five minutes while just keeping
the
receive part perpetually running. The transmit and
receive part is running smoothly.

----will doing this solve the problem:

self.timer1 = wxTimer(evtHandler=self, id=wxID_WXMDIPARENTFRAME1TIMER1)

EVT_TIMER(self, wxID_WXMDIPARENTFRAME1TIMER1,
self.OnTimer1Timer)

def OnTimer1Timer(self, event):
   #put serial transmit part of the code here #

def OnStartButton(self, event):
   self.timer1.Start(30000) #5 minutes
   #put serial receive part of the code here #

----thanks for the help you'll extend to me!

__________________________________ Do you Yahoo!? Yahoo! Mail - Find what you need with new enhanced
search. http://info.mail.yahoo.com/mail_250

---------------------------------------------------------------------

To unsubscribe, e-mail:
wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail:
wxPython-users-help@lists.wxwidgets.org

__________________________________ Do you Yahoo!? Yahoo! Mail - Find what you need with new enhanced search. http://info.mail.yahoo.com/mail_250

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

jan rienyer gadil wrote:

simply put, if i do this:

def OnTimer1Timer(self, event):
     Transmit() #put serial transmit part of the
code here #
def OnStartButton(self, event):
     self.timer1.Start(30000) #5 minutes
     Receive() #put serial receive part of the code
here #

will the receive part still be executed or the program
will just wait for the timer to stop first before
executing the Receive()

Receive will still be executed. The timer.Start function does not block execution, it just tells the system when to send the timer event.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!