/
PicModule.py
160 lines (128 loc) · 4.44 KB
/
PicModule.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/python3.2
import minimalmodbus
import time
class PicMbus:
#CONFIGURATION IO
IOCONFIG_NONE= 255
IOCONFIG_ANALOGVDD= 0
IOCONFIG_ANALOG1V= 1
IOCONFIG_ANALOG2V= 2
IOCONFIG_ANALOG4V= 3
IOCONFIG_INPUT= 4
IOCONFIG_INPUT_PULLUP= 5
IOCONFIG_OUTPUT= 6
IOCONFIG_PWM= 7
IOCONFIG_CAP_SENSE_OFF= 8
IOCONFIG_CAP_SENSE_LOW=9
IOCONFIG_CAP_SENSE_MEDIUM=10
IOCONFIG_CAP_SENSE_HIGH=11
IOCONFIG_DHT11= 16
IOCONFIG_DHT22= 17
IOCONFIG_DS18B20= 32
IOCONFIG_SERVO= 64
IOCONFIG_COUNTER= 128
IOConfig=[IOCONFIG_NONE,IOCONFIG_NONE,IOCONFIG_NONE,IOCONFIG_NONE,IOCONFIG_NONE,
IOCONFIG_NONE,IOCONFIG_NONE,IOCONFIG_NONE,IOCONFIG_NONE,IOCONFIG_NONE]
def __init__(self,SlaveAddress,Baud=57600,Device='/dev/ttyUSB0'):
self.SlaveAddress= SlaveAddress
self.module = minimalmodbus.Instrument(Device,SlaveAddress)
self.module.serial.baudrate=Baud
self.module.serial.timeout=0.05
self.module.serial.flushInput();
#ok lire Identification
# Id = self.readId()
# Count=0
# if Id == 0x653A:
Count=10
# else:
# Count=2
#ok lire Configuration
# for loop in range(Count):
# self.IOConfig[loop]= self.module.read_register(0x100+loop,0,3)
def setAddress(self, SlaveAddress):
self.SlaveAddress = SlaveAddress
self.module.address=SlaveAddress
#we change module . we don't know the io config so let's put it to none
for i in self.IOConfig:
i=self.IOCONFIG_NONE
def config(self,Pin,value):
#enable configuration change
self.module.write_register(0x1ff,0x5678,0,6)
self.module.write_register(0x100+Pin,value,0,6)
self.IOConfig[Pin]= value
def readConfig(self,Pin):
ioconfig=self.module.read_register(0x100+Pin,0,3)
self.IOConfig[Pin]=ioconfig
return ioconfig
def readVRef2V(self):
return self.module.read_register(0x1000,0,4)
def readDiode(self):
return self.module.read_register(0x1001,0,4)
def readSensor(self,Pin):
#first let's checkif we know the config information
ioconfig= self.IOConfig[Pin]
if ioconfig == self.IOCONFIG_NONE:
ioconfig= self.readConfig(Pin)
if (ioconfig == self.IOCONFIG_PWM) or (ioconfig == self.IOCONFIG_SERVO):
return self.module.read_register(Pin);
elif (ioconfig & self.IOCONFIG_CAP_SENSE_OFF)>0:
return self.module.read_long(Pin*16,4,False)
elif (ioconfig & (self.IOCONFIG_DHT11 | self.IOCONFIG_DS18B20 | self.IOCONFIG_COUNTER))>0:
return self.module.read_registers(Pin*16,3,4)
else:
return self.module.read_registers(Pin*16,1,4)[0]
def resetCounter(self,Pin):
return self.module.write_register(Pin,0,0,6)
def ResetModule(self):
return self.module.write_register(0xAA55,0x1234,0,6)
def RCServo(self,Pin,value):
return self.module.write_register(Pin,value,0,6)
def readVersion(self):
version = self.module.read_register(250,0,3)
print("{}.{}".format(version>>8, version & 0xff))
def readId(self):
return self.module.read_register(251,0,3)
def readIO(self,Pin):
return self.module.read_bit(Pin)
def writeIO(self,Pin,Value):
self.module.write_bit(Pin,Value)
def writeAllIO(self,Value):
self.module.write_register(0x1002,Value,0,6)
def readAllIO(self):
return self.module.read_register(0x1002,0,4)
def readDHT(self,Pin):
#check config
if self.IOConfig[Pin] == self.IOCONFIG_NONE:
self.readConfig(Pin)
if (self.IOConfig[Pin] & self.IOCONFIG_DHT11) == 0:
#return IO config not set for dht
return None
while(True):
value = self.readSensor(Pin)
if value[0] == 0xffff:
#No sensor Found
return None
if value[0] == 1:
if self.IOConfig[Pin] == self.IOCONFIG_DHT11 :
Factor = 1.0
else:
if(value[2] & 0x8000)!=0:
Factor = (-0.1)
else:
Factor = 0.1
temperature = (value[2] & 0x7fff) * Factor
humidity = value[1] * 0.1
return [humidity , temperature]
def readDS18B20(self,Pin):
#check config
if self.IOConfig[Pin] == self.IOCONFIG_NONE:
self.readConfig(Pin)
if (self.IOConfig[Pin] & self.IOCONFIG_DS18B20) ==0:
#return IO config not set for dht
return None, 0xffff
value = self.readSensor(Pin)
Factor = 0.0625
Temp = value[1]
if(Temp & 0x8000) !=0:
Temp-=65536
return Temp*Factor, value[0]