• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python ustruct.pack_into函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: wait_msg

 def wait_msg(self):
     res = self.sock.read(1)
     self.sock.setblocking(True)
     if res is None:
         return None
     if res == b"":
         raise OSError(-1)
     if res == b"\xd0":  # PINGRESP
         sz = self.sock.read(1)[0]
         assert sz == 0
         return None
     op = res[0]
     if op & 0xf0 != 0x30:
         return op
     sz = self._recv_len()
     topic_len = self.sock.read(2)
     topic_len = (topic_len[0] << 8) | topic_len[1]
     topic = self.sock.read(topic_len)
     sz -= topic_len + 2
     if op & 6:
         pid = self.sock.read(2)
         pid = pid[0] << 8 | pid[1]
         sz -= 2
     msg = self.sock.read(sz)
     self.cb(topic, msg)
     if op & 6 == 2:
         pkt = bytearray(b"\x40\x02\0\0")
         struct.pack_into("!H", pkt, 2, pid)
         self.sock.write(pkt)
     elif op & 6 == 4:
         assert 0
开发者ID:stinos,项目名称:micropython-lib,代码行数:31,代码来源:mqtt.py


示例2: publish

 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:stinos,项目名称:micropython-lib,代码行数:34,代码来源:mqtt.py


示例3: publish

 def publish(self, topic, msg, retain=False, qos=0):
     pkt = bytearray(b"\x30\0\0")
     pkt[0] |= qos << 1 | retain
     sz = 2 + len(topic) + len(msg)
     if qos > 0:
         sz += 2
     assert sz <= 16383
     pkt[1] = (sz & 0x7f) | 0x80
     pkt[2] = sz >> 7
     #print(hex(len(pkt)), hexlify(pkt, ":"))
     self.sock.send(pkt)
     self._send_str(topic)
     if qos > 0:
         self.pid += 1
         pid = self.pid
         buf = bytearray(b"\0\0")
         struct.pack_into("!H", buf, 0, pid)
         self.sock.send(buf)
     self.sock.send(msg)
     if qos == 1:
         while 1:
             op = self.wait_msg()
             if op == 0x40:
                 sz = self.sock.recv(1)
                 assert sz == b"\x02"
                 rcv_pid = self.sock.recv(2)
                 rcv_pid = rcv_pid[0] << 8 | rcv_pid[1]
                 if pid == rcv_pid:
                     return
     elif qos == 2:
         assert 0
开发者ID:Jonty,项目名称:Mk3-Firmware,代码行数:31,代码来源:mqtt.py


示例4: _register_char

    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:radiumray,项目名称:mdxly,代码行数:7,代码来源:mpu9250.py


示例5: _register_short

    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:radiumray,项目名称:mdxly,代码行数:7,代码来源:mpu9250.py


示例6: subscribe

 def subscribe(self, topic):
     print("subscribe")
     pkt = bytearray(b"\x82\0\0\0")
     self.pid += 1
     # B=1 byte; H = 2 bytes,!=network or big endian and offset is 1
     struct.pack_into("!BH", pkt, 1, 2 + 2 + len(topic) + 1, self.pid)
     self.sock.send(pkt)
     self.send_str(topic.encode('utf-8'))
     self.sock.send(b"\0")
     resp = self.sock.recv(5)
     print(resp)
     assert resp[0] == 0x90
     assert resp[2] == pkt[2] and resp[3] == pkt[3]
     assert resp[4] == 0
开发者ID:slzatz,项目名称:miscellaneous,代码行数:14,代码来源:umqtt_client.py


示例7: subscribe

 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.send(pkt)
     self._send_str(topic)
     self.sock.send(qos.to_bytes(1))
     resp = self.sock.recv(5)
     #print(resp)
     assert resp[0] == 0x90
     assert resp[2] == pkt[2] and resp[3] == pkt[3]
     if resp[4] == 0x80:
         raise MQTTException(resp[4])
开发者ID:Jonty,项目名称:Mk3-Firmware,代码行数:15,代码来源:mqtt.py


示例8: subscribe

 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)
  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)
    assert resp[1]==pkt[2]and resp[2]==pkt[3]
    if resp[3]==0x80:
     raise MQTTException(resp[3])
    return
开发者ID:radiumray,项目名称:mdxly,代码行数:16,代码来源:simple.py


示例9: _fcmd2b

 def _fcmd2b(self, fmt, a0, a1, a2, a3, a4=0):
     buf = self.buf[calcsize(fmt)]
     pack_into(fmt, buf, 0, 2, a0, a1, a2, a3, a4)
     self._send(buf)
开发者ID:fabaff,项目名称:micropython,代码行数:4,代码来源:lcd160cr.py


示例10: _fcmd2

 def _fcmd2(self, fmt, a0, a1=0, a2=0):
     buf = self.buf[calcsize(fmt)]
     pack_into(fmt, buf, 0, 2, a0, a1, a2)
     self._send(buf)
开发者ID:fabaff,项目名称:micropython,代码行数:4,代码来源:lcd160cr.py


示例11: set_scroll_win

 def set_scroll_win(self, win, x=-1, y=0, w=0, h=0, vec=0, pat=0, fill=0x07e0, color=0):
     pack_into('<BBBHHHHHHHH', self.buf19, 0, 2, 0x55, win, x, y, w, h, vec, pat, fill, color)
     self._send(self.buf19)
开发者ID:fabaff,项目名称:micropython,代码行数:3,代码来源:lcd160cr.py


示例12: set_spi_win

 def set_spi_win(self, x, y, w, h):
     pack_into('<BBBHHHHHHHH', self.buf19, 0, 2, 0x55, 10, x, y, x + w - 1, y + h - 1, 0, 0, 0, 0xffff)
     self._send(self.buf19)
开发者ID:fabaff,项目名称:micropython,代码行数:3,代码来源:lcd160cr.py


示例13: print

    print('TypeError')

# make sure that unknown types are detected
try:
    struct.pack("z", 1)
except:
    print("Unknown type")

# Initially repitition counters were supported only for strings,
# but later were implemented for all.
print(struct.unpack("<3B2h", b"foo\x12\x34\xff\xff"))
print(struct.pack("<3B", 1, 2, 3))

# pack_into
buf = bytearray(b'>>>123<<<')
struct.pack_into('<bbb', buf, 3, 0x41, 0x42, 0x43)
print(buf)
struct.pack_into('<bbb', buf, -6, 0x44, 0x45, 0x46)
print(buf)

try:
    struct.pack_into('<bbb', buf, 7, 0x41, 0x42, 0x43)
except:
    print('struct.error')
try:
    struct.pack_into('<bbb', buf, -10, 0x41, 0x42, 0x43)
except:
    print('struct.error')

# unpack_from
buf = b'0123456789'
开发者ID:fabaff,项目名称:micropython,代码行数:31,代码来源:struct1.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python ustruct.unpack函数代码示例发布时间:2022-05-27
下一篇:
Python ustruct.pack函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap