is there a problem on this simple code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jrlen balane

    #16
    Re: is there a problem on this simple code

    the assembly program for the microcontroller is created by a
    classmate. he based the protocol for the serial program from a
    protocol he found in the internet. unfortunately, i can't find the
    fpdf file, guess i'll just ask him later when he comes back.

    On 13 Mar 2005 03:28:43 -0800, John Machin <sjmachin@lexic on.net> wrote:[color=blue]
    >
    > jrlen balane wrote:[color=green]
    > > the hardware is a school project that uses a microcontroller for[/color]
    > "light dimming"[color=green]
    > > 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.[/color]
    >
    > (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.
    >[color=green]
    > >
    > > anymore suggestion???.. .[/color]
    >
    > YES: show the whole program; you have left out the initialisation part.
    >[color=green]
    > > =============== =============== =============== =====
    > > 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...[/color]
    >
    > 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?
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >[/color]

    Comment

    • Peter Hansen

      #17
      Re: is there a problem on this simple code

      Bengt Richter wrote:[color=blue]
      > Sorry for jumping in with a largely irrelevant comment. I didn't look
      > at the code, just sought to illustrate the 6/18 thing further, in a kneejerk reaction.
      > Though BTW FWIW the visual sequence of glyphs representing the data was more a str output
      > than repr, I guess:
      >[color=green][color=darkred]
      > >>> repr("C\x01\x02 \x10'\x83")[/color][/color]
      > '"C\\x01\\x02\\ x10\'\\x83"'[color=green][color=darkred]
      > >>> str("C\x01\x02\ x10'\x83")[/color][/color]
      > "C\x01\x02\x10' \x83"[/color]

      Actually, both of those have an additional repr() call
      courtesy of the Python interactive console. The output
      of str() on that string is unprintable, but the above
      representation has already been repr()ed by Python for
      consumption by fragile hyoo-mans...

      -Peter

      Comment

      • Peter Hansen

        #18
        Re: is there a problem on this simple code

        Dennis Lee Bieber wrote:[color=blue][color=green]
        >>rx_data1=0
        >>while (rx_data1 != 0x46):
        >> rx_data1 = ser.read(1)
        >> (rx_command) = unpack('1B', rx_data1)[/color]
        >
        > Isn't this unpack rather redundant -- assuming ser.read(1) only
        > reads one byte, then rx_data1 and rx_command would be identical.[/color]

        Brain fart... unpack converts the raw bytes to integer
        values with the "B" format character. You're thinking
        of what would happen with a "1c" format.

        -Peter

        Comment

        • Jan Rienyer Gadil

          #19
          Re: is there a problem on this simple code

          @ sir Peter
          so you mean that it is correct (at least on the unpack() part)

          when i run this program on IDLE , Python 2.3 (enthought edition),
          nothing is outputted on the shell, until i decide to close the shell,
          wherein it tells me if i would like to kill a process...

          import serial
          import string
          import time
          from struct import *

          ser = serial.Serial()

          ser.baudrate = 9600
          ser.port = 0
          ser
          ser.close()
          ser.open()

          command = 67
          message_no = 1
          total_data = 2

          item = 10000

          for item in range(10000, 30001, 250):
          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))

          rx_data1=0
          while (rx_data1 != 0x46):
          rx_data1 = ser.read(1)
          (rx_command) = unpack('1B', rx_data1)

          rx_data2=ser.re ad(9)
          (rx_msg_no, rx_no_databyte, temp1, temp2, pyra1, pyra2, voltage,
          current, rx_checksum) = unpack('9B', data)
          print rx_command, rx_msg_no, rx_no_databyte, temp1, temp2, pyra1,
          pyra2, voltage, current, rx_checksum

          ser.close()

          Comment

          • Bengt Richter

            #20
            Re: is there a problem on this simple code

            On Sun, 13 Mar 2005 10:46:52 -0500, Peter Hansen <peter@engcorp. com> wrote:
            [color=blue]
            >Bengt Richter wrote:[color=green]
            >> Sorry for jumping in with a largely irrelevant comment. I didn't look
            >> at the code, just sought to illustrate the 6/18 thing further, in a kneejerk reaction.
            >> Though BTW FWIW the visual sequence of glyphs representing the data was more a str output
            >> than repr, I guess:
            >>[color=darkred]
            >> >>> repr("C\x01\x02 \x10'\x83")[/color]
            >> '"C\\x01\\x02\\ x10\'\\x83"'[color=darkred]
            >> >>> str("C\x01\x02\ x10'\x83")[/color]
            >> "C\x01\x02\x10' \x83"[/color]
            >
            >Actually, both of those have an additional repr() call
            >courtesy of the Python interactive console. The output
            >of str() on that string is unprintable, but the above
            >representati on has already been repr()ed by Python for
            >consumption by fragile hyoo-mans...[/color]
            Ok,
            [color=blue][color=green][color=darkred]
            >>> print repr("C\x01\x02 \x10'\x83")[/color][/color][/color]
            "C\x01\x02\x10' \x83"

            But note that _no_ str-type string is printable at all until you assume
            that it is an encoding identifying a glyph sequence and you (re)encode/interpret
            for a device (virtual or not) that has a font and can present the font
            information visually to your eyes (or via some modulation of sensible
            environment for other senses) ;-)

            Regards,
            Bengt Richter

            Comment

            • jrlen balane

              #21
              Re: is there a problem on this simple code

              why is it that here:

              1)rx_data = ser.read(10)
              (rx_command, rx_msg_no, rx_no_databyte, temp1, temp2, pyra1,
              pyra2, voltage, current, rx_checksum) = unpack('10B', rx_data)
              print rx_command, rx_msg_no, rx_no_databyte, temp1, temp2, pyra1,
              pyra2, voltage, current, rx_checksum
              [color=blue][color=green][color=darkred]
              >>> type (rx_command)[/color][/color][/color]
              <type 'int'>

              but here:

              2)rx_data_comma nd = ser.read()
              (rx_command) = unpack('1B', rx_data_command )
              [color=blue][color=green][color=darkred]
              >>> type (rx_command)[/color][/color][/color]
              <type 'tuple'>

              how can i make rx_command of type 'int' if i am to use 2)?

              @sir John
              the reason why my first post all starts with '70' , which is what i
              really wanted to happen, is that it is buffered. the microcontroller
              sends data at a very fast rate, that the program just retrieve data
              from the buffer. so basically, they are not "real time". but there are
              also cases where the command is in other position. the good thing is,
              it remains in that position throughout...

              i want to make the program show data in "real time" so everytime i am
              able to read data, i immediately use flushInput(), to erase data from
              the buffer.

              so i what i want to do now is to search for the rx_command first
              ('70') just so i know where my data should start.

              the checksum will be validated, but first, i must know where my checksum is!
              if (rx_checksum != -(temp1 + temp2 + pyra1 + pyra2 + voltage + current
              + rx_command + rx_message_no + rx_no_databyte) & 0xff):
              #then discard message, loop again to search for rx_command

              plase help...

              On 13 Mar 2005 13:06:16 -0800, John Machin <sjmachin@lexic on.net> wrote:[color=blue]
              >
              > Jan Rienyer Gadil wrote:[color=green]
              > > @ sir Peter
              > > so you mean that it is correct (at least on the unpack() part)[/color]
              >
              > No he doesn't mean that at all. All it means is that minor scuffles
              > have broken out among the spectators. Don't worry about them, batons &
              > water cannon will fix them; you concentrate on the football match :-)
              >[color=green]
              > >
              > > when i run this program on IDLE , Python 2.3 (enthought edition),
              > > nothing is outputted on the shell, until i decide to close the shell,
              > > wherein it tells me if i would like to kill a process...[/color]
              >
              > So you did some elementary debugging, like putting in some print
              > statements at various places, as shown below, and what happened?
              >[color=green]
              > >
              > > import serial
              > > import string[/color]
              >
              > Redundant.
              >[color=green]
              > > import time
              > > from struct import *
              > >
              > > ser = serial.Serial()
              > >
              > > ser.baudrate = 9600
              > > ser.port = 0
              > > ser[/color]
              >
              > What is the above line meant to do? It actually does nothing.
              >[color=green]
              > > ser.close()
              > > ser.open()
              > >
              > > command = 67
              > > message_no = 1
              > > total_data = 2
              > >
              > > item = 10000[/color]
              >
              > Redundant.[color=green]
              > >[/color]
              >
              > print "DEBUG: before outer loop"
              >
              >[color=green]
              > > for item in range(10000, 30001, 250):[/color]
              >
              > print "DEBUG: inside outer loop; item =", repr(item)
              >[color=green]
              > > data_hi, data_lo = divmod(item, 0x100)
              > > checksum = -(data_hi + data_lo + 0x46) & 0xff[/color]
              >
              > You obviouly haven't taken the advice to generalise your checksum
              > calculation.
              >[color=green]
              > > ser.write(pack( '6B', command, message_no, total_data, data_lo,
              > > data_hi, checksum))
              > >
              > > rx_data1=0[/color]
              >
              > print "DEBUG: before inner loop"
              >[color=green]
              > > while (rx_data1 != 0x46):
              > > rx_data1 = ser.read(1)
              > > (rx_command) = unpack('1B', rx_data1)[/color]
              >
              > print "DEBUG: inside inner loop; rx_data1 =", repr(rx_data1), ";
              > rx_command =", repr(rx_command )
              >
              > And if you had have done that, you would/should have realised that you
              > have a:
              > !! CODING BUG !!
              > ser.read(1) will return a STRING, so even if you get the byte you are
              > looking for, rx_data1 will refer to 'F' == chr(70) == chr(0x46) ==
              > '\x46' none of which are == 0x46, and you will loop forever (if the
              > hardware is continuously outputting data) or hang waiting for data from
              > the hardware.
              >
              > !! DESIGN BUG !!
              > HOWEVER, as Dennis and I have been trying to tell you, it is WRONG to
              > be looping trying to sync on the first character in the packet. You
              > need to fix your data loss problem, not try to kludge your way around
              > it. I'll say it again, but only once: go back to the code of your
              > original posting. That code was not losing data. Among whatever else is
              > needed to revert to the first-shown code, put back the sleep() between
              > iterations.
              > As advised earlier, make sure that you use separate rx_command and
              > tx_command so that you don't accidentally start sending 70 as the
              > command.
              > Then show us what happened.
              >[color=green]
              > > rx_data2=ser.re ad(9)
              > > (rx_msg_no, rx_no_databyte, temp1, temp2, pyra1, pyra2, voltage,
              > > current, rx_checksum) = unpack('9B', data)[/color]
              >
              > !! CODING BUG !!
              > You read into "rx_data2" but unpack from "data". The result, when you
              > reach it after fixing the earlier bug, will be either an exception or
              > an utter nonsense, depending on what "data" is bound to at the time (if
              > anything).
              >[color=green]
              > > print rx_command, rx_msg_no, rx_no_databyte, temp1, temp2,[/color]
              > pyra1,[color=green]
              > > pyra2, voltage, current, rx_checksum
              > >[/color]
              >
              > You obviously haven't taken the advice from Dennis and myself to
              > validate the packet you receive -- (1) checksum is OK (2)
              > rx_no_databyte == 6
              >[color=green]
              > > ser.close()[/color]
              >
              > HTH,
              > John
              >
              > --
              > http://mail.python.org/mailman/listinfo/python-list
              >[/color]

              Comment

              • John Machin

                #22
                Re: is there a problem on this simple code


                jrlen balane wrote:[color=blue]
                > why is it that here:
                >
                > 1)rx_data = ser.read(10)
                > (rx_command, rx_msg_no, rx_no_databyte, temp1, temp2, pyra1,
                > pyra2, voltage, current, rx_checksum) = unpack('10B', rx_data)
                > print rx_command, rx_msg_no, rx_no_databyte, temp1, temp2, pyra1,
                > pyra2, voltage, current, rx_checksum
                >[color=green][color=darkred]
                > >>> type (rx_command)[/color][/color]
                > <type 'int'>
                >
                > but here:
                >
                > 2)rx_data_comma nd = ser.read()[/color]

                Are you sure you have really have read() -- which will read all
                available data -- or read(1) -- which will read just one byte???
                [color=blue]
                > (rx_command) = unpack('1B', rx_data_command )
                >[color=green][color=darkred]
                > >>> type (rx_command)[/color][/color]
                > <type 'tuple'>
                >
                > how can i make rx_command of type 'int' if i am to use 2)?[/color]

                unpack returns a tuple. In 1) you unpack it. In 2) you don't unpack it.

                either do this:

                (rx_command,) = unpack('1B', rx_data_command ) # unpack tuple

                or this:

                rx_command = unpack('1B', rx_data_command )[0] # grab 1st element of
                tuple

                or, simply, to get what you want, just do this:

                rx_command = ord(ser.read(1) )
                [color=blue]
                >
                > @sir John
                > the reason why my first post all starts with '70' , which is what i
                > really wanted to happen, is that it is buffered. the microcontroller
                > sends data at a very fast rate, that the program just retrieve data
                > from the buffer. so basically, they are not "real time". but there[/color]
                are[color=blue]
                > also cases where the command is in other position. the good thing is,
                > it remains in that position throughout...[/color]

                It wasn't doing that before; In the second posting with examples, there
                were some that were a MIXTURE of (a) 70 as the first byte of 10 (b) 70
                as the fourth byte of 10 (128 128 103 70 2 6 64 64 192 0)
                [color=blue]
                >
                > i want to make the program show data in "real time" so everytime i am
                > able to read data, i immediately use flushInput(), to erase data from
                > the buffer.[/color]

                How large is the buffer?
                [color=blue]
                >
                > so i what i want to do now is to search for the rx_command first
                > ('70') just so i know where my data should start.[/color]

                OK, so NOW you are saying that the microcontroller is pumping out data
                continuously, it's not one response to each of your "67" commands???

                In that case what I'd suggest you do is to read 19 bytes from the
                serial port (with time-out, I'd suggest; give up if you don't get 19
                bytes returned), and for each k in range(9), test for:

                (a) byte[k] == 70
                (b) byte[k+2] == 6
                (c) checksum(byte[k:k+10]) == 0

                If you don't get a valid data packet, give up.

                This should snatch one valid data packet (if there are any) from the
                torrent.

                Comment

                • jrlen balane

                  #23
                  Re: is there a problem on this simple code

                  @sir John
                  could you please show me how to do this exactly? it's in the "tip of
                  my toungue" but i just can get it, please...


                  On 14 Mar 2005 14:06:15 -0800, John Machin <sjmachin@lexic on.net> wrote:[color=blue]
                  >
                  > jrlen balane wrote:[color=green]
                  > > why is it that here:
                  > >
                  > > 1)rx_data = ser.read(10)
                  > > (rx_command, rx_msg_no, rx_no_databyte, temp1, temp2, pyra1,
                  > > pyra2, voltage, current, rx_checksum) = unpack('10B', rx_data)
                  > > print rx_command, rx_msg_no, rx_no_databyte, temp1, temp2, pyra1,
                  > > pyra2, voltage, current, rx_checksum
                  > >[color=darkred]
                  > > >>> type (rx_command)[/color]
                  > > <type 'int'>
                  > >
                  > > but here:
                  > >
                  > > 2)rx_data_comma nd = ser.read()[/color]
                  >
                  > Are you sure you have really have read() -- which will read all
                  > available data -- or read(1) -- which will read just one byte???
                  >[color=green]
                  > > (rx_command) = unpack('1B', rx_data_command )
                  > >[color=darkred]
                  > > >>> type (rx_command)[/color]
                  > > <type 'tuple'>
                  > >
                  > > how can i make rx_command of type 'int' if i am to use 2)?[/color]
                  >
                  > unpack returns a tuple. In 1) you unpack it. In 2) you don't unpack it.
                  >
                  > either do this:
                  >
                  > (rx_command,) = unpack('1B', rx_data_command ) # unpack tuple
                  >
                  > or this:
                  >
                  > rx_command = unpack('1B', rx_data_command )[0] # grab 1st element of
                  > tuple
                  >
                  > or, simply, to get what you want, just do this:
                  >
                  > rx_command = ord(ser.read(1) )
                  >[color=green]
                  > >
                  > > @sir John
                  > > the reason why my first post all starts with '70' , which is what i
                  > > really wanted to happen, is that it is buffered. the microcontroller
                  > > sends data at a very fast rate, that the program just retrieve data
                  > > from the buffer. so basically, they are not "real time". but there[/color]
                  > are[color=green]
                  > > also cases where the command is in other position. the good thing is,
                  > > it remains in that position throughout...[/color]
                  >
                  > It wasn't doing that before; In the second posting with examples, there
                  > were some that were a MIXTURE of (a) 70 as the first byte of 10 (b) 70
                  > as the fourth byte of 10 (128 128 103 70 2 6 64 64 192 0)
                  >[color=green]
                  > >
                  > > i want to make the program show data in "real time" so everytime i am
                  > > able to read data, i immediately use flushInput(), to erase data from
                  > > the buffer.[/color]
                  >
                  > How large is the buffer?
                  >[color=green]
                  > >
                  > > so i what i want to do now is to search for the rx_command first
                  > > ('70') just so i know where my data should start.[/color]
                  >
                  > OK, so NOW you are saying that the microcontroller is pumping out data
                  > continuously, it's not one response to each of your "67" commands???
                  >
                  > In that case what I'd suggest you do is to read 19 bytes from the
                  > serial port (with time-out, I'd suggest; give up if you don't get 19
                  > bytes returned), and for each k in range(9), test for:
                  >
                  > (a) byte[k] == 70
                  > (b) byte[k+2] == 6
                  > (c) checksum(byte[k:k+10]) == 0
                  >
                  > If you don't get a valid data packet, give up.
                  >
                  > This should snatch one valid data packet (if there are any) from the
                  > torrent.
                  >
                  > --
                  > http://mail.python.org/mailman/listinfo/python-list
                  >[/color]

                  Comment

                  • John Machin

                    #24
                    Re: is there a problem on this simple code


                    jrlen balane wrote:[color=blue]
                    > @sir John
                    > could you please show me how to do this exactly? it's in the "tip of
                    > my toungue" but i just can get it, please...
                    >[/color]

                    You've had far too much help already for a school project. Asking for
                    someone to write the code for you is "over the fence".

                    Comment

                    • Dennis Lee Bieber

                      #25
                      Re: is there a problem on this simple code

                      On 14 Mar 2005 17:04:13 -0800, "John Machin" <sjmachin@lexic on.net>
                      declaimed the following in comp.lang.pytho n:
                      [color=blue]
                      >
                      > jrlen balane wrote:[color=green]
                      > > @sir John
                      > > could you please show me how to do this exactly? it's in the "tip of
                      > > my toungue" but i just can get it, please...
                      > >[/color]
                      >
                      > You've had far too much help already for a school project. Asking for
                      > someone to write the code for you is "over the fence".[/color]

                      The repetition /is/ getting tedious, isn't it...

                      At the least, getting a formal definition of the protocol the
                      microcontroller uses would be nice. If it is continuously sending "70"
                      status reports, WITHOUT handshaking (RTS/CTS), then I'd strongly
                      recommend making the reader code a separate thread -- hopefully one
                      won't lose bytes during other processing. When a valid status report is
                      found, put it onto a Queue, which the main code can read when ever it
                      finds time.

                      Heck, at this point in time... I'd remove everything involved
                      with /sending/ commands. Start with coding something that only reads the
                      status from the microcontroller (since it sounds like the controller is
                      always sending). When the reader works correctly, then start trying to
                      add the command writer.

                      --[color=blue]
                      > =============== =============== =============== =============== == <
                      > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
                      > wulfraed@dm.net | Bestiaria Support Staff <
                      > =============== =============== =============== =============== == <
                      > Home Page: <http://www.dm.net/~wulfraed/> <
                      > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

                      Comment

                      • jrlen balane

                        #26
                        Re: is there a problem on this simple code

                        rx_data = ser.read(19)
                        byte[] = unpack('19B', rx_data)

                        for k in range(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
                        print byte[k:k+9]
                        =============== =============== ======
                        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) :(


                        On Tue, 15 Mar 2005 08:05:04 GMT, Dennis Lee Bieber
                        <wlfraed@ix.net com.com> wrote:[color=blue]
                        > On 14 Mar 2005 17:04:13 -0800, "John Machin" <sjmachin@lexic on.net>
                        > declaimed the following in comp.lang.pytho n:
                        >[color=green]
                        > >
                        > > jrlen balane wrote:[color=darkred]
                        > > > @sir John
                        > > > could you please show me how to do this exactly? it's in the "tip of
                        > > > my toungue" but i just can get it, please...
                        > > >[/color]
                        > >
                        > > You've had far too much help already for a school project. Asking for
                        > > someone to write the code for you is "over the fence".[/color]
                        >
                        > The repetition /is/ getting tedious, isn't it...
                        >
                        > At the least, getting a formal definition of the protocol the
                        > microcontroller uses would be nice. If it is continuously sending "70"
                        > status reports, WITHOUT handshaking (RTS/CTS), then I'd strongly
                        > recommend making the reader code a separate thread -- hopefully one
                        > won't lose bytes during other processing. When a valid status report is
                        > found, put it onto a Queue, which the main code can read when ever it
                        > finds time.
                        >
                        > Heck, at this point in time... I'd remove everything involved
                        > with /sending/ commands. Start with coding something that only reads the
                        > status from the microcontroller (since it sounds like the controller is
                        > always sending). When the reader works correctly, then start trying to
                        > add the command writer.
                        >
                        > --[color=green]
                        > > =============== =============== =============== =============== == <
                        > > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
                        > > wulfraed@dm.net | Bestiaria Support Staff <
                        > > =============== =============== =============== =============== == <
                        > > Home Page: <http://www.dm.net/~wulfraed/> <
                        > > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]
                        > --
                        > http://mail.python.org/mailman/listinfo/python-list
                        >[/color]

                        Comment

                        • jrlen balane

                          #27
                          Re: is there a problem on this simple code

                          did some editing:

                          rx_data = ser.read(19)
                          byte[0:18] = unpack('19B', rx_data)

                          for k in range(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:
                          print byte[k:k+9]
                          =============== =============== ======
                          heres the error:
                          Traceback (most recent call last):
                          File "C:\Python23\pr actices\serialn ewesttest2.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) :(

                          Comment

                          • John Machin

                            #28
                            Re: is there a problem on this simple code


                            jrlen balane wrote:[color=blue]
                            > did some editing:
                            >[/color]

                            The error means that you received less than 19 bytes of data.
                            [color=blue]
                            > rx_data = ser.read(19)[/color]
                            !rx_len = len(rx_data)
                            !print 'rx_len', rx_len[color=blue]
                            > byte[0:18] = unpack('19B', rx_data)[/color]
                            !# 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)[color=blue]
                            >[/color]
                            !for k in range(rx_len - 9):[color=blue]
                            > if byte[k] == 70:
                            > if byte[k+2] == 6:
                            > if byte[k+9] ==
                            >[/color]
                            -(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])[color=blue]
                            > & 0xff:[/color]

                            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:


                            [color=blue]
                            > print byte[k:k+9] <<<<<<=== you probably mean 10,[/color]
                            not nine[color=blue]
                            > =============== =============== ======
                            > heres the error:
                            > Traceback (most recent call last):
                            > File "C:\Python23\pr actices\serialn ewesttest2.py", line 28, in[/color]
                            -toplevel-[color=blue]
                            > 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[/color]
                            data[color=blue]
                            > 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[/color]
                            received[color=blue]
                            > data from that point. then if the succeding tests are confirmed, i[/color]
                            can[color=blue]
                            > get my data.
                            >
                            > please help....(again) :([/color]

                            Comment

                            • jrlen balane

                              #29
                              Re: is there a problem on this simple code

                              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(by te[k+3])
                              temp2.append(by te[k+4])
                              pyra1.append(by te[k+5])
                              pyra2.append(by te[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@lexic on.net> wrote:[color=blue]
                              >
                              > jrlen balane wrote:[color=green]
                              > > did some editing:
                              > >[/color]
                              >
                              > The error means that you received less than 19 bytes of data.
                              >[color=green]
                              > > rx_data = ser.read(19)[/color]
                              > !rx_len = len(rx_data)
                              > !print 'rx_len', rx_len[color=green]
                              > > byte[0:18] = unpack('19B', rx_data)[/color]
                              > !# 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)[color=green]
                              > >[/color]
                              > !for k in range(rx_len - 9):[color=green]
                              > > if byte[k] == 70:
                              > > if byte[k+2] == 6:
                              > > if byte[k+9] ==
                              > >[/color]
                              > -(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])[color=green]
                              > > & 0xff:[/color]
                              >
                              > 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:
                              >[color=green]
                              > > print byte[k:k+9] <<<<<<=== you probably mean 10,[/color]
                              > not nine[color=green]
                              > > =============== =============== ======
                              > > heres the error:
                              > > Traceback (most recent call last):
                              > > File "C:\Python23\pr actices\serialn ewesttest2.py", line 28, in[/color]
                              > -toplevel-[color=green]
                              > > 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[/color]
                              > data[color=green]
                              > > 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[/color]
                              > received[color=green]
                              > > data from that point. then if the succeding tests are confirmed, i[/color]
                              > can[color=green]
                              > > get my data.
                              > >
                              > > please help....(again) :([/color]
                              >
                              > --
                              > http://mail.python.org/mailman/listinfo/python-list
                              >[/color]

                              Comment

                              • jrlen balane

                                #30
                                Re: is there a problem on this simple code

                                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.readl ines()

                                def process(list_of _lines):
                                data_points = []
                                for line in list_of_lines:
                                data_points.app end(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(irradian ce[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(by te[k+3])
                                temp2.append(by te[k+4])
                                pyra1.append(by te[k+5])
                                pyra2.append(by te[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:[color=blue]
                                > 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(by te[k+3])
                                > temp2.append(by te[k+4])
                                > pyra1.append(by te[k+5])
                                > pyra2.append(by te[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@lexic on.net> wrote:[color=green]
                                > >
                                > > jrlen balane wrote:[color=darkred]
                                > > > did some editing:
                                > > >[/color]
                                > >
                                > > The error means that you received less than 19 bytes of data.
                                > >[color=darkred]
                                > > > rx_data = ser.read(19)[/color]
                                > > !rx_len = len(rx_data)
                                > > !print 'rx_len', rx_len[color=darkred]
                                > > > byte[0:18] = unpack('19B', rx_data)[/color]
                                > > !# 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)[color=darkred]
                                > > >[/color]
                                > > !for k in range(rx_len - 9):[color=darkred]
                                > > > if byte[k] == 70:
                                > > > if byte[k+2] == 6:
                                > > > if byte[k+9] ==
                                > > >[/color]
                                > > -(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])[color=darkred]
                                > > > & 0xff:[/color]
                                > >
                                > > 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:
                                > >[color=darkred]
                                > > > print byte[k:k+9] <<<<<<=== you probably mean 10,[/color]
                                > > not nine[color=darkred]
                                > > > =============== =============== ======
                                > > > heres the error:
                                > > > Traceback (most recent call last):
                                > > > File "C:\Python23\pr actices\serialn ewesttest2.py", line 28, in[/color]
                                > > -toplevel-[color=darkred]
                                > > > 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[/color]
                                > > data[color=darkred]
                                > > > 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[/color]
                                > > received[color=darkred]
                                > > > data from that point. then if the succeding tests are confirmed, i[/color]
                                > > can[color=darkred]
                                > > > get my data.
                                > > >
                                > > > please help....(again) :([/color]
                                > >
                                > > --
                                > > http://mail.python.org/mailman/listinfo/python-list
                                > >[/color]
                                >[/color]

                                Comment

                                Working...