forked from filipamator/vu_meter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialread.py
More file actions
36 lines (26 loc) · 789 Bytes
/
serialread.py
File metadata and controls
36 lines (26 loc) · 789 Bytes
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
import serial
import time
ser = serial.Serial('COM5', 4*115200, timeout=1)
buffer = bytearray([])
sample = bytearray([])
counter = 0
def twos_comp(val, bits):
"""compute the 2's complement of int value val"""
if (val & (1 << (bits - 1))) != 0: # if sign bit is set e.g., 8bit: 128-255
val = val - (1 << bits) # compute negative value
return val
ser.reset_input_buffer()
while counter<2048:
bufflen = ser.in_waiting
if bufflen > 0:
buffer = ser.read(bufflen)
sample = sample + buffer
counter = counter + len(buffer)
time.sleep(0.05)
#print(len(buffer))
size = len(sample)/2
for x in range(0, int(size) ):
value = 256*sample[2*x] + sample[2*x+1]
value = twos_comp(value,16)
print(str(x) + " " + str(value))
#print(buffer[0])