Blame view

src/utils/plc/guitalens_observatory/driver_cv7.py 3.47 KB
ac31ddb1   Alain Klotz   New celme documen...
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
# -*- coding: utf-8 -*-
# == pour donner les droits d'acces a /dev/ttS0
# gedit /boot/cmdline.txt &
# effacer console=serial0,115200
# == a priori il n'est pas necessaire de faire les deux lignes suivantes
# usermod -a -G tty pi
# chmod 777 /dev/ttyS0

import time
import serial
import RPi.GPIO as io
    
def get_data(port="/dev/ttyS0",pin_vin_bcm=25, debug_level=0):
   """Lecture d'une trame du capteur meteo CV7. 
Retourne un tuple, dans l'ordre: Un code de validité, la vitesse du vent en m/s, la direction relative du vent en degre, la température en °C, la trame IIMWV lue, la trame WIXDR lue. Le code de validité =0 si la trame est valide, =1 si Vin n'est pas detecte, =2 si les trames ne sont pas valides.

Exemple:

import driver_cv7 ; driver_cv7.get_data("/dev/ttyS0", 25,2)
"""

   # === verify 12V power supply if it is possible
   power_mife = 1
   if (pin_vin_bcm>0):
      io.setmode(io.BCM) #set up GPIO using BCM numbering
      io.setup(pin_vin_bcm,io.IN, pull_up_down=io.PUD_DOWN) # make pin an input (=arduino 35->34 SWO_T60 = Vin)
      pin34  = io.input(pin_vin_bcm)
      power_mife = 1-pin34
      if (debug_level==2):
         print("=== Voltage intput ===")
         disp ="pin34,35,gpio25,Vin="+str(pin34)+ " (0=12V 1=0V)"
         print(disp)
   err = 0
   if ( power_mife==1 ):
      power_mife_msg = "12Volts"
      power_mife = 1
   else:
      power_mife_msg = "0Volt"
      power_mife = 0
      err = err +1

   # === get the raw frame
   serialerror=0
   try:
      ser = serial.Serial(
         port=port,
         baudrate = 4800,
         parity=serial.PARITY_NONE,
         stopbits=serial.STOPBITS_ONE,
         bytesize=serial.EIGHTBITS,
         timeout=0,
      )
      if ser.isOpen():
         serialerror=0
      else:
         serialerror=1
   except:
      serialerror=2
   else:
      if ser.isOpen():
         time.sleep(0.1)
         ser.flush()
         raw_str=""
         for i in range(0,30000):
            x=ser.read()
            if (len(x)>0):
                raw_str+= "{:c}".format(x[0])
         if (debug_level==2):
            print("=== Raw frame ===")
            print(raw_str)
         ser.close()

   # === decode
   valid=0b00
   wind_dir_deg = 0
   wind_dir_kt = 0
   wind_dir_ms = 0
   wind_tmp_degC = 0
   msg_wind = ""
   msg_temp = ""
   raw_list = raw_str.split()
   if (debug_level==2):
      print("=== Raw list ===")
      print(raw_list)
   for message in raw_list:
      msg = str(message)
       # $IIMWV,135.0,R,000.0,N,A*3A
      key = "$IIMWV"
      k = msg.find(key)
      if ( (k==0) and (len(msg)>=27) ):
         msg_list = msg.split(",");
         if (debug_level==2):
            print("=== IIMWV ===")
            print(msg)
            print(msg_list)
         wind_dir_deg = float(msg_list[1])
         wind_speed_kt = float(msg_list[3])
96e2b8ea   Alain Klotz   Ameliorations sur...
98
         wind_speed_ms = wind_speed_kt*1.852/3.6
ac31ddb1   Alain Klotz   New celme documen...
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
         valid = valid | 0b01
         msg_wind = msg
      # $WIXDR,C,021.0,C,,*51
      key = "$WIXDR"
      k = msg.find(key)
      if ( (k==0) and (len(msg)>=21) ):
         msg_list = msg.split(",");
         if (debug_level==2):
            print("=== WIXDR ===")
            print(msg)
            print(msg_list)
         wind_tmp_degC = float(msg_list[2])
         valid = valid | 0b10
         msg_temp = msg
   # === return
   err = 0
   if (power_mife==0):
      err += 1
   if (serialerror==1):
      err += 2
   if (valid==0):
      err += 4

   return err, power_mife, wind_dir_deg, wind_speed_ms, wind_tmp_degC, power_mife_msg, msg_wind, msg_temp