OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

some questions about Run time per cycle

  • Thread starter Thread starter 不在藏身于荒野
  • Start date Start date

不在藏身于荒野

Guest
I recently ran into some problems: I used the time.time function to calculate the sensor data acquisition function I wrote myself, and a run took about 0.3 seconds. I want it to run continuously and collect, so I'm doing this while True loop. But when I looked at the collection time recorded in the database, I found that the collection time interval was between 1 and 2 seconds, I did not understand why there was such a big difference I want to know how I can fix this problem and increase the speed, because I have increased the function to 0.3 seconds, but finally the actual interval is still 1 or 2 seconds and I am frustrated Here is the full code:

Code:
import struct
import dill as pickle
import serial.tools.list_ports
from binascii import *
import crcmod
import serial
import queue
from pyecharts import options as opts
from pyecharts.charts import Pie, Line, Grid
from multiprocessing import Process, Event, Pool, Manager
from DBUtils.PooledDB import PooledDB
import pymysql
import time
from pyecharts.globals import CurrentConfig

class MysqlClient(object):
    __pool = None

    def __init__(self, mincached=10, maxcached=20, maxshared=10, maxconnections=200, blocking=True,
                 maxusage=100, setsession=None, reset=True,
                 host='localhost', port=3306, db='gzjc_db',
                 user='root', passwd='password', charset='utf8mb4'):
        """

        :param mincached:连接池中空闲连接的初始数量
        :param maxcached:连接池中空闲连接的最大数量
        :param maxshared:共享连接的最大数量
        :param maxconnections:创建连接池的最大数量
        :param blocking:超过最大连接数量时候的表现,为True等待连接数量下降,为false直接报错处理
        :param maxusage:单个连接的最大重复使用次数
        :param setsession:optional list of SQL commands that may serve to prepare
            the session, e.g. ["set datestyle to ...", "set time zone ..."]
        :param reset:how connections should be reset when returned to the pool
            (False or None to rollback transcations started with begin(),
            True to always issue a rollback for safety's sake)
        :param host:数据库ip地址
        :param port:数据库端口
        :param db:库名
        :param user:用户名
        :param passwd:密码
        :param charset:字符编码
        """

        if not self.__pool:
            self.__class__.__pool = PooledDB(pymysql,
                                             mincached, maxcached,
                                             maxshared, maxconnections, blocking,
                                             maxusage, setsession, reset,
                                             host=host, port=port, db=db,
                                             user=user, passwd=passwd,
                                             charset=charset,
                                             cursorclass=pymysql.cursors.DictCursor
                                             )
        # self._conn = None
        # self._cursor = None
        # self.__get_conn()
        self._conn = self.__pool.connection()
        self._cursor = self._conn.cursor()

    def __get_conn(self):
        self._conn = self.__pool.connection()
        self._cursor = self._conn.cursor()

    def close(self):
        try:
            self._cursor.close()
            self._conn.close()
        except Exception as e:
            print(e)

    def __execute(self, sql, param=()):
        count = self._cursor.execute(sql, param)
        print(count)
        return count

    def begin(self):
        """开启事务"""
        self._conn.autocommit(0)

    def end(self, option='commit'):
        """结束事务"""
        if option == 'commit':
            self._conn.autocommit()
        else:
            self._conn.rollback()

class cgq():
    def __init__(self, Cganqi, COM):
        # 实例
        self.Cganqi = Cganqi
        self.mc = MysqlClient()
        self.mc._cursor.execute('SELECT * FROM cgqshebeixinxi WHERE id =' + str(self.Cganqi))
        myresult = self.mc._cursor.fetchall()
        print(myresult)
        shebeilist = []
        for x in myresult[0].values():
            shebeilist.append(x)
        self.sbmc = shebeilist[1]
        self.sblx = shebeilist[2]
        self.ssqy = shebeilist[3]
        self.ckxx = shebeilist[4]
        # host可以改成地址信息
        # COM=self.ckxx
        self.mc.close()
        # 数据
        self.qX = queue.Queue(maxsize=0)
        self.qY = queue.Queue(maxsize=0)
        self.qZ = queue.Queue(maxsize=0)
        # 数据
        self.qX1 = queue.Queue(maxsize=0)
        self.qY1 = queue.Queue(maxsize=0)
        self.qZ1 = queue.Queue(maxsize=0)
        # 打开串口
        self.ser = serial.Serial(COM, 9600, timeout=1)
        self.ser.set_buffer_size(rx_size=12800, tx_size=12800)
        # 数据库
        self.time1 = []
        self.liedux = []
        self.lieduy = []
        self.lieduz = []
        self.wendu = []
        self.time2 = []
        self.boxingx = []
        self.boxingy = []
        self.boxingz = []
        self.errorresult_tz = []
        self.errorresult_bx = []

    def ChuanRuCGQ(self):
        # temp_send = '01 04 00 1C 00 01 F0 0C'
        global buffer_data1
        temp_send = '01 04 00 10 00 10 F0 03'
        # 发送的数据转为2进制b'\x01\x03\x00\x00\x00\x02\xc4\x0b'
        temp_send = bytes.fromhex(temp_send)
        #print(temp_send)
        #time_start = time.time()  # 记录开始时间
        if self.ser.is_open:
            self.mc.__init__()
            #print('串口已打开')
            self.ser.write(temp_send)
            # self.ser.timeout = 1
            try:
                buffer_data1 = self.ser.read(37)
                #print(buffer_data1)
            except serial.SerialTimeoutException:
                pass
            # 使用 struct 模块解析字节数据
            wendu = struct.unpack('>H', buffer_data1[27:29])[0] / 10
            liedux = struct.unpack('>H', buffer_data1[15:17])[0] / 100
            lieduy = struct.unpack('>H', buffer_data1[17:19])[0] / 100
            lieduz = struct.unpack('>H', buffer_data1[19:21])[0] / 100
            #print("当前温度为:", wendu)
            #print("当前烈度x为:", liedux)
            #print("当前烈度y为:", lieduy)
            #print("当前烈度z为:", lieduz)
            sql = "INSERT INTO ChuanGanQi" + str(self.Cganqi) + "(时间, 温度, 烈度x, 烈度y, 烈度z) VALUES (%s, %s, %s, %s, %s)"
            # 时间,数字量要加str
            localtimes = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            val = (localtimes, str(wendu), str(liedux), str(lieduy), str(lieduz))
            self.mc._cursor.execute(sql, val)
            self.mc._conn.commit()  # 数据表内容有更新,必须使用到该语句
            #print(self.mc._cursor.rowcount, "条记录插入成功。")
            self.mc.close()
            #time_end = time.time()  # 记录结束时间
            #time_sum = time_end - time_start  # 计算的时间差为程序的执行时间,单位为秒/s
            #print(time_sum)


    def JieShouBoxing(self):

        time_start = time.time()  # 记录开始时间
        global buffer_data2
        self.mc.__init__()
        # 发送标志位
        temp_send1 = '01 06 27 10 00 01'
        temp_send1 = crc16Add(temp_send1)
        # print(temp_send1)
        temp_send1 = bytes.fromhex(temp_send1)
        # print(temp_send1)
        if self.ser.is_open:
            #print('串口已打开')
            self.ser.write(temp_send1)
        #else:
            #print('串口未打开')
        # 100毫秒延时
        time.sleep(0.1)
        # 查看状态码
        temp_send2 = '01 03 27 10 00 01'
        temp_send2 = crc16Add(temp_send2)
        # print(temp_send2)
        temp_send2 = bytes.fromhex(temp_send2)
        self.ser.write(temp_send2)
        try:
            buffer_data2 = self.ser.read(15)
            # buffer_data = ser.readline()
        except serial.SerialTimeoutException:
            pass
        return_data_hex = str(buffer_data2.hex())
        # print(return_data_hex)
        # print(return_data_hex[20:22])
        jud = return_data_hex[20:22]
        # 不应该读取80个 记得改
        if jud == '02':
            # 从文件中加载预先计算好的数据
            with open('send_data.pkl', 'rb') as f:
                send_data_list = pickle.load(f)
            for temp_send3X, temp_send3Y, temp_send3Z in send_data_list:
                self.ser.write(temp_send3X)
                try:
                    buffer_data3X = self.ser.read(7)
                    #buffer_data3X = self.ser.readline()
                    #print(buffer_data3X)
                    #print('xxxxx')
                    #return_data_hexX = str(buffer_data3X.hex())
                    #print('返回的数据转换为16进制:', return_data_hexX)
                    # 这是存入队列
                    #self.qX.put(return_data_hexX)
                    #self.qX1.put(buffer_data3X)

                except serial.SerialTimeoutException:
                    pass
                self.ser.write(temp_send3Y)
                try:
                    buffer_data3Y = self.ser.read(7)
                    #return_data_hexY = str(buffer_data3Y.hex())
                    #print('返回的数据转换为16进制:', return_data_hexY)
                    # 这是存入队列
                    #self.qY.put(return_data_hexY)
                    #self.qY1.put(buffer_data3Y)
                except serial.SerialTimeoutException:
                    pass
                self.ser.write(temp_send3Z)
                try:
                    buffer_data3Z = self.ser.read(7)
                    #return_data_hexZ = str(buffer_data3Z.hex())
                    #print('返回的数据转换为16进制:', return_data_hexZ)
                    # 这是存入队列
                    #self.qZ.put(return_data_hexZ)
                    #self.qZ1.put(buffer_data3Z)
                except serial.SerialTimeoutException:
                    pass
            temp_send4 = '01 06 27 10 00 00'
            temp_send4 = crc16Add(temp_send4)
            temp_send4 = bytes.fromhex(temp_send4)
            self.ser.write(temp_send4)
            #x_data = GetApm(self.qX.get())
            #y_data = GetApm(self.qY.get())
            #z_data = GetApm(self.qZ.get())
            x_data1 = GetApm0(buffer_data3X)
            y_data1 = GetApm0(buffer_data3Y)
            z_data1 = GetApm0(buffer_data3Z)


            sql = "INSERT INTO ChuanGanQiBoxing" + str(self.Cganqi) + " (时间, x波形, y波形, z波形) VALUES (%s, %s, %s, %s)"
            # 时间,数字量要加str
            localtimes = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            #print(localtimes)
            val = (localtimes, str(x_data1), str(y_data1), str(z_data1))
            print(localtimes, str(x_data1), str(y_data1), str(z_data1))
            self.mc._cursor.execute(sql, val)
            self.mc._conn.commit()  # 数据表内容有更新,必须使用到该语句
            #print(self.mc._cursor.rowcount, "条记录插入成功。")
            time_end = time.time()  # 记录结束时间
            time_sum = time_end - time_start  # 计算的时间差为程序的执行时间,单位为秒/s
            print(time_sum)

        self.mc.close()



# 生成CRC16-MODBUS校验码
def crc16Add(read):
    crc16 = crcmod.mkCrcFun(0x18005, rev=True, initCrc=0xFFFF, xorOut=0x0000)
    data = read.replace(" ", "")  # 消除空格
    readcrcout = hex(crc16(unhexlify(data))).upper()
    str_list = list(readcrcout)
    # print(str_list)
    if len(str_list) == 5:
        str_list.insert(2, '0')  # 位数不足补0,因为一般最少是5个
    crc_data = "".join(str_list)  # 用""把数组的每一位结合起来  组成新的字符串
    # print(crc_data)
    read = read.strip() + ' ' + crc_data[4:] + ' ' + crc_data[2:4]  # 把源代码和crc校验码连接起来
    # print('CRC16校验:', crc_data[4:] + ' ' + crc_data[2:4])
    # print(read)
    return read

# 十进制地址转换
def ZhuanHuan(wave_address):
    wave_address_hex = str(hex(wave_address)[2:4]) + ' ' + str(hex(wave_address)[4:6])
    # print('dizhi' + wave_address_hex)
    return wave_address_hex

def GetApm0(data_bytes):
    value = struct.unpack('>H', data_bytes[3:5])[0]
    print(data_bytes)
    print(data_bytes[3:5])
    if value <= 32767:
        Apm0 = value / 100
    else:
        Apm0 = (value - 65536) / 100
    return Apm0





if __name__ == '__main__':
    cgq1 = cgq(1,COM='COM17')
    while True:
        cgq1.JieShouBoxing()
<p>I recently ran into some problems: I used the time.time function to calculate the sensor data acquisition function I wrote myself, and a run took about 0.3 seconds. I want it to run continuously and collect, so I'm doing this while True loop. But when I looked at the collection time recorded in the database, I found that the collection time interval was between 1 and 2 seconds, I did not understand why there was such a big difference
I want to know how I can fix this problem and increase the speed, because I have increased the function to 0.3 seconds, but finally the actual interval is still 1 or 2 seconds and I am frustrated
Here is the full code:</p>
<pre><code>import struct
import dill as pickle
import serial.tools.list_ports
from binascii import *
import crcmod
import serial
import queue
from pyecharts import options as opts
from pyecharts.charts import Pie, Line, Grid
from multiprocessing import Process, Event, Pool, Manager
from DBUtils.PooledDB import PooledDB
import pymysql
import time
from pyecharts.globals import CurrentConfig

class MysqlClient(object):
__pool = None

def __init__(self, mincached=10, maxcached=20, maxshared=10, maxconnections=200, blocking=True,
maxusage=100, setsession=None, reset=True,
host='localhost', port=3306, db='gzjc_db',
user='root', passwd='password', charset='utf8mb4'):
"""

:param mincached:连接池中空闲连接的初始数量
:param maxcached:连接池中空闲连接的最大数量
:param maxshared:共享连接的最大数量
:param maxconnections:创建连接池的最大数量
:param blocking:超过最大连接数量时候的表现,为True等待连接数量下降,为false直接报错处理
:param maxusage:单个连接的最大重复使用次数
:param setsession:optional list of SQL commands that may serve to prepare
the session, e.g. ["set datestyle to ...", "set time zone ..."]
:param reset:how connections should be reset when returned to the pool
(False or None to rollback transcations started with begin(),
True to always issue a rollback for safety's sake)
:param host:数据库ip地址
:param port:数据库端口
:param db:库名
:param user:用户名
:param passwd:密码
:param charset:字符编码
"""

if not self.__pool:
self.__class__.__pool = PooledDB(pymysql,
mincached, maxcached,
maxshared, maxconnections, blocking,
maxusage, setsession, reset,
host=host, port=port, db=db,
user=user, passwd=passwd,
charset=charset,
cursorclass=pymysql.cursors.DictCursor
)
# self._conn = None
# self._cursor = None
# self.__get_conn()
self._conn = self.__pool.connection()
self._cursor = self._conn.cursor()

def __get_conn(self):
self._conn = self.__pool.connection()
self._cursor = self._conn.cursor()

def close(self):
try:
self._cursor.close()
self._conn.close()
except Exception as e:
print(e)

def __execute(self, sql, param=()):
count = self._cursor.execute(sql, param)
print(count)
return count

def begin(self):
"""开启事务"""
self._conn.autocommit(0)

def end(self, option='commit'):
"""结束事务"""
if option == 'commit':
self._conn.autocommit()
else:
self._conn.rollback()

class cgq():
def __init__(self, Cganqi, COM):
# 实例
self.Cganqi = Cganqi
self.mc = MysqlClient()
self.mc._cursor.execute('SELECT * FROM cgqshebeixinxi WHERE id =' + str(self.Cganqi))
myresult = self.mc._cursor.fetchall()
print(myresult)
shebeilist = []
for x in myresult[0].values():
shebeilist.append(x)
self.sbmc = shebeilist[1]
self.sblx = shebeilist[2]
self.ssqy = shebeilist[3]
self.ckxx = shebeilist[4]
# host可以改成地址信息
# COM=self.ckxx
self.mc.close()
# 数据
self.qX = queue.Queue(maxsize=0)
self.qY = queue.Queue(maxsize=0)
self.qZ = queue.Queue(maxsize=0)
# 数据
self.qX1 = queue.Queue(maxsize=0)
self.qY1 = queue.Queue(maxsize=0)
self.qZ1 = queue.Queue(maxsize=0)
# 打开串口
self.ser = serial.Serial(COM, 9600, timeout=1)
self.ser.set_buffer_size(rx_size=12800, tx_size=12800)
# 数据库
self.time1 = []
self.liedux = []
self.lieduy = []
self.lieduz = []
self.wendu = []
self.time2 = []
self.boxingx = []
self.boxingy = []
self.boxingz = []
self.errorresult_tz = []
self.errorresult_bx = []

def ChuanRuCGQ(self):
# temp_send = '01 04 00 1C 00 01 F0 0C'
global buffer_data1
temp_send = '01 04 00 10 00 10 F0 03'
# 发送的数据转为2进制b'\x01\x03\x00\x00\x00\x02\xc4\x0b'
temp_send = bytes.fromhex(temp_send)
#print(temp_send)
#time_start = time.time() # 记录开始时间
if self.ser.is_open:
self.mc.__init__()
#print('串口已打开')
self.ser.write(temp_send)
# self.ser.timeout = 1
try:
buffer_data1 = self.ser.read(37)
#print(buffer_data1)
except serial.SerialTimeoutException:
pass
# 使用 struct 模块解析字节数据
wendu = struct.unpack('>H', buffer_data1[27:29])[0] / 10
liedux = struct.unpack('>H', buffer_data1[15:17])[0] / 100
lieduy = struct.unpack('>H', buffer_data1[17:19])[0] / 100
lieduz = struct.unpack('>H', buffer_data1[19:21])[0] / 100
#print("当前温度为:", wendu)
#print("当前烈度x为:", liedux)
#print("当前烈度y为:", lieduy)
#print("当前烈度z为:", lieduz)
sql = "INSERT INTO ChuanGanQi" + str(self.Cganqi) + "(时间, 温度, 烈度x, 烈度y, 烈度z) VALUES (%s, %s, %s, %s, %s)"
# 时间,数字量要加str
localtimes = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
val = (localtimes, str(wendu), str(liedux), str(lieduy), str(lieduz))
self.mc._cursor.execute(sql, val)
self.mc._conn.commit() # 数据表内容有更新,必须使用到该语句
#print(self.mc._cursor.rowcount, "条记录插入成功。")
self.mc.close()
#time_end = time.time() # 记录结束时间
#time_sum = time_end - time_start # 计算的时间差为程序的执行时间,单位为秒/s
#print(time_sum)


def JieShouBoxing(self):

time_start = time.time() # 记录开始时间
global buffer_data2
self.mc.__init__()
# 发送标志位
temp_send1 = '01 06 27 10 00 01'
temp_send1 = crc16Add(temp_send1)
# print(temp_send1)
temp_send1 = bytes.fromhex(temp_send1)
# print(temp_send1)
if self.ser.is_open:
#print('串口已打开')
self.ser.write(temp_send1)
#else:
#print('串口未打开')
# 100毫秒延时
time.sleep(0.1)
# 查看状态码
temp_send2 = '01 03 27 10 00 01'
temp_send2 = crc16Add(temp_send2)
# print(temp_send2)
temp_send2 = bytes.fromhex(temp_send2)
self.ser.write(temp_send2)
try:
buffer_data2 = self.ser.read(15)
# buffer_data = ser.readline()
except serial.SerialTimeoutException:
pass
return_data_hex = str(buffer_data2.hex())
# print(return_data_hex)
# print(return_data_hex[20:22])
jud = return_data_hex[20:22]
# 不应该读取80个 记得改
if jud == '02':
# 从文件中加载预先计算好的数据
with open('send_data.pkl', 'rb') as f:
send_data_list = pickle.load(f)
for temp_send3X, temp_send3Y, temp_send3Z in send_data_list:
self.ser.write(temp_send3X)
try:
buffer_data3X = self.ser.read(7)
#buffer_data3X = self.ser.readline()
#print(buffer_data3X)
#print('xxxxx')
#return_data_hexX = str(buffer_data3X.hex())
#print('返回的数据转换为16进制:', return_data_hexX)
# 这是存入队列
#self.qX.put(return_data_hexX)
#self.qX1.put(buffer_data3X)

except serial.SerialTimeoutException:
pass
self.ser.write(temp_send3Y)
try:
buffer_data3Y = self.ser.read(7)
#return_data_hexY = str(buffer_data3Y.hex())
#print('返回的数据转换为16进制:', return_data_hexY)
# 这是存入队列
#self.qY.put(return_data_hexY)
#self.qY1.put(buffer_data3Y)
except serial.SerialTimeoutException:
pass
self.ser.write(temp_send3Z)
try:
buffer_data3Z = self.ser.read(7)
#return_data_hexZ = str(buffer_data3Z.hex())
#print('返回的数据转换为16进制:', return_data_hexZ)
# 这是存入队列
#self.qZ.put(return_data_hexZ)
#self.qZ1.put(buffer_data3Z)
except serial.SerialTimeoutException:
pass
temp_send4 = '01 06 27 10 00 00'
temp_send4 = crc16Add(temp_send4)
temp_send4 = bytes.fromhex(temp_send4)
self.ser.write(temp_send4)
#x_data = GetApm(self.qX.get())
#y_data = GetApm(self.qY.get())
#z_data = GetApm(self.qZ.get())
x_data1 = GetApm0(buffer_data3X)
y_data1 = GetApm0(buffer_data3Y)
z_data1 = GetApm0(buffer_data3Z)


sql = "INSERT INTO ChuanGanQiBoxing" + str(self.Cganqi) + " (时间, x波形, y波形, z波形) VALUES (%s, %s, %s, %s)"
# 时间,数字量要加str
localtimes = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
#print(localtimes)
val = (localtimes, str(x_data1), str(y_data1), str(z_data1))
print(localtimes, str(x_data1), str(y_data1), str(z_data1))
self.mc._cursor.execute(sql, val)
self.mc._conn.commit() # 数据表内容有更新,必须使用到该语句
#print(self.mc._cursor.rowcount, "条记录插入成功。")
time_end = time.time() # 记录结束时间
time_sum = time_end - time_start # 计算的时间差为程序的执行时间,单位为秒/s
print(time_sum)

self.mc.close()



# 生成CRC16-MODBUS校验码
def crc16Add(read):
crc16 = crcmod.mkCrcFun(0x18005, rev=True, initCrc=0xFFFF, xorOut=0x0000)
data = read.replace(" ", "") # 消除空格
readcrcout = hex(crc16(unhexlify(data))).upper()
str_list = list(readcrcout)
# print(str_list)
if len(str_list) == 5:
str_list.insert(2, '0') # 位数不足补0,因为一般最少是5个
crc_data = "".join(str_list) # 用""把数组的每一位结合起来 组成新的字符串
# print(crc_data)
read = read.strip() + ' ' + crc_data[4:] + ' ' + crc_data[2:4] # 把源代码和crc校验码连接起来
# print('CRC16校验:', crc_data[4:] + ' ' + crc_data[2:4])
# print(read)
return read

# 十进制地址转换
def ZhuanHuan(wave_address):
wave_address_hex = str(hex(wave_address)[2:4]) + ' ' + str(hex(wave_address)[4:6])
# print('dizhi' + wave_address_hex)
return wave_address_hex

def GetApm0(data_bytes):
value = struct.unpack('>H', data_bytes[3:5])[0]
print(data_bytes)
print(data_bytes[3:5])
if value <= 32767:
Apm0 = value / 100
else:
Apm0 = (value - 65536) / 100
return Apm0





if __name__ == '__main__':
cgq1 = cgq(1,COM='COM17')
while True:
cgq1.JieShouBoxing()
</code></pre>
 
Top