多线程仿三菱

from threading import Thread
import serial
from time import sleep
from threading import Thread
import cv2

port= serial.Serial(“COM1”,9600,parity=serial.PARITY_EVEN,stopbits= serial.STOPBITS_ONE,bytesize=7)
camera = cv2.VideoCapture(0)
img = None
datas = [0x02, 0x30, 0x30, 0x31, 0x31, 0x46, 0x30, 0x31, 0x03, 0x36, 0x43]
def Sum_Check(datas):
if (len(datas) ==0):
return False
sum = 0
for i in range(1,len(datas)-2):
sum+= datas[i]
sum %=256
sum_h = sum//16
sum_l = sum%16
sumcheck_H = 0
sumcheck_L = 0
if sum_h<10:
sumcheck_H = sum_h+0x30
else:
sumcheck_H = sum_h + 0x37
if sum_l<10:
sumcheck_L = sum_l +0x30
else:
sumcheck_L = sum_l + 0x37

if sumcheck_H != datas[len(datas)-2]:
    return False

if sumcheck_L != datas[len(datas)-1]:
    return False
return True

'''
  public static bool check(byte[] data)
    {
        if (data.Length == 0) return false;
        if (data == null) return false;
        int sum = 0;
        for (int i = 1; i < data.Length - 2; i++)
        {
            sum += data[i];
        }
        sum %= 256;
        
        int sum_H = sum / 16;
        int sum_L = sum % 16;
        int newsum_h = sum_H < 10 ? sum_H + 0x30 : sum_H + 0x37;
        int newsum_l = sum_L < 10 ? sum_L + 0x30 : sum_L + 0x37;

        if (newsum_h != data[data.Length - 2] || newsum_l != data[data.Length - 1])
        {
            return false;
        }
        return true;
    }
:return: 
'''

def savepicture():
pass
def main():
if port.isOpen():
port.close()
port.open()
while True:
# print(Thread.name)
if (cv2.waitKey(10) & 0xff) ==ord(‘s’):

        name = input("请输入端口号")
        if port.isOpen():
            port.close()
            port.name=name
            port.open()
    port.write(datas)
    sleep(1)
    datas_Receive = port.read(6)
    str1 = ""
    for data in datas_Receive:
        str1+=" 0x"
        str1+= str(data)
    # str1 = " 0x".join(datas_Receive)
    isright = Sum_Check(datas_Receive)
    if isright:
        print("数据正确 " + str1)
        value_h = 0
        value_l = 0
        if datas_Receive[1]<0x39:
            value_h = datas_Receive[1]-0x30
        else:
            value_h = datas_Receive[1] - 0x37

        if datas_Receive[2]<0x39:
            value_l = datas_Receive[2]-0x30
        else:
            value_l = datas_Receive[2] - 0x37
        intreceive = value_h*16+value_l
        if(intreceive & 0x08)==0x08:

            print('开始气泡检测')
            cv2.imwrite("1.jpg",img)

        '''
        int value_h = data[1]<=0x39? data[1] - 0x30:data[1] -0x37;
        int value_l = data[2] <= 0x39 ? data[2] - 0x30 : data[2] - 0x37;
        return value_h * 16 + value_l;

        '''


    else:
        print("wrong")

    # print(str1)
    # print(datas_Receive)

if name == ‘main’:
thread1 =Thread(target=main)

thread1.start()
# thread1.join()
ret,frame = camera.read()
while True:
    ret,frame = camera.read()
    img = cv2.flip(frame,1)
    cv2.imshow("",img)
    cv2.waitKey(10)
# main()

猜你喜欢

转载自blog.csdn.net/tel_1392/article/details/113785250