当前位置: 首页>>代码示例>>Python>>正文


Python ustruct.pack_into方法代码示例

本文整理汇总了Python中ustruct.pack_into方法的典型用法代码示例。如果您正苦于以下问题:Python ustruct.pack_into方法的具体用法?Python ustruct.pack_into怎么用?Python ustruct.pack_into使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ustruct的用法示例。


在下文中一共展示了ustruct.pack_into方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _publish

# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import pack_into [as 别名]
def _publish(self, topic, msg, retain, qos, dup, pid):
        pkt = bytearray(b"\x30\0\0\0")
        pkt[0] |= qos << 1 | retain | dup << 3
        sz = 2 + len(topic) + len(msg)
        if qos > 0:
            sz += 2
        if sz >= 2097152:
            raise MQTTException('Strings too long.')
        i = 1
        while sz > 0x7f:
            pkt[i] = (sz & 0x7f) | 0x80
            sz >>= 7
            i += 1
        pkt[i] = sz
        await self._as_write(pkt, i + 1)
        await self._send_str(topic)
        if qos > 0:
            struct.pack_into("!H", pkt, 0, pid)
            await self._as_write(pkt, 2)
        await self._as_write(msg)

    # Can raise OSError if WiFi fails. Subclass traps 
开发者ID:peterhinch,项目名称:micropython-mqtt,代码行数:24,代码来源:mqtt_as.py

示例2: subscribe

# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import pack_into [as 别名]
def subscribe(self, topic, qos):
        pkt = bytearray(b"\x82\0\0\0")
        pid = next(self.newpid)
        self.rcv_pids.add(pid)
        struct.pack_into("!BH", pkt, 1, 2 + 2 + len(topic) + 1, pid)
        async with self.lock:
            await self._as_write(pkt)
            await self._send_str(topic)
            await self._as_write(qos.to_bytes(1, "little"))

        if not await self._await_pid(pid):
            raise OSError(-1)

    # Wait for a single incoming MQTT message and process it.
    # Subscribed messages are delivered to a callback previously
    # set by .setup() method. Other (internal) MQTT
    # messages processed internally.
    # Immediate return if no data available. Called from ._handle_msg(). 
开发者ID:peterhinch,项目名称:micropython-mqtt,代码行数:20,代码来源:mqtt_as.py

示例3: subscribe

# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import pack_into [as 别名]
def subscribe(self, topic, qos=0):
        assert self.cb is not None, "Subscribe callback is not set"
        self.sock.setblocking(True)
        pkt = bytearray(b"\x82\0\0\0")
        self.pid += 1
        struct.pack_into("!BH", pkt, 1, 2 + 2 + len(topic) + 1, self.pid)
        #print(hex(len(pkt)), hexlify(pkt, ":"))
        self.sock.write(pkt)
        self._send_str(topic)
        self.sock.write(qos.to_bytes(1, "little"))
        # print("simple --> wait socket msg")
        while 1:
            op = self.wait_msg()
            if op == 0x90:
                resp = self.sock.read(4)
                #print(resp)
                assert resp[1] == pkt[2] and resp[2] == pkt[3]
                if resp[3] == 0x80:
                    raise MQTTException(resp[3])
                # print("simple --> wait socket finish")                
                return 
开发者ID:m5stack,项目名称:UIFlow-Code,代码行数:23,代码来源:simple.py

示例4: subscribe

# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import pack_into [as 别名]
def subscribe(self, topic, qos=0):
        assert self.cb is not None, "Subscribe callback is not set"
        pkt = bytearray(b"\x82\0\0\0")
        self.pid += 1
        struct.pack_into("!BH", pkt, 1, 2 + 2 + len(topic) + 1, self.pid)
        #print(hex(len(pkt)), hexlify(pkt, ":"))
        self.sock.write(pkt)
        self._send_str(topic)
        self.sock.write(qos.to_bytes(1))
        while 1:
            op = self.wait_msg()
            if op == 0x90:
                resp = self.sock.read(4)
                #print(resp)
                assert resp[1] == pkt[2] and resp[2] == pkt[3]
                if resp[3] == 0x80:
                    raise MQTTException(resp[3])
                return

    # Wait for a single incoming MQTT message and process it.
    # Subscribed messages are delivered to a callback previously
    # set by .set_callback() method. Other (internal) MQTT
    # messages processed internally. 
开发者ID:ayoy,项目名称:upython-aq-monitor,代码行数:25,代码来源:mqtt.py

示例5: subscribe

# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import pack_into [as 别名]
def subscribe(self, topic, qos=0):
        assert self.cb is not None, "Subscribe callback is not set"
        pkt = bytearray(b"\x82\0\0\0")
        self.pid += 1
        struct.pack_into("!BH", pkt, 1, 2 + 2 + len(topic) + 1, self.pid)
        #print(hex(len(pkt)), hexlify(pkt, ":"))
        self.sock.write(pkt)
        self._send_str(topic)
        self.sock.write(qos.to_bytes(1, "little"))
        while 1:
            op = self.wait_msg()
            if op == 0x90:
                resp = self.sock.read(4)
                #print(resp)
                assert resp[1] == pkt[2] and resp[2] == pkt[3]
                if resp[3] == 0x80:
                    raise MQTTException(resp[3])
                return

    # Wait for a single incoming MQTT message and process it.
    # Subscribed messages are delivered to a callback previously
    # set by .set_callback() method. Other (internal) MQTT
    # messages processed internally. 
开发者ID:digidotcom,项目名称:xbee-micropython,代码行数:25,代码来源:simple.py

示例6: unsubscribe

# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import pack_into [as 别名]
def unsubscribe(self, topic):
        pkt = bytearray(b"\xa2\0\0\0")
        pid = next(self.newpid)
        self.rcv_pids.add(pid)
        struct.pack_into("!BH", pkt, 1, 2 + 2 + len(topic), pid)
        async with self.lock:
            await self._as_write(pkt)
            await self._send_str(topic)

        if not await self._await_pid(pid):
            raise OSError(-1)

    # Wait for a single incoming MQTT message and process it.
    # Subscribed messages are delivered to a callback previously
    # set by .setup() method. Other (internal) MQTT
    # messages processed internally.
    # Immediate return if no data available. Called from ._handle_msg(). 
开发者ID:microhomie,项目名称:microhomie,代码行数:19,代码来源:mqtt_as.py

示例7: _register_short

# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import pack_into [as 别名]
def _register_short(self, register, value=None, buf=bytearray(2)):
        if value is None:
            self.i2c.readfrom_mem_into(self.address, register, buf)
            return ustruct.unpack(">h", buf)[0]

        ustruct.pack_into(">h", buf, 0, value)
        return self.i2c.writeto_mem(self.address, register, buf) 
开发者ID:tuupola,项目名称:micropython-mpu9250,代码行数:9,代码来源:mpu6500.py

示例8: _register_char

# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import pack_into [as 别名]
def _register_char(self, register, value=None, buf=bytearray(1)):
        if value is None:
            self.i2c.readfrom_mem_into(self.address, register, buf)
            return buf[0]

        ustruct.pack_into("<b", buf, 0, value)
        return self.i2c.writeto_mem(self.address, register, buf) 
开发者ID:tuupola,项目名称:micropython-mpu9250,代码行数:9,代码来源:mpu6500.py

示例9: _register_short

# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import pack_into [as 别名]
def _register_short(self, register, value=None, buf=bytearray(2)):
        if value is None:
            self.i2c.readfrom_mem_into(self.address, register, buf)
            return ustruct.unpack("<h", buf)[0]

        ustruct.pack_into("<h", buf, 0, value)
        return self.i2c.writeto_mem(self.address, register, buf) 
开发者ID:tuupola,项目名称:micropython-mpu9250,代码行数:9,代码来源:ak8963.py

示例10: publish

# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import pack_into [as 别名]
def publish(self, topic, msg, retain=False, qos=0):
        pkt = bytearray(b"\x30\0\0\0")
        pkt[0] |= qos << 1 | retain
        sz = 2 + len(topic) + len(msg)
        if qos > 0:
            sz += 2
        assert sz < 2097152
        i = 1
        while sz > 0x7f:
            pkt[i] = (sz & 0x7f) | 0x80
            sz >>= 7
            i += 1
        pkt[i] = sz
        #print(hex(len(pkt)), hexlify(pkt, ":"))
        self.sock.write(pkt, i + 1)
        self._send_str(topic)
        if qos > 0:
            self.pid += 1
            pid = self.pid
            struct.pack_into("!H", pkt, 0, pid)
            self.sock.write(pkt, 2)
        self.sock.write(msg)
        if qos == 1:
            while 1:
                op = self.wait_msg()
                if op == 0x40:
                    sz = self.sock.read(1)
                    assert sz == b"\x02"
                    rcv_pid = self.sock.read(2)
                    rcv_pid = rcv_pid[0] << 8 | rcv_pid[1]
                    if pid == rcv_pid:
                        return
        elif qos == 2:
            assert 0 
开发者ID:m5stack,项目名称:UIFlow-Code,代码行数:36,代码来源:simple.py

示例11: _register_short

# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import pack_into [as 别名]
def _register_short(self, register, value=None, buf=bytearray(2)):
        if value is None:
            self._i2c.readfrom_mem_into(self._addr, register, buf)
            return ustruct.unpack(">h", buf)[0]

        ustruct.pack_into(">h", buf, 0, value)
        return self._i2c.writeto_mem(self._addr, register, buf) 
开发者ID:m5stack,项目名称:UIFlow-Code,代码行数:9,代码来源:_tracker.py

示例12: _register_char

# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import pack_into [as 别名]
def _register_char(self, register, value=None, buf=bytearray(1)):
        if value is None:
            self.i2c.readfrom_mem_into(_ADDR, register, buf)
            return buf[0]

        ustruct.pack_into("<b", buf, 0, value)
        return self.i2c.writeto_mem(_ADDR, register, buf) 
开发者ID:m5stack,项目名称:UIFlow-Code,代码行数:9,代码来源:_tof.py

示例13: _register_short

# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import pack_into [as 别名]
def _register_short(self, register, value=None, buf=bytearray(2)):
        if value is None:
            self.i2c.readfrom_mem_into(_ADDR, register, buf)
            return ustruct.unpack(">h", buf)[0]

        ustruct.pack_into(">h", buf, 0, value)
        return self.i2c.writeto_mem(_ADDR, register, buf) 
开发者ID:m5stack,项目名称:UIFlow-Code,代码行数:9,代码来源:_tof.py

示例14: _register_char

# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import pack_into [as 别名]
def _register_char(self, register, value=None, buf=bytearray(1)):
        if value is None:
            self.i2c.readfrom_mem_into(self.addr, register, buf)
            return buf[0]

        ustruct.pack_into("<b", buf, 0, value)
        return self.i2c.writeto_mem(self.addr, register, buf) 
开发者ID:m5stack,项目名称:UIFlow-Code,代码行数:9,代码来源:_color.py

示例15: _register_short

# 需要导入模块: import ustruct [as 别名]
# 或者: from ustruct import pack_into [as 别名]
def _register_short(self, register, value=None, buf=bytearray(2)):
        if value is None:
            self.i2c.readfrom_mem_into(self.addr, register, buf)
            return buf[1]*256 + buf[0]

        ustruct.pack_into(">h", buf, 0, value)
        return self.i2c.writeto_mem(self.addr, register, buf) 
开发者ID:m5stack,项目名称:UIFlow-Code,代码行数:9,代码来源:_color.py


注:本文中的ustruct.pack_into方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。