smartbus Package

smartbus Package

smartbus 的Python封装

包括 net 与 ipc 客户端的Python类型封装

date:2013-7-14
author:lxy@hesong.ent

_c_smartbus Module

smartbus.h 的Python封装

该模块大致实现了与 smartbus.h 一对一的 ctyps 封装。 请参考 smartbus.h

date:2013-6-1
author:lxy@hesong.ent
smartbus._c_smartbus.MAX_GLOBAL_SMART_NODE = 16

全局起始单元(节点)编号

smartbus._c_smartbus.MAX_SMARTBUS_NETCLI_UNITID_NUM = 32

net客户端值范围是16-47,全局最多32个。

smartbus._c_smartbus.MAX_SMARTBUS_NODE_CLI_NUM = 8

最大节点内的客户端数

smartbus._c_smartbus.MAX_SMARTBUS_NODE_NUM = 48

最大节点数

16 + 32 = 48 最大节点数

smartbus._c_smartbus.MIN_SMARTBUS_NETCLI_UNITID = 16

最小net客户端unitid值为16

Net客户端的unitid不能小于16

class smartbus._c_smartbus.PackInfo(lp_head_struct)

基类:builtins.object

对应 SMARTBUS_PACKET_HEAD 结构体的 ctypes 数据类型 _PACKET_HEAD 的再次封装

cmd

命令

一条 SmartBus 数据的命令关键字

cmdType

命令类型

一条 SmartBus 数据的命令类型

dataLen

正文数据长度

dstUnitClientId

接收者客户端ID

dstUnitClientType

接收者客户端类型

dstUnitId

接收者节点ID

packetSize

包长度

srcUnitClientId

发送者客户端ID

srcUnitClientType

发送者客户端类型

srcUnitId

发送者节点ID

errors Module

错误信息定义 :date: 2013-6-8 :author: lxy@hesong.ent

exception smartbus.errors.AlreadyExistsError

基类:builtins.Exception

对象已经存在,无法再次新建

exception smartbus.errors.AlreadyInitializedError

基类:builtins.Exception

已经初始化,无法重复的初始化

exception smartbus.errors.InvokeFlowIdError

基类:builtins.Exception

收到调用流程的返回结果时,ID无法匹配

exception smartbus.errors.NotInitializedError

基类:builtins.Exception

尚未初始化,无法使用

exception smartbus.errors.SmartBusError(code, message)

基类:builtins.Exception

SmartBus 通信错误

code

SmartBus错误码

message

错误信息

smartbus.errors.check_restval(code, raise_if_err=True)

检查 SmartBus 客户端 C-API 的返回结果是否正确

参数:
  • code (int) – 要检查的返回结果编码
  • raise_if_err (bool) – 是否在发现错误时抛出异常。默认为真。
返回:

raise_if_err 为 True 时,无错误则返回 None,有错误则抛出错误异常; 当 raise_if_err 为 False 时,无错误则返回 None,有错误则返回错误异常实例。

utils Module

辅助功能 :date: 2013-3-4 :author: tanbro

smartbus.utils.default_encoding = 'utf-8'

默认编码

sys.getdefaultencoding() 的返回值

smartbus.utils.ifnone(a, b)

如果第一个参数为None,则返回第二个参数,否则返回第一个参数 :param a: 第一个参数 :param b: 第二个参数 :return: 如果 a 为 None,则返回 b,否则返回 a。

相当于:

def ifnone(a, b):
   if a is None:
       return b
   else:
       return a

例如:

v1 = 1
v2 = 2
ifnone(v1, v2)

那么,返回值为1。

如果:

v1 = None
v2 = 2
ifnone(v1, v2)

那么,返回值为2。

smartbus.utils.unicode

str 的别名

smartbus.utils.to_bytes(s, encoding='utf-8')

str 转为 bytes

参数:
  • s (str, unicode, bytes, None) – 要转换的字符串
  • encoding – 编码,默认为系统编码。
smartbus.utils.to_unicode(s, encoding='utf-8')

str 转为 unicode

参数:
  • s (str, unicode, bytes, None) – 要转换的字符串
  • encoding – 编码,默认为系统编码。
smartbus.utils.to_str(s, encoding='utf-8')

unicode 或者 bytes 转为系统 str