serjial.py 3.1 KB
Newer Older
Amadej Arh's avatar
Amadej Arh committed
1
import math
Amadej Arh's avatar
graf  
Amadej Arh committed
2
import serial
Amadej Arh's avatar
Amadej Arh committed
3
import struct
Amadej Arh's avatar
ok    
Amadej Arh committed
4
from datetime import datetime
Amadej Arh's avatar
zaspan    
Amadej Arh committed
5
import time
Amadej Arh's avatar
graf  
Amadej Arh committed
6

7

Amadej Arh's avatar
Amadej Arh committed
8
def main():
Amadej Arh's avatar
Amadej Arh committed
9
    with open("logfile", "a+") as log:
Adrian's avatar
Adrian committed
10
        with open("beshrukt.uk2", "wb+") as smolOut:
Amadej Arh's avatar
Amadej Arh committed
11
12
13
14
15
            try:
                ser = serial.Serial("/dev/ttyUSB1", 19200)
            except:
                ser = serial.Serial("/dev/ttyUSB0", 19200)

Adrian's avatar
Adrian committed
16
            print(ser.name)
Adrian Šiška's avatar
Adrian Šiška committed
17
18

            print("\033[2J")
Amadej Arh's avatar
Amadej Arh committed
19
20
            accelMin = 10
            accelMax = 0
Adrian Šiška's avatar
Adrian Šiška committed
21
22
23

            correct_len = 170

Adrian's avatar
Adrian committed
24
25
            while 1:
                count_plus = 0
26

Adrian's avatar
Adrian committed
27
28
29
30
31
32
                time.sleep(0.1)
                print("\033[2H")

                while 1:
                    byte = ser.read(1)
                    # print(byte)
33
                    if byte == b"+":
Adrian's avatar
Adrian committed
34
35
36
                        count_plus += 1
                    else:
                        count_plus = 0
37
                    if count_plus == 3:
Adrian's avatar
Adrian committed
38
39
                        print("[PACKET]", end="")
                        break
40

Adrian's avatar
Adrian committed
41
42
43
44
                input_data = ser.read(correct_len)
                # input_data = ser.read(177)
                # input_data = open("example.dat", "rb").read()

45
                if (input_data[169] != 45) or (input_data[168] != 45):
Adrian's avatar
Adrian committed
46
47
                    print("[FAIL]", input_data[169], input_data[168], len(input_data))
                    continue
Adrian Šiška's avatar
Adrian Šiška committed
48
49
50
51
52
53

                # if ((end := input_data.find(b'--')) - (start := input_data.find(b'+++')) + 2 != correct_len):
                #     print(len(input_data))
                #     print(input_data)
                #     continue

Adrian's avatar
Adrian committed
54
55
                input_data = input_data[:-2]
                print("", flush=True)
56

Amadej Arh's avatar
Amadej Arh committed
57
58
59
                rezultat = list(struct.unpack("=9f1l", input_data[:-128]))
                rezultat[3] -= 0.22
                accelSqrt = math.sqrt(rezultat[3]*rezultat[3] + rezultat[4]*rezultat[4] + rezultat[5]*rezultat[5])
Adrian Šiška's avatar
Adrian Šiška committed
60

Amadej Arh's avatar
Amadej Arh committed
61

Adrian Šiška's avatar
Adrian Šiška committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
                print("\x1b[5;10f", end="")

                print("TEMPERATURE:")
                print("\x1b[6;12fT: %3.3f *C  " % (rezultat[0]), end="")

                print("\x1b[8;10f", end="")

                print("HUMIDITY:")
                print("\x1b[9;12fH: %3.3f %%  " % (rezultat[1]), end="")

                print("\x1b[11;10f", end="")

                print("PRESSURE:")
                print("\x1b[12;12fP: %4.3f hPa  " % (rezultat[2]), end="")

                print("\x1b[14;10f", end="")

Amadej Arh's avatar
Amadej Arh committed
79
80
81
82
                print("ACCELERATION: %3.3f G" % (accelSqrt))
                print("\x1b[15;12fX: %3.3f G  " % (rezultat[3]), end="")
                print("\x1b[16;12fY: %3.3f G  " % (rezultat[4]), end="")
                print("\x1b[17;12fZ: %3.3f G  " % (rezultat[5]), end="")
Adrian Šiška's avatar
Adrian Šiška committed
83
84
85
86
87
88
89
90

                print("\x1b[19;10f", end="")

                print("GYROSCOPE:")
                print("\x1b[20;12fX: %3.3f  " % (rezultat[6]), end="")
                print("\x1b[21;12fY: %3.3f  " % (rezultat[7]), end="")
                print("\x1b[22;12fZ: %3.3f  " % (rezultat[8]), end="")

Amadej Arh's avatar
Amadej Arh committed
91
92
                log.write(repr((rezultat, datetime.now(), input_data[-132:])) + "\n")
                smolOut.write(input_data[-132:])
93
                smolOut.flush()
Amadej Arh's avatar
zaspan    
Amadej Arh committed
94

Amadej Arh's avatar
Amadej Arh committed
95
        ser.close()
Amadej Arh's avatar
Amadej Arh committed
96

97

Amadej Arh's avatar
Amadej Arh committed
98
if __name__ == "__main__":
Amadej Arh's avatar
Amadej Arh committed
99
    main()