2021-05-24 Hikvision DS-TVL224-4-5Y Python 显示部分驱动代码

Hikvision DS-TVL224-4-5Y Python 显示部分驱动代码

import socket
from threading import Thread

thread_lock = False

"""
对接海康威视 DS-TVL224-4-5Y 64*64
4行,每行可以显示4个汉字
"""
def receive_thread(s: socket):
    while thread_lock:
        # get the socket send buffer size and receive buffer size
        # s_send_buffer_size = s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
        s_receive_buffer_size = s.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)
        # print("client TCP send buffer size is %d" % s_send_buffer_size)
        print("client TCP receive buffer size is %d" % s_receive_buffer_size)
        if 0 != s_receive_buffer_size:
            msg = s.recv(s_receive_buffer_size)
            print("received data: %s", msg.decode('utf-8'))
            # print("recv len is : [%d]" % len(msg))


def close_socket(s: socket):
    global thread_lock
    thread_lock = False
    s.close()
    print("socket closed!")


def start_tcp_client(ip: str, port: int):
    global thread_lock
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        print("start connect to server...")
        s.connect((ip, port))
        thread_lock = True
        send_data_to_screen(s, "测试数据", 1)
    except socket.error:
        print("fail to connect to server!")
    return s


def list_split(items, n):
    """
    每隔指定位数切割字符串,返回子字符串数组
    :param items: 字符串
    :param n: 间隔位数
    :return: 切割后的字符串列表
    """
    return [items[i:i + n] for i in range(0, len(items), n)]


def get_length_4list(x_len: int):
    """
    输入数字,输出四位长度的大端列表
    :param x_len: 数字
    :return: 四位长度的大端列表
    """
    xx = '{:08x}'.format(x_len)
    after_split = list_split(xx, 2)
    after_split.reverse()
    return [int(x, 16) for x in after_split]


def send_data_to_screen(s: socket, data: str, color: int = 1):
    cmd_text = list()
    cmd_text.append([0x55, 0xAA, 0x00, 0x00, 0x01, 0x00, 0x00, 0xDA, 0x00, 0x00])  # 数据头
    cmd_text.append([0x3D, 0x00, 0x00, 0x00])  # 1 包长度(数据长度 + 51)
    cmd_text.append([0x01, 0x01])
    cmd_text.append([0x3D, 0x00, 0x00, 0x00])  # 3 帧长度(数据长度 + 51)
    cmd_text.append([0x01, 0x01])
    cmd_text.append([0x3C, 0x00, 0x00, 0x00])  # 5 节目长度(数据长度 + 50)
    cmd_text.append([0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                     0x00, 0x00, 0x00, 0x00, 0x01])
    cmd_text.append([0x24, 0x00, 0x00, 0x00])  # 7 区域数据长度 (数据长度 + 26)
    cmd_text.append([0x0E, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x3F, 0x00])
    cmd_text.append([color])  # 9 颜色 01红 02绿 03黄
    cmd_text.append([0x00, 0x00, 0x01, 0xFF, 0x01, 0x01, 0x10])
    cmd_text.append([0x0A, 0x00, 0x00, 0x00])  # 11 数据长度 低位在前
    cmd_text.append([0xD2, 0xBB, 0xC2, 0xB7, 0xC6, 0xBD, 0xB0, 0xB2, 0x38, 0x36])  # 数据内容
    cmd_text.append([0x00, 0x00, 0x0D, 0x0A])  # 13 帧尾

    cmd_text[12] = list(bytes(data.encode('GB2312')))
    text_length = len(cmd_text[12])

    cmd_text[11] = get_length_4list(text_length)
    cmd_text[7] = get_length_4list(text_length + 26)
    cmd_text[5] = get_length_4list(text_length + 50)
    cmd_text[1] = cmd_text[3] = get_length_4list(text_length + 51)

    cmd_data = list()
    for index, item in enumerate(cmd_text):
        cmd_data.extend(item)
    s.send(bytes(cmd_data))


if __name__ == '__main__':
    x_socket = start_tcp_client('192.168.8.174', 10000)
    # t = Thread(target=receive_thread, args=(x_socket,))
    # t.start()
    # t.join()

你可能感兴趣的:(2021-05-24 Hikvision DS-TVL224-4-5Y Python 显示部分驱动代码)