Python 测试多条DS18B20传感器电缆
我对python和一般的编码都是新手,所以我在这里寻求帮助。在工作中,我们需要测试多条电缆,最多连接五个DS18B20传感器。我们有这个程序,我们已经用它测试了一段时间的树莓皮电缆。但它几乎没有什么问题 程序需要检测连接的传感器数量并查看它们是否正常工作,然后按序列号Lower>highest列出它们(这样我们可以对它们进行预热,查看哪一个传感器突出显示,逐个并对其进行物理编号),然后重复 它通常感觉很慢,而且经常会撞车。 有了连接了5个传感器的电缆,它可以在最多2根电缆上找到传感器(如果幸运的话,会更多),当你连接第3根电缆时,它将再也找不到传感器,或者它会一直说连接了10个传感器。重新启动Pi会修复它,但这需要时间。热插拔电缆可能是这里的罪魁祸首,但这是最快的方法 使用3根传感器电缆,您可以检查其中的4-5根,直到其挂起 欢迎提供问题所在的任何提示,或者我如何使事情更高效、更快 代码:Python 测试多条DS18B20传感器电缆,python,raspberry-pi,Python,Raspberry Pi,我对python和一般的编码都是新手,所以我在这里寻求帮助。在工作中,我们需要测试多条电缆,最多连接五个DS18B20传感器。我们有这个程序,我们已经用它测试了一段时间的树莓皮电缆。但它几乎没有什么问题 程序需要检测连接的传感器数量并查看它们是否正常工作,然后按序列号Lower>highest列出它们(这样我们可以对它们进行预热,查看哪一个传感器突出显示,逐个并对其进行物理编号),然后重复 它通常感觉很慢,而且经常会撞车。 有了连接了5个传感器的电缆,它可以在最多2根电缆上找到传感器(如果幸运的
import os,sys,time
import curses
stdscr = curses.initscr()
begin_x = 5; begin_y = 5
height = 40; width = 40
def temp_raw(sensor_str):
f = open(sensor_str, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp(sensor_str):
lines = temp_raw(sensor_str)
while lines[0].strip()[-3:] != 'YES':
lines = temp_raw(sensor_str)
temp_output = lines[1].find('t')
if temp_output != -1:
temp_string = lines[1].strip()[temp_output+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
init = 1
timer = 0
search = 1
curses.noecho()
curses.curs_set(0)
subdirectories = []
old_nbr_of_subdirs = 1
nbr_of_subdirs = 1
old_subdirectories = []
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
while (1):
timer = timer + 1
while search == 1:
stdscr.addstr(10,30, "Searching for sensors");
subdirectories = os.listdir("/sys/bus/w1/devices/");
nbr_of_subdirs = len(subdirectories);
time.sleep(3)
if nbr_of_subdirs > old_nbr_of_subdirs:
stdscr.addstr(10,30, "Found %d new sensors " % (nbr_of_subdirs-old_nbr_of_subdirs));
stdscr.addstr(11,30, "Is this all sensors? (y/n)?");
stdscr.refresh()
c = -1;
while(c == -1):
c = stdscr.getch();
if c == ord('y'):
stdscr.addstr(10,30, " ");
stdscr.addstr(11,30, " ");
stdscr.refresh()
old_nbr_of_subdirs = nbr_of_subdirs;
init = 1;
search = 0;
timer = 0;
else:
stdscr.addstr(10,51 + timer, ". ");
timer = timer + 1;
timer = timer % 4;
stdscr.refresh()
if init == 1:
init = 0
alive_ticker = 0;
temp_list = [];
sensor_oldtemp = [0,0,0,0,0,0];
sensor_temp = [0,0,0,0,0,0];
active_sensor = [0,0,0,0,0,0];
sensors = 0;
alive_ticker = 0;
active = 2;
confirm = 0;
stdscr.addstr(17,0, "Number of subdirectories %d" % (len(subdirectories)));
removed = 0;
index = 0;
length_old_subdirs = len(old_subdirectories);
while (index < length_old_subdirs):
length_old_subdirs = len(old_subdirectories);
if old_subdirectories[index] in subdirectories:
subdirectories.remove(old_subdirectories[index]);
removed += 1;
index += 1;
else:
old_subdirectories.remove(old_subdirectories[index]);
index = 0;
stdscr.addstr(30,20, "Removed %d" % (removed))
stdscr.addstr(18,0, "Number of CUT subdirectories %d" % (len(subdirectories)));
stdscr.addstr(19,0, "Number of old_subdirectories %d" % (len(old_subdirectories)));
stdscr.addstr(13,20, "Press space to confirm active sensor!");
stdscr.addstr(14,20, "Press r to reset!");
for index in range(len(subdirectories)):
if subdirectories[index].find("28") != -1:
temp_list.append("/sys/bus/w1/devices/"+subdirectories[index]+"/w1_slave");
sensor_temp[sensors] = 0;
sensors = sensors + 1;
old_subdirectories = old_subdirectories + subdirectories;
temp_list.sort();
if confirm == 1:
stdscr.addstr(13,20, "Press space to confirm active sensor!");
stdscr.addstr(14,20, "Press r to restart!");
curses.halfdelay(1);
c = stdscr.getch();
if c == 32:
confirm = 0;
timer = 10
for z in range(sensors):
if active_sensor[z] > active:
stdscr.addstr(z+5,20, " " , curses.A_DIM)
active_sensor[z] = 100
stdscr.refresh()
elif c == ord('n'):
init = 1
continue
elif c == ord('r'):
for z in range(sensors):
active_sensor[z] = 1;
if (timer > 9):
timer = 0
nbr_active_sensors = 0
for y in range(sensors):
if 99 > active_sensor[y]:
nbr_active_sensors = nbr_active_sensors + 1;
sensor_temp[y] = read_temp(temp_list[y]);
if ((sensor_oldtemp[y] + 0.1) < sensor_temp[y]):
active_sensor[y] = active_sensor[y] + 1;
sensor_oldtemp[y] = sensor_temp[y]
stdscr.addstr(3,5, "nbr_active_sensors=%d" % (nbr_active_sensors))
stdscr.addstr(4,5, "sensors=%d" % (sensors))
if nbr_active_sensors == 0:
search = 1
timer = 0;
continue
for x in range(sensors):
if (99 > active_sensor[x] and active_sensor[x] > active):
confirm = 1
stdscr.addstr(x+5,20, "Sensor %d value %4.2f ID:%s : %d " % (x+1, sensor_temp[x], temp_list[x][20:-9], active_sensor[x]), curses.A_STANDOUT)
elif active_sensor[x] <= active:
stdscr.addstr(x+5,20, "Sensor %d value %4.2f ID:%s : %d " % (x+1, sensor_temp[x], temp_list[x][20:-9], active_sensor[x]))
stdscr.refresh()
alive_ticker = alive_ticker + 1
stdscr.addstr(2, 55, "Alive ticker: %6d" % (alive_ticker))
导入操作系统、系统、时间
进口诅咒
stdscr=curses.initscr()
开始x=5;开始_y=5
高度=40;宽度=40
def温度原始值(传感器温度):
f=开路(传感器“r”)
行=f.读行()
f、 关闭()
回程线
def读取温度(传感器温度):
线路=温度原始值(传感器str)
而行[0].strip()[-3:!='是的:
线路=温度原始值(传感器str)
temp_输出=行[1]。查找('t')
如果温度输出!=-1:
temp_字符串=行[1]。strip()[temp_输出+2:]
温度c=浮动(温度字符串)/1000.0
返回温度
init=1
计时器=0
搜索=1
诅咒
诅咒。诅咒集(0)
子目录=[]
旧分区的编号=1
nbr\u/r=1
旧的_子目录=[]
操作系统('modprobe w1 gpio')
操作系统('modprobe w1 therm')
而(一):
定时器=定时器+1
搜索=1时:
stdscr.addstr(10,30,“搜索传感器”);
子目录=os.listdir(“/sys/bus/w1/devices/”);
nbr_of_subdirs=len(子目录);
时间。睡眠(3)
如果子分区的nbr\U>旧的子分区的nbr\U:
stdscr.addstr(10,30,“发现%d个新传感器”%(分区的nbr_-old_nbr_));
stdscr.addstr(11,30,“这是所有传感器吗?(y/n)?”;
stdscr.refresh()
c=-1;
而(c==-1):
c=stdscr.getch();
如果c==ord('y'):
stdscr.addstr(10,30,“”);
stdscr.addstr(11,30,“”);
stdscr.refresh()
旧分区的编号=分区的编号;
init=1;
搜索=0;
定时器=0;
其他:
stdscr.addstr(10,51+定时器“.”);
定时器=定时器+1;
计时器=计时器%4;
stdscr.refresh()
如果init==1:
init=0
活股票=0;
临时列表=[];
传感器温度=[0,0,0,0,0,0];
传感器温度=[0,0,0,0,0,0];
有源_传感器=[0,0,0,0,0];
传感器=0;
活股票=0;
活跃=2;
确认=0;
stdscr.addstr(17,0,“子目录数%d%”(len(子目录));
删除=0;
指数=0;
长度\旧\子目录=len(旧\子目录);
而(索引激活:
stdscr.addstr(z+5,20,“,curses.A_DIM)
有源_传感器[z]=100
stdscr.refresh()
elif c==ord('n'):
init=1
持续
elif c==ord('r'):
对于范围内的z(传感器):
有源_传感器[z]=1;
如果(计时器>9):
计时器=0
nbr\u有源传感器=0
对于范围内的y(传感器):
如果99>有源_传感器[y]:
nbr\U有源传感器=nbr\U有源传感器+1;
传感器温度[y]=读取温度(温度列表[y]);
如果((传感器温度[y]+0.1)<传感器温度[y]):
有源检波器[y]=有源检波器[y]+1;
传感器温度[y]=传感器温度[y]
stdscr.addstr(3,5,“nbr\u活动传感器=%d”%(nbr\u活动传感器))
stdscr.addstr(4,5,“传感器=%d”%(传感器))
如果nbr\U活动传感器==0:
搜索=1
定时器=0;
class DS18B20(object):
...
def read(self):
...
class One_Wire(object):
def search(self):
print("Searching sensors")
...
def read(self):
print('Reading sensors')
for key in self.sensors:
self.sensors[key].read()
def refresh(self):
print("Refreshing sensors")
...
def history(self, alive=True):
print('Reading History')
...
if __name__ == '__main__':
oWire = One_Wire()
oWire.search()
print(oWire)
oWire.refresh()
print(oWire)
oWire.read()
print(oWire)
history = oWire.history()
print(history)
Searching sensors
Sensors:5, active:5
id:i2c-0 CONNECT temp:None, id:i2c-3 CONNECT temp:None, id:i2c-1 CONNECT temp:None, id:i2c-5 CONNECT temp:None, id:i2c-6 CONNECT temp:None
Refreshing sensors
NEW Sensor:i2c-2
NEW Sensor:i2c-4
Sensors:7, active:6
id:i2c-0 CONNECT temp:None, id:i2c-2 CONNECT temp:None, id:i2c-3 CONNECT temp:None, id:i2c-1 disconnect temp:None, id:i2c-5 CONNECT temp:None, id:i2c-6 CONNECT temp:None, id:i2c-4 CONNECT temp:None
Reading sensors
Reading sensors
Reading sensors
Sensors:7, active:6
id:i2c-0 CONNECT temp:19, id:i2c-2 CONNECT temp:25, id:i2c-3 CONNECT temp:26, id:i2c-1 disconnect temp:None, id:i2c-5 CONNECT temp:25, id:i2c-6 CONNECT temp:18, id:i2c-4 CONNECT temp:30
Reading History
[{'i2c-0': [20, 27, 19]}, {'i2c-2': [30, 21, 25]}, {'i2c-3': [23, 29, 26]}, {'i2c-5': [30, 18, 25]}, {'i2c-6': [30, 21, 18]}, {'i2c-4': [24, 18, 30]}]