is there a problem on this simple code

jrlen balane nbbalane at gmail.com
Tue Mar 15 18:34:44 EST 2005


will this be correct???
what i want to happen is saved every received data (6 data bytes) to
an array for each one.

for k in range (rx_len-9):
     if byte[k] == 70 and byte [k+2] == 6 and sum(byte[k:k+10]) & 0xff == 0:
              #print byte[k:k+10]
        
               temp1.append(byte[k+3])
               temp2.append(byte[k+4])
               pyra1.append(byte[k+5])
               pyra2.append(byte[k+6])
               voltage.append(byte[k+7])
               current.append(byte[k+8])
                
               if time.sleep(300) == True:
                     temp1 = []
                     temp2 = []
                     pyra1 = []
                     pyra2 = []
                     voltage = []
                     current = []

and after x minutes of of receiving data, the arrays will be emptied
of its contents.

thanks again for the help. 
On 15 Mar 2005 02:13:40 -0800, John Machin <sjmachin at lexicon.net> wrote:
> 
> jrlen balane wrote:
> > did some editing:
> >
> 
> The error means that you received less than 19 bytes of data.
> 
> > rx_data = ser.read(19)
> !rx_len = len(rx_data)
> !print 'rx_len', rx_len
> > byte[0:18] = unpack('19B', rx_data)
> !# trash the above, do this
> !byte = [ord(x) for x in rx_data]
> !print 'received', byte
> !if rx_len < 10:
> !   print 'it is not pumping data out as fast as we assumed!'
> !   sys.exit(1)
> >
> !for k in range(rx_len - 9):
> >        if byte[k] == 70:
> >            if byte[k+2] == 6:
> >                if byte[k+9] ==
> >
> -(byte[k]+byte[k+1]+byte[k+2]+byte[k+3]+byte[k+4]+byte[k+5]+byte[k+6]+byte[k+7]+byte[k+8])
> > & 0xff:
> 
> Yuk!
> 
> (1) use 'and'
> (2) when you find yourself typing repetitive crap like that, your brain
> should be shrieking "There must be a better way!!"
> 
> if byte[k] == 70 \
> and byte[k+2] == 6 \
> and sum(byte[k:k+10]) & 0xff == 0:
> 
> >                    print byte[k:k+9] <<<<<<=== you probably mean 10,
> not nine
> > ====================================
> > heres the error:
> > Traceback (most recent call last):
> >   File "C:\Python23\practices\serialnewesttest2.py", line 28, in
> -toplevel-
> >     byte[0:18] = unpack('19B', rx_data)
> > error: unpack str size does not match format
> >
> > what i am doing here is creating an array from based on the unpacked
> data
> > then i am searching for the array member that is equal to "70" since
> > it is going to be my reference. once i find it, i'll based my
> received
> > data from that point. then if the succeding tests are confirmed, i
> can
> > get my data.
> >
> > please help....(again) :(
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list