is there a problem on this simple code

John Machin sjmachin at lexicon.net
Tue Mar 15 05:13:40 EST 2005


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) :(




More information about the Python-list mailing list