相關(guān)推薦
您可能對(duì)下面課程感興趣
Python上位機(jī)開發(fā)教程

105小節(jié)已有5471人學(xué)過(guò)

Python上位機(jī)開發(fā)位運(yùn)算案例
發(fā)布時(shí)間:2024-11-20 09:23 [ 我要自學(xué)網(wǎng)原創(chuàng) ] 發(fā)布人: 快樂(lè)小女 閱讀: 50

import sys
from snap7 import client
from PySide6.QtWidgets import *
from PySide6.QtUiTools import loadUiType


def connect_plc() -> client.Client:
    plc = client.Client()
    plc.set_connection_type(3)
    plc.connect("192.168.5.20", 0, 1)
    return plc
def write_to_plc(start_addr, byte: bytes):
    plc = connect_plc()
    plc.db_write(1, start_addr, byte)
    plc.disconnect()
def read_plc(start_addr, num) -> bytearray:
    plc = connect_plc()
    data = plc.db_read(1, start_addr, num)
    plc.disconnect()
    return data

ui_form,widget = loadUiType("./pane.ui")



class Window(widget,ui_form):
        def __init__(self):
            super().__init__()
            self.setupUi(self)

            self.byte = bytearray([0b0000_0000])


        def on_btn0_click(self,checked): #v0.0
            num = 1<<0
            if checked:
                # self.byte[0] = self.byte[0] | 0b0000_0001
                self.byte[0] = self.byte[0]+num
            else:
                # self.byte[0] = self.byte[0] & 0b1111_1110
                self.byte[0] = self.byte[0]-num

            write_to_plc(0,self.byte)


        def on_btn1_click(self,checked):  #v0.1
            num = 1<<1
            if checked:
                # self.byte[0] = self.byte[0] | 0b0000_0010   #0x02
                self.byte[0] = self.byte[0] +num

            else:
                # self.byte[0] = self.byte[0] & 0b1111_1101   #0xfc
                self.byte[0] = self.byte[0]-num

            write_to_plc(0, self.byte)

        def on_btn2_click(self,checked):  #v0.2
            if checked:
                self.byte[0] = self.byte[0] | 0b0000_0100  # 0x04
            else:
                self.byte[0] = self.byte[0] & 0b1111_1011  # 0xfb

            write_to_plc(0, self.byte)


        def on_btn3_click(self,checked):  #v0.3
            if checked:
                self.byte[0] = self.byte[0] | 0b0000_1000  # 0x08
            else:
                self.byte[0] = self.byte[0] & 0b1111_0111  # 0xf7

            write_to_plc(0, self.byte)

        def on_btn4_click(self,checked): #0.4
            if checked:
                self.byte[0] = self.byte[0] | 0b0001_0000  # 0x10
            else:
                self.byte[0] = self.byte[0] & 0b1110_1111  # 0xef

            write_to_plc(0, self.byte)


        def on_btn5_click(self,checked): #v0.5
            if checked:
                self.byte[0] = self.byte[0] | 0b0010_0000  # 0x20
            else:
                self.byte[0] = self.byte[0] & 0b1101_1111  # 0xcf

            write_to_plc(0, self.byte)


        def on_btn6_click(self,checked): #v0.6
            if checked:
                self.byte[0] = self.byte[0] | 0b0100_0000  # 0x40
            else:
                self.byte[0] = self.byte[0] & 0b1011_1111  # 0xbf

            write_to_plc(0, self.byte)

        def on_btn7_click(self,checked):
            if checked:
                self.byte[0] = self.byte[0] | 0b1000_0000  # 0x80
            else:
                self.byte[0] = self.byte[0] & 0b0111_1111  # 0x7f

            write_to_plc(0, self.byte)



if __name__ == '__main__':

    app = QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec())







Python上位機(jī)開發(fā)教程
我要自學(xué)網(wǎng)商城 ¥80 元
進(jìn)入購(gòu)買
文章評(píng)論
0 條評(píng)論 按熱度排序 按時(shí)間排序 /350
添加表情
遵守中華人民共和國(guó)的各項(xiàng)道德法規(guī),
承擔(dān)因您的行為而導(dǎo)致的法律責(zé)任,
本站有權(quán)保留或刪除有爭(zhēng)議評(píng)論。
參與本評(píng)論即表明您已經(jīng)閱讀并接受
上述條款。
V
特惠充值
聯(lián)系客服
APP下載
官方微信
返回頂部
相關(guān)推薦
您可能對(duì)下面課程感興趣
Python上位機(jī)開發(fā)教程

105小節(jié)已有5471人學(xué)過(guò)

分類選擇:
電腦辦公 平面設(shè)計(jì) 室內(nèi)設(shè)計(jì) 室外設(shè)計(jì) 機(jī)械設(shè)計(jì) 工業(yè)自動(dòng)化 影視動(dòng)畫 程序開發(fā) 網(wǎng)頁(yè)設(shè)計(jì) 會(huì)計(jì)課程 興趣成長(zhǎng) AIGC