设置 serial.readline() 的采样率
Setting sampling rate of serial.readline()
如何以可控的速度执行 ser.readline(),比如说每 0.002 秒一次?下面的代码在 Python returns 每个 运行 之后的不同大小的列表中,这意味着采样率每次都不同。我想知道在给定所需采样率为 500 scans/second:
的情况下是否有一种从串口读取的受控方式
import numpy as np
from time import time
import serial
ser = serial.Serial('COM3', 115200, timeout=1)
ser.flushInput()
digital_data = np.array([])
# Set the end time 60 seconds from start
te = time() + 60
# While loop runs for 60 seconds
while time() <= te:
digital_data = append(digital_data, ser.readline().decode('utf-8'))
print(len(digital_data)) # Varies in size for each run
这可能不准确,具体取决于 OS 您 运行...
import numpy as np
import time
import serial
ser = serial.Serial('COM3', 115200, timeout=1)
ser.flushInput()
digital_data = np.array([])
# Set the end time 60 seconds from start
te = time.time() + 60
delay = 0.002
while time.time() <= te:
start = time.time()
digital_data = append(digital_data, ser.readline().decode('utf-8'))
duration = time.time() - start
if duration < delay:
time.sleep(delay - duration)
print(len(digital_data)) # Varies in size for each run
如何以可控的速度执行 ser.readline(),比如说每 0.002 秒一次?下面的代码在 Python returns 每个 运行 之后的不同大小的列表中,这意味着采样率每次都不同。我想知道在给定所需采样率为 500 scans/second:
的情况下是否有一种从串口读取的受控方式import numpy as np
from time import time
import serial
ser = serial.Serial('COM3', 115200, timeout=1)
ser.flushInput()
digital_data = np.array([])
# Set the end time 60 seconds from start
te = time() + 60
# While loop runs for 60 seconds
while time() <= te:
digital_data = append(digital_data, ser.readline().decode('utf-8'))
print(len(digital_data)) # Varies in size for each run
这可能不准确,具体取决于 OS 您 运行...
import numpy as np
import time
import serial
ser = serial.Serial('COM3', 115200, timeout=1)
ser.flushInput()
digital_data = np.array([])
# Set the end time 60 seconds from start
te = time.time() + 60
delay = 0.002
while time.time() <= te:
start = time.time()
digital_data = append(digital_data, ser.readline().decode('utf-8'))
duration = time.time() - start
if duration < delay:
time.sleep(delay - duration)
print(len(digital_data)) # Varies in size for each run