直接从Python调用PJON C++库
pjon-cython的Python项目详细描述
PJON cython
直接从Python 2或Python 3(通过Cython)调用pjon C++库< /p>
pjon(github:PJON)是一个开源、多主、多媒体(单线、双线、无线)通信协议,可用于各种平台(arduino/avr、esp8266、teensy)。
PJON是为数不多的用于微控制器的多主通信协议的开源实现之一。
Pjon cython与Pjon python之比较
^ {STR 1 } $PJON Cython 允许您通过Python通过Cython(Cy+PrpPysPython)使用C++ PJON库 pjon python是python中pjon协议的重新实现
当前状态:
仅支持pjon 11.2和以下策略:-
- 本地udp
- globaludp
- 通过序列
- 通过serialasyncnew
注意
- pjon cython版本与pjon版本一致,表明与uc平台的c实现兼容。
python支持
python 2.7、3.4、3.5和3.6都经过了测试,并被认为是受支持的
平台支持
Linux和MacOSX被认为是受支持的。不支持Windows(抱歉!).
从PIP安装
当前版本是11.2.0
pip install pjon-cython
测试
$(which python) setup.py nosetests --with-doctest --doctest-extension=md
GlobalUDP示例
>>>importpjon_cythonasPJON>>>classGlobalUDP(PJON.GlobalUDP):...# you can overload __init__ if you want...def__init__(self,device_id):...PJON.GlobalUDP.__init__(self,device_id)...self.packets_received=0...defreceive(self,data,length,packet_info):...print("Recv ({}): {}".format(length,data))...print(packet_info)...self.packets_received+=1...self.reply(b'P')>>>g=GlobalUDP(44)>>>idx=g.send(123,b'HELO')>>># calling loop calls the PJON bus.update() and bus.receive()>>># and the return is the results of those functions ->>>packets_to_send,receive_status=g.loop()>>># packets_to_send is the Number of packets in the PJON buffer>>>packets_to_send1>>>#PJON constants are available too>>>receive_status==PJON.PJON_FAILTrue>>># When you're done with your PJON interface, you can cleanup the connection by deleting it>>>delg
通过序列示例
>>>importpjon_cythonasPJON>>>#ThroughSerial Example>>># Make sure you set self.bus.set_synchronous_acknowledge(false) on the other side>>>>>>classThroughSerial(PJON.ThroughSerial):......defreceive(self,data,length,packet_info):...ifdata.startswith(b'H'):...print("Recv ({}): {} - REPLYING".format(length,data))...self.reply(b'BONZA')...else:...print("Recv ({}): {}".format(length,data))...print('')...>>># Put your actual serial device in here...>>>ts=ThroughSerial(44,b"/dev/null",115200)>>># Send returns the packet's index in the packet buffer>>>ts.send(100,b'PING 1')0>>>ts.send(100,b'PING 2')1>>># Error handling happens through exceptions such as PJON.PJON_Connection_Lost>>>whileTrue:...packets_to_send,receive_status=ts.loop()Traceback(mostrecentcalllast):...PJON_Connection_Lost
设置可配置属性
>>>importpjon_cythonasPJON>>>classGlobalUDP(PJON.GlobalUDP):...defreceive(self,data,length,packet_info):...print("Recv ({}): {}".format(length,data))>>># GlobalUDP and LocalUDP both support set_port to configure their UDP listening port>>>g=GlobalUDP(99,8821)>>>delg>>>#They return the class object, so you can "chain them">>>pjon=GlobalUDP(100,8821).set_autoregistration(False)>>>pjon# doctest: +ELLIPSIS<__main__.GlobalUDPobjectat0x...>>>>>>># These options affect packet overhead (in bytes)>>>pjon.packet_overhead()6>>>pjon.set_crc_32(True).packet_overhead()9>>>pjon.set_packet_id(True).packet_overhead()11>>>pjon.set_synchronous_acknowledge(True).packet_overhead()11>>>pjon.set_packet_id(False).set_asynchronous_acknowledge(False).packet_overhead()9>>>pjon.set_crc_32(False).include_sender_info(False).packet_overhead()5
对pyserial_Asyncio使用基于序列的策略
您可以将文件描述符传递给throughserial和throughserialasync,而不是传递串行端口字符串 允许其他库轮询串行端口的方法。
例如,请参见reticul8 项目。