is there a problem on this simple code

John Machin sjmachin at lexicon.net
Sun Mar 13 06:28:43 EST 2005


jrlen balane wrote:
> 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.

(1) But it's NOT zero after the first time around! (2) How do you know
what it will accept? Guessing or reading the manual? If the checksum is
not zero, then what happens? Please quote the exact section from the
manual.

>
> anymore suggestion???...

YES: show the whole program; you have left out the initialisation part.

> ==================================================
> 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...

As Dennis has told you, searching is pointless. You are losing data
now. Before you weren't losing data. Until you fix that problem,
fiddling with anything else is pointless. You have stopped doing
sleep() -- why? You are now doing ser.open() and ser.flushInput() and
ser.close() on *each* time around the loop -- why? I'd strongly suggest
you put these back the way they were. THEN do what I told you to do:
use a separate tx_command and rx_command. The first time through the
loop, command is set to 70 from the received data, and then the second
time around, you SEND 70, not 67!!! Fix that, and then show us what you
get. DON'T try searching for a 70. Don't thrash around trying multiple
changes at once -- make changes one at a time so you can see the
effects.

Do you have a part number for the manual which describes the 67 and 70
"commands" and the check-sum? Is the manual on the internet anywhere?




More information about the Python-list mailing list