is there a problem on this simple code

jrlen balane nbbalane at gmail.com
Sun Mar 13 01:56:55 EST 2005


the hardware is a school project that uses a microcontroller for "light dimming"
the message command "67" will tell the microcontroller (PIC16F877) to
do a command (to control the intensity of a lamp)
the message command "70" should tell the GUI that the microcontroller
has started transmitting.
the message sent by the GUI is different from the message sent by the
microcontroller
(but i guess sir John is right, i think i should use different
variable for the data transmitted and data received)
i am currently developing this part of the project which implements
the serial communication
i've fixed the command and total_data since i know beforehand that
this will be its value.
to simplify the program, i have also fixed the message_no since the
microcontroller will still accept the transmitted data as long as the
checksum == 0.

anymore suggestion???...
==================================================
command = 67
message_no = 1
total_data = 2
item=10000
for item in range(10000, 30001, 250):
    ser.open()
    data_hi, data_lo = divmod(item, 0x100)
    checksum = -(data_hi + data_lo + 0x46) & 0xff
    ser.write(pack('6B', command, message_no, total_data, data_lo,
data_hi, checksum))
    data = ser.read(10)
    (command, msg_no, no_databyte, temp1, temp2, pyra1, pyra2,
voltage, current, checksum) = unpack('10B', data)  #serial receive
protocol
    print command, msg_no, no_databyte, temp1, temp2, pyra1, pyra2,
voltage, current, checksum
    ser.flushInput()
    ser.close()

===================================
i've rewritten the code and deleted some unnecessary entries, now the
problem is :
21 6 64 64 192 0 0 0 175 70
70 2 6 64 64 192 0 0 0 114
70 11 6 64 64 192 0 0 0 105
0 0 104 70 2 6 64 64 192 0
70 2 6 64 64 192 0 0 0 114
128 128 103 70 2 6 64 64 192 0
70 2 6 64 64 192 0 0 0 114
16 208 246 70 2 6 64 64 192 0
70 2 6 64 64 192 0 0 0 114

===================================
the received data does not always start with the command "70", 
is this ok??? since i can always search first for the command "70"
before i read the remaining 9 bytes, then calculate first for the
checksum before finally accepting the received data.

am i making sense here?! please help me...



More information about the Python-list mailing list