这个简单的代码有问题吗? [英] is there a problem on this simple code
问题描述
基本上代码的作用是将数据传输到硬件然后
接收硬件将传输的数据。
import serial
导入字符串
导入时间
来自struct import *
ser = serial.Serial()
ser.baudrate = 9600
ser.port = 0
ser
ser.close()
ser.open()
命令= 67
message_no = 1
total_data = 2
item = 12000#来预热硬件
hexed = hex(item)
data_hi,data_lo = divmod(item,0x100)
checksum = - (data_hi + data_lo + 0x46)& 0xff
ser.write(pack(''''',命令,message_no,total_data,data_lo,
data_hi,校验和))#serial传输协议
time.sleep(1)
item = 10000
no = 0
范围内的物品(10000 ,30001,250):
否=否+1
hexed = hex(item)
data_hi,data_lo = divmod(item,0x100 )
校验和= - (data_hi + data_lo + 0x46)& 0xff
ser.write(pack(''''',命令,message_no,total_data,data_lo,
data_hi,校验和))
data = ser.read(10)
(命令,msg_no,no_databyte,temp1,temp2,pyra1,pyra2,
电压,电流,校验和)= unpack('' 10B'',数据)#serial接收
协议
打印否,命令,msg_no,no_databyte,temp1,temp2,pyra1,
pyra2 ,电压,电流,校验和
time.sleep(1)
ser.close()
===== ======================
这是在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
==========================
我期望的结果是数据字节(字节4-9)改变它的价值因为我能够控制传感器的数据是什么
根据。我只是问你的意见,如果有什么不对的话,这个程序可能是
,否则,这可能是硬件问题。
提前感谢。
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_ data_lo = divmod(item, 0x100)
checksum = -(data_hi + data_lo + 0x46) & 0xff
ser.write(pack(''6B'', command, message_no, total_data, data_lo,
data_ 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_ data_lo = divmod(item, 0x100)
checksum = -(data_hi + data_lo + 0x46) & 0xff
ser.write(pack(''6B'', command, message_no, total_data, data_lo,
data_ 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.
推荐答案
哈哈,这段代码不过是简单的......
hah, this code is anything but simple...
@sir harlin
所以你说这个简单程序没有错。
2005年3月12日07:39:31 -0800,Harlin Seritt< ha **********@yahoo.com>写道:
@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**********@yahoo.com> wrote:
哈,这段代码不过是简单的......
http://mail.python.org/mailman/listinfo/python-list
hah, this code is anything but simple...
--
http://mail.python.org/mailman/listinfo/python-list
jrlen balane写道:
jrlen balane wrote:
基本上代码的作用是将数据传输到硬件然后接收数据硬件将传输。
导入序列
导入字符串
导入时间从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#来预热硬件
hexed = hex(item)
data_hi,data_lo = divmod(item,0x100)
checksum = - (data_hi + data_lo + 0x46)& 0xff
ser.write(pack(''''',command,message_no,total_data,data_lo,
data_hi,checksum))#serial传输协议
time.sleep(1)
item = 10000
no = 0
对于范围内的项目(10000,30001,250):
否=否+1
hexed = hex(项目) )data_hi,data_lo = divmod(item,0x100)
checksum = - (data_hi + data_lo + 0x46)& 0xff
ser.write(pack(''''',command,message_no,total_data,data_lo,
data_hi,checksum))
实际传输的内容是C\x01 \ x02 \ x10\\\\ x83。
这是18个字节。命令应该是ASCII
字符\ x01还是单个字节,其值为1?
data = ser.read(10)
(命令,msg_no,no_databyte,temp1,temp2,pyra1,pyra2,
电压,电流,校验和)= unpack('''''','数据)#serial receive
protocol > print no,command,msg_no,no_databyte,temp1,temp2,pyra1,
pyra2,voltage,current,checksum
time.sleep(1)
ser.close()
===========================
这是在空闲时运行程序后的一些结果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
> ==========================
我期待的结果是数据字节(字节4-9) )因为我能够控制基于数据的传感器,所以要改变它的价值。我只是问你的意见,如果这个程序有什么问题,否则,这可能是一个硬件问题。
提前感谢。
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_ data_lo = divmod(item, 0x100)
checksum = -(data_hi + data_lo + 0x46) & 0xff
ser.write(pack(''6B'', command, message_no, total_data, data_lo,
data_ 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_ data_lo = divmod(item, 0x100)
checksum = -(data_hi + data_lo + 0x46) & 0xff
ser.write(pack(''6B'', command, message_no, total_data, data_lo,
data_ 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.
这篇关于这个简单的代码有问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!