473,735 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is there a problem on this simple code

basically what the code does is transmit data to a hardware and then
receive data that the hardware will transmit.

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 = 12000 #to warm-up the hardware
hexed = hex(item)
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)) #serial transmit protocol
time.sleep(1)

item = 10000
no = 0
for item in range(10000, 30001, 250):
no = no +1
hexed = hex(item)
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 no, command, msg_no, no_databyte, temp1, temp2, pyra1,
pyra2, voltage, current, checksum
time.sleep(1)

ser.close()
=============== ============
and here is some result after running the program on Idle Python 2.3
(enthought ed.)

1 70 168 6 0 0 0 0 0 0 12
2 70 2 6 0 0 0 0 0 0 178
3 70 3 6 0 0 0 0 0 0 177
4 70 4 6 0 0 0 0 0 0 176
5 70 5 6 0 0 0 0 0 0 175
6 70 6 6 0 0 0 0 0 0 174
7 70 7 6 0 0 0 0 0 0 173
8 70 8 6 0 0 0 0 0 0 172
9 70 9 6 0 0 0 0 0 0 171
10 70 10 6 0 0 0 0 0 0 170
11 70 11 6 0 0 0 0 0 0 169
12 70 12 6 0 0 0 0 0 0 168
13 70 13 6 0 0 0 0 0 0 167
14 70 14 6 0 0 0 0 0 0 166
15 70 15 6 0 0 0 0 0 0 165

=============== ===========

the result i am expecting is for the data bytes (bytes 4-9) to change
its value since i am able to control the sensors which the data were
based. i am just asking for your opinion if there is something wrong
with this program, otherwise, it could be a hardware problem.

thanks in advance.
Jul 18 '05 #1
38 3533
hah, this code is anything but simple...

Jul 18 '05 #2
@sir harlin
so you are saying that there is nothing wrong in this simple program.
On 12 Mar 2005 07:39:31 -0800, Harlin Seritt <ha**********@y ahoo.com> wrote:
hah, this code is anything but simple...

--
http://mail.python.org/mailman/listinfo/python-list

Jul 18 '05 #3

jrlen balane wrote:
basically what the code does is transmit data to a hardware and then
receive data that the hardware will transmit.

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 = 12000 #to warm-up the hardware
hexed = hex(item)
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)) #serial transmit protocol
time.sleep(1)

item = 10000
no = 0
for item in range(10000, 30001, 250):
no = no +1
hexed = hex(item)
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))
What actually gets transmitted is "C\x01\x02\x10' \x83".
That's 18 bytes. Is the command supposed to be the ASCII
characters \x01 or a single byte whose value is 1?

data = ser.read(10)
(command, msg_no, no_databyte, temp1, temp2, pyra1, pyra2,
voltage, current, checksum) = unpack('10B', data) #serial receive
protocol
print no, command, msg_no, no_databyte, temp1, temp2, pyra1,
pyra2, voltage, current, checksum
time.sleep(1)

ser.close()
=============== ============
and here is some result after running the program on Idle Python 2.3
(enthought ed.)

1 70 168 6 0 0 0 0 0 0 12
2 70 2 6 0 0 0 0 0 0 178
3 70 3 6 0 0 0 0 0 0 177
4 70 4 6 0 0 0 0 0 0 176
5 70 5 6 0 0 0 0 0 0 175
6 70 6 6 0 0 0 0 0 0 174
7 70 7 6 0 0 0 0 0 0 173
8 70 8 6 0 0 0 0 0 0 172
9 70 9 6 0 0 0 0 0 0 171
10 70 10 6 0 0 0 0 0 0 170
11 70 11 6 0 0 0 0 0 0 169
12 70 12 6 0 0 0 0 0 0 168
13 70 13 6 0 0 0 0 0 0 167
14 70 14 6 0 0 0 0 0 0 166
15 70 15 6 0 0 0 0 0 0 165

=============== ===========

the result i am expecting is for the data bytes (bytes 4-9) to change
its value since i am able to control the sensors which the data were
based. i am just asking for your opinion if there is something wrong
with this program, otherwise, it could be a hardware problem.

thanks in advance.


Jul 18 '05 #4
jrlen balane *TOP-POSTED*:
On 12 Mar 2005 07:39:31 -0800, Harlin Seritt <ha**********@y ahoo.com>

wrote:
hah, this code is anything but simple...

--
http://mail.python.org/mailman/listinfo/python-list

@sir harlin
so you are saying that there is nothing wrong in this simple program.


No, he is saying that it is not simple IHHO. Neither are your
explanations IMHO. What does "(enthought ed.)" mean??

Some observations:

The variable message_no is set to 1 initially, but thereafter is set to
the message_no returned from the hardware -- 168 the first time. This
is at best very confusing. I'd suggest two separate variables,
to_hw_msg_no and from_hw_msg_no. Does the hardware reject a message
with a number that is not ((previous msg no + 1) % 256)?

You have a similar problem with "command"; initially 67 but then is set
to what comes back from the hardware i.e. 70. Again confusing, maybe
wrong, use 2 variables to_hw_command and from_hw_command .

Some suggestions:

(1) read the manual for the hardware interface

(2) look at all the variables you have; what are they for? E.g. "hexed"
is computed but never used. Clean out the irrelevant stuff and it may
become simple enough for delicate folk like Harlin to be able to read
it :-)

(3) are you sure you have the check-sum right? Using a hard-coded 0x46
(= 67 + 1 + 2) is dangerous! Recall command was 67 but then changes to
70! What does the manual say happens if the check-sum fails? It would
be sensible to compute the check-sum from the *ACTUAL* data that is
being sent: check_sum = 256 - (to_hw_command + to_hw_msg_no +
total_data + data_lo + data_hi) & 0xff -- but see next point.

(4) If the program is going to become more complicated, you should get
some structure into it. For example a function
bytes_to_hardwa re(command, msg_no, list_or_tuple_o f_ints) would be a
good idea -- it would compute the data-length and the check_sum and do
the struct.pack(), ONCE.

Then you could have another function (say)
!def item_to_hardwar e(command, msg_no, item):
! bytes_to_hardwa re(command, msg_no, divmod(1tem, 256))

(5) Not only are the data values not changing, they appear to be all
zero; what does this mean?

(6) You are not validating the checksum coming back from the hardware;
the values printed out appear to be OK -- that is, they sum to zero
modulo 256; check the manual to see if this is the correct expectation!
-- but the point of having a CHECKsum is that you should CHECK it. It
may not be OK tomorrow.

(7) What does command=70 coming back from the hardware mean? Are there
"commands" that mean (a) invalid command received (b) failure in
message_number protocol (c) check_sum error in received message? If so,
augment your program to check for these. If not, or the h/w doesn't
respond appropriately when you send it a bad message, drop it down the
great white chute in your bathroom :-)

Jul 18 '05 #5

me********@aol. com wrote:

What actually gets transmitted is "C\x01\x02\x10' \x83".
No, that's repr(What actually gets transmitted)
That's 18 bytes. Is the command supposed to be the ASCII
characters \x01 or a single byte whose value is 1?


For a start, according to the OP's code, the command ('C' a.k.a. 67) is
first. The 1 is a meant to be a message number.

Secondly, the hardware smells like it's got an 8080 or 6502 inside. The
likelihood that it groks Python/C string representation is minimal.
Folk just don't send 18 bytes at 9600 bps when 6 bytes will do.

Jul 18 '05 #6
On 12 Mar 2005 17:35:50 -0800, "John Machin" <sj******@lexic on.net> wrote:

me********@aol .com wrote:

What actually gets transmitted is "C\x01\x02\x10' \x83".
No, that's repr(What actually gets transmitted)


If so, that's 6 bytes, not 18:
"C\x01\x02\x10' \x83" "C\x01\x02\x10' \x83" list("C\x01\x02 \x10'\x83") ['C', '\x01', '\x02', '\x10', "'", '\x83'] len("C\x01\x02\ x10'\x83") 6

OTOH, list(r"C\x01\x0 2\x10'\x83") ['C', '\\', 'x', '0', '1', '\\', 'x', '0', '2', '\\', 'x', '1', '0', "'", '\\', 'x', '8', '3'] len(r"C\x01\x02 \x10'\x83")

18
That's 18 bytes. Is the command supposed to be the ASCII
characters \x01 or a single byte whose value is 1?


For a start, according to the OP's code, the command ('C' a.k.a. 67) is
first. The 1 is a meant to be a message number.

Secondly, the hardware smells like it's got an 8080 or 6502 inside. The
likelihood that it groks Python/C string representation is minimal.
Folk just don't send 18 bytes at 9600 bps when 6 bytes will do.


Regards,
Bengt Richter
Jul 18 '05 #7

Bengt Richter wrote:
On 12 Mar 2005 17:35:50 -0800, "John Machin" <sj******@lexic on.net> wrote:

me********@aol .com wrote:

What actually gets transmitted is "C\x01\x02\x10' \x83".


No, that's repr(What actually gets transmitted)


If so, that's 6 bytes, not 18:
>>> "C\x01\x02\x10' \x83" "C\x01\x02\x10' \x83" >>> list("C\x01\x02 \x10'\x83") ['C', '\x01', '\x02', '\x10', "'", '\x83'] >>> len("C\x01\x02\ x10'\x83") 6

OTOH, >>> list(r"C\x01\x0 2\x10'\x83") ['C', '\\', 'x', '0', '1', '\\', 'x', '0', '2', '\\', 'x', '1', '0',

"'", '\\', 'x', '8', '3']
>>> len(r"C\x01\x02 \x10'\x83") 18
That's 18 bytes. Is the command supposed to be the ASCII
characters \x01 or a single byte whose value is 1?


For a start, according to the OP's code, the command ('C' a.k.a. 67)

isfirst. The 1 is a meant to be a message number.

Secondly, the hardware smells like it's got an 8080 or 6502 inside. Thelikelihood that it groks Python/C string representation is minimal.
Folk just don't send 18 bytes at 9600 bps when 6 bytes will do.


Regards,
Bengt Richter


The number of bytes transmitted is 6. However the length of the visual
representation of what was sent is 18. Mensator was confused by this,
as was apparent from his/her question "Is the command supposed to be
the ASCII characters \x01 or a single byte whose value is 1?". I tried
to explain this to him/her. However it's not apparent whether your post
is part of the problem or part of the solution. Enlightenment, please.

Jul 18 '05 #8

John Machin wrote:
me********@aol. com wrote:

What actually gets transmitted is "C\x01\x02\x10' \x83".
No, that's repr(What actually gets transmitted)


Drat, I always get burned by that.
That's 18 bytes. Is the command supposed to be the ASCII
characters \x01 or a single byte whose value is 1?
For a start, according to the OP's code, the command ('C' a.k.a. 67)

is first. The 1 is a meant to be a message number.
Yeah, that's what I meant.

Secondly, the hardware smells like it's got an 8080 or 6502 inside. The likelihood that it groks Python/C string representation is minimal.
That's what I was thinking. And maybe the replies he's seeing
from the hardware is an error message because it doesn't
understand what it's seeing (which could be due to other things).
A datascope would come in handy for this situation.
Folk just don't send 18 bytes at 9600 bps when 6 bytes will do.


Jul 18 '05 #9
On 12 Mar 2005 20:12:19 -0800, "John Machin" <sj******@lexic on.net> wrote:

Bengt Richter wrote:
On 12 Mar 2005 17:35:50 -0800, "John Machin" <sj******@lexic on.net>

wrote:
>
>me********@aol .com wrote:
>>
>> What actually gets transmitted is "C\x01\x02\x10' \x83".
>
>No, that's repr(What actually gets transmitted)


If so, that's 6 bytes, not 18:
>>> "C\x01\x02\x10' \x83"

"C\x01\x02\x10' \x83"
>>> list("C\x01\x02 \x10'\x83")

['C', '\x01', '\x02', '\x10', "'", '\x83']
>>> len("C\x01\x02\ x10'\x83")

6

OTOH,
>>> list(r"C\x01\x0 2\x10'\x83")

['C', '\\', 'x', '0', '1', '\\', 'x', '0', '2', '\\', 'x', '1', '0',

"'", '\\', 'x', '8', '3']
>>> len(r"C\x01\x02 \x10'\x83")

18
>
>> That's 18 bytes. Is the command supposed to be the ASCII
>> characters \x01 or a single byte whose value is 1?
>
>For a start, according to the OP's code, the command ('C' a.k.a. 67)is >first. The 1 is a meant to be a message number.
>
>Secondly, the hardware smells like it's got an 8080 or 6502 inside.The >likelihood that it groks Python/C string representation is minimal.
>Folk just don't send 18 bytes at 9600 bps when 6 bytes will do.
>


Regards,
Bengt Richter


The number of bytes transmitted is 6. However the length of the visual
representati on of what was sent is 18. Mensator was confused by this,
as was apparent from his/her question "Is the command supposed to be
the ASCII characters \x01 or a single byte whose value is 1?". I tried
to explain this to him/her. However it's not apparent whether your post
is part of the problem or part of the solution. Enlightenment, please.


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:
repr("C\x01\x02 \x10'\x83") '"C\\x01\\x02\\ x10\'\\x83"' str("C\x01\x02\ x10'\x83")

"C\x01\x02\x10' \x83"

Sorry, no enlightenment ;-)

Regards,
Bengt Richter
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6081
by: delisonews | last post by:
I'm looking for a simple, filesystem-based message board. (No MySQL!) Something that I could include easily in my code: include '../inc/messageboard.php'; .... so that the board shows up at the bottom of every PHP page. The board should have just the basic features, like: - posting capability
24
6340
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to have on his site, a Javascript Calculator for working out the cost of what they want, for example: 1 widget and 2 widglets = £5.00
10
110276
true911m
by: true911m | last post by:
This is a simple walkthrough to get PyInstaller up and running. I decided to give PI a try, because it claims to be more selective about what it bundles into its executable files by default, and it also integrates UPX (Ultimate Packer for eXecutables) into the build process, if you have it installed. It also claims functionality on linux, as a bonus (I didn't test this). I highly recommend the UPX options, and will cover how to get it...
14
2984
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
2
1427
by: Michael7 | last post by:
Hi everyone, I'm new to CSS of course, and have been trying to learn it. However, when I try to pull off something as simple as positioning of text . . . nothing works in my index page. So in frustration I made a test page. Just a but of words to test only the code I'm looking for. Still nothing! I apologize if this question is a nuisance, but could anyone perhaps at least let me know what I'm doing wrong with this very simple thing?...
30
3538
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
1
2216
by: =?ISO-8859-1?Q?Tor_Erik_S=F8nvisen?= | last post by:
Hi, A while ago I asked a question on the list about a simple eval function, capable of eval'ing simple python constructs (tuples, dicts, lists, strings, numbers etc) in a secure manner: http://groups.google.com/group/comp.lang.python/browse_thread/thread/58a01273441d445f/ also pointed to a simple eval function by Fredrik Lundh: http://effbot.org/zone/simple-iterator-parser.htm. His solution, using module tokenize, was short and...
10
2133
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.ComponentModel <System.Web.Services.WebService(Namespace:="http:// wwwpreview.#deleted#.co.uk/~ptaylor/Customer.wsdl")_
1
2073
by: astrogirl77 | last post by:
I'm new to C++ and am hoping to find help with coding a simple C program, am wanting to obtain code and functioning exe's. I code in an old version of Visual Basic 4.0, I have a simple app that is about 3 and a half pages of code long it does some relatively simple math additions and subtractions The problem I have is that some numbers get to be very large integers and VB automatically converts this to scientifc notation, what I need is...
17
5815
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /* Simple Thread Object ______________________________________________________________*/ #include <pthread.h> extern "C" void* thread_entry(void*);
0
8965
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8786
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9466
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9255
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8202
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6748
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4567
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4823
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2191
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.