Re: is there a problem on this simple code

From: jrlen balane (nbbalane_at_gmail.com)
Date: 03/16/05


Date: Wed, 16 Mar 2005 08:33:23 +0800
To: John Machin <sjmachin@lexicon.net>

ok heres the code, i'm trying on IDLE:

import sys
import serial
import sys, os
import serial
import string
import time
from struct import *

data_file = open('C:/Documents and Settings/nyer/Desktop/IRRADIANCE.txt', 'r')
data = data_file.readlines()

def process(list_of_lines):
    data_points = []
    for line in list_of_lines:
        data_points.append(int(line))
    return data_points

irradiance = process(data)

ser = serial.Serial()
ser.baudrate = 9600
ser.port = 0
ser

ser.open()
tx_command = 67
tx_no_databyte = 2
tx_message_no = 1
tx_len = len (irradiance)
        
for j in range (tx_len) :
    start_time = time.time()

    temp1 = []
    temp2 = []
    pyra1 = []
    pyra2 = []
    voltage = []
    current = []

    current_time = time.time()

    while( current_time >= start_time + 300):

        data_hi, data_lo = divmod(irradiance[j], 0x100)
        tx_checksum = -(data_hi + data_lo + tx_command + tx_message_no
+ tx_no_databyte) & 0xff
        ser.write(pack('6B', tx_command, tx_message_no,
tx_no_databyte, data_lo, data_hi, tx_checksum))
        
        rx_data = ser.read(19)
        rx_len = len(rx_data)
        byte = [ord(x) for x in rx_data]
            
        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 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])
                print temp1, temp2, pyra1, pyra2, voltage, current
                
        current_time = time.time()

while theres no error in the output, there is also no response from
the hardware or maybe communication is off.

could somebody out there help me.

by the way, here is a working code: though here the data to be
transmitted is just incrementing and not read from a text file:

import serial
import string
import time
from struct import *
import os

ser = serial.Serial()

ser.baudrate = 9600
ser.port = 0
ser.timeout = 1
ser

ser.open()
tx_command = 67
tx_message_no = 1
tx_no_databyte = 2
item=10000
for item in range(10000, 30001, 10):
            
    data_hi, data_lo = divmod(item, 0x100)
    tx_checksum = -(data_hi + data_lo + tx_command + tx_message_no +
tx_no_databyte) & 0xff
    ser.write(pack('6B', tx_command, tx_message_no, tx_no_databyte,
data_lo, data_hi, tx_checksum))
    #print tx_command, tx_message_no, tx_total_data, data_lo, data_hi,
tx_checksum
    

    rx_data = ser.read(19)
    rx_len = len(rx_data)
    #print 'rx_len', rx_len
    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 and byte [k+2] == 6 and sum(byte[k:k+10]) & 0xff == 0:
            print byte[k:k+10]
===================================
outputs:
[70, 2, 6, 54, 197, 253, 230, 231, 211, 26]
[70, 3, 6, 54, 197, 253, 230, 231, 211, 25]
[70, 3, 6, 54, 197, 253, 230, 231, 210, 26]
[70, 3, 6, 54, 197, 253, 230, 231, 210, 26]
[70, 3, 6, 54, 197, 253, 230, 231, 211, 25]
[70, 3, 6, 54, 197, 253, 230, 231, 210, 26]
[70, 3, 6, 54, 197, 253, 230, 231, 211, 25]
[70, 3, 6, 54, 197, 253, 230, 231, 210, 26]
[70, 3, 6, 54, 197, 253, 230, 231, 211, 25]
...
...

On Wed, 16 Mar 2005 07:34:44 +0800, jrlen balane <nbbalane@gmail.com> wrote:
> 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@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
> >
>



Relevant Pages

  • Re: pyserial ser.write(string) TypeError in OS X
    ... Rodrigo schrieb: ... In python 2.5 ser. ... ser.write('this is a string') i get the following error" ...
    (comp.lang.python)
  • pyserial ser.write(string) TypeError in OS X
    ... Maybe this is not a bug at all, ... In python 2.5 ser. ... ser.write('this is a string') i get the following error" ...
    (comp.lang.python)
  • Re: pyserial ser.write(string) TypeError in OS X
    ... In python 2.5 ser. ... ser.write('this is a string') i get the following error" ... Python 3.0 introduced many changes, one of them is to work with unicode by default. ...
    (comp.lang.python)
  • Probably simple problem with networking
    ... It should do only one thing - write received data to console and terminate itself when "exit" received. ... Problem is that if I send some string to this application, it'll receive that string and many ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Pyserial again
    ... Ok you write that it close:Because the "ser" object is never used ... previous com port: ... a = textCtrl1GetValue# i put here some different string ... why at any read the serial close, is possible to prevent the closure of ...
    (comp.lang.python)