python串口通信迟缓_python串口通信

本文介绍了Python进行串口通信时的数据编码、解码过程,包括JSON转换和CRC16Kermit校验。通过详细讲解CRC16Kermit的计算方法,帮助理解数据的正确性和可靠性。
摘要由CSDN通过智能技术生成

python串口通信

1 编码:

def bytes(dataDict):

strBody = json.dumps(dataDict) # 将dict 数据妆化为字符串

sendBuf = bytearray()

sendBuf += ('%04X' % len(strBody)).encode()

sendBuf += strBody.encode() # 将字符串转化为字节数组

crcVal = CRC16Kermit().calculate(str(strBody)) sendBuf += crcVal.encode() return sendBuf

2 解码:

msg = json.loads(body.decode('utf-8')) # 先将字节数组解码为字符串 , JSON编码的字符串转换回一个Python数据结构

3 crc16:

from ctypes import c_ushort

class CRC16Kermit(object):

crc16kermit_tab = []

crc16Kermit_constant = 0x8408

def __init__(self):

if not len(self.crc16kermit_tab): self.init_crc16kermit() # initialize the precalculated tables

def calculate(self, string=''):

try:

if not isinstance(string, str): raise Exception("Please provide a string as argument for calculation.")

if not string: return 0

crcValue = 0x0000

for c in string:

tmp = crcValue ^ ord(c)

crcValue = c_ushort(crcValue 8).value ^ int(self.crc16kermit_tab[(tmp 0x00ff)], 0)

# After processing, the one's complement of the CRC is calcluated and the

# two bytes of the CRC are swapped.

low_byte = (crcValue 0xff00) 8

high_byte = (crcValue 0x00ff) 8

crcValue = low_byte | high_byte

return "%04X" % crcValue

except:

print("calculate fail")

def init_crc16kermit(self):

'''The algorithm use tables with precalculated values'''

for i in range(0, 256):

crc = c_ushort(i).value

for j in range(0, 8):

if (crc 0x0001):

crc = c_ushort(crc 1).value ^ self.crc16Kermit_constant

else:

crc = c_ushort(crc 1).value

self.crc16kermit_tab.append(hex(crc))

python串口通信 相关文章

python之异常

1 ''' 2 异常的概念 :代码产生错误,无法继续执行 3 异常的产生 4 异常的捕获与处理 5 函数调用栈 6 自定义异常 7 ''' 8 #导入模块,可以打印出报错的详细信息 9 # import traceback10 #11 # # 异常的产生, 解决方法:1.if 过滤(这个方法适合提前已知可能出

Selenium 3 自动化测试实战-基于 Python 语言之【爬取比特币最新价格USD】

这个涉及到python中的【函数】、【方法】和【异地文件调用】 需要将第三章中的3.4【列表、元组与字典】、3.5【函数、类与方法】、3.6【模组】全部看一遍才能继续往下面走 下面我将分成两个文件放在不同的文件夹去执行程序。 需求:访问https网址,根据接口返

python之列表与元组

1 #列表 2 # 列表也是一种序列类型,代表列表里的元素也是有下标索引的(可以用下标,切片),列表里面的元素可以是任意类型的 3 # 4 # alist = ['a',1,2,[100,200]] 5 # print(alist) 6 #获取嵌套列表里面的下标为1的元素,多种方式 7 # print(alist[3][1])

python之对象与变量

1 #python语言里,一切数据都是对象 2 # type()#查看对象类型 3 # 常见整形 :int 4 #浮点型:3.1415 ,有精度要求,16位 5 6 # 如果只想运行某一部分代码,可以选中需要运行的那部分代码,右键→选择Execute line in python console,在python解释中运行 7

python之模块与包

1 #模块与包使用 2 #使用标准库 3 #标准库是python安装包里面提供的功能模块和包 4 # 比如:内置函数 :len print int import 5 #功能模块:包含程序设计所需要的常用的功能,用import导入就行3 6 #获取当前时间 ,内置模块 7 import time 8 print(time.strf

python之循环嵌套与算法

1 #循环嵌套 2 # 循环嵌套就是先把外层循环执行一次,内部循环全部执行一次,外部循环有三个值的话,内部循环要全部执行3遍 3 4 5 #例子 6 # beforetax = [10000,9000,5000,3000] 7 # aftertax = [] 8 # for one in beforetax: 9 # aftertax.append(one*0.9)

python之循环语句与注释

1 #while循环 2 #for循环 3 #break语句 4 #continue语句 5 # 注释 6 #return 是函数结束语句 7 #break 是循环结束的语句 8 9 #while 求和 1-100的和10 #求和函数11 # def get_sum():12 # sum = 0 #结果和13 # cnt = 1 #从1开始累积14 # while cnt = 100:15 #

python之字符串格式化方法

1 #字符串格式化方法一 ,是为了更好的表达字符串,往字符串里传递参数 2 # print里面的变量名不需要加引号 3 # 使用场景:requests.post(data = infoData ) 4 #字符串格式化方法一 %s %d %f %x 5 6 # 小明的年龄 7 # myAge = 19 8 # print("小明的年龄是%d"

python之对象的方法

1 #字符串方法 2 #列表的方法 3 #查看python手册 4 5 #对象方法的概念 :对象方法,其实就是属于这个对象的函数 6 7 #字符串方法 格式: 对象.方法 8 # count 方法 ,计算字符串中包含的多少个指定的字符个数,如果字符串中没有对应的返回值,只会返回0 9 fro

python之函数

1 #函数 2 # print('helloword') 3 # print('helloword') 4 # print('helloword') 5 6 7 # #定义一个函数,函数必须先定义再执行,光定义不会执行代码 8 # def func(): 9 # print('helloword')10 # print('helloword')11 # print('helloword')12 # print('he

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值