python串口库函数-python串口模块pyserial简单介绍工作实践真知分享ITp_第1页
python串口库函数-python串口模块pyserial简单介绍工作实践真知分享ITp_第2页
python串口库函数-python串口模块pyserial简单介绍工作实践真知分享ITp_第3页
python串口库函数-python串口模块pyserial简单介绍工作实践真知分享ITp_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

python串⼝库函数_python串⼝模块pyserial简单介绍⼯作实践真知分享ITp。。。前⾯已经介绍过ubuntu下安装ch341的驱动程序下⾯安装python的串⼝模块,这样就直接可以将协调器发⽣送过来的所有数据,通过python串⼝读出来,然后写⼊到数据库中(可以使⽤python操作数据库的),有关数据库的远程同步(搜索mysql同步)这个以后再说.⾸先要有⼏点认识:FeaturessameclassbasedinterfaceonallsupportedplatformsaccesstotheportsettingsthroughPython2.2+propertiesportnumberingstartsatzero,noneedtoknowtheportnameintheuserprogramportstring(devicename)canbespecifiedifaccessthroughnumberingisinappropriatesupportfordifferentbytesizes,stopbits,parityandflowcontrolwithRTS/CTSand/orXon/XoffworkingwithorwithoutreceivetimeoutfilelikeAPIwith“read”and“write”(“readline”etc.alsosupported)Thefilesinthispackageare100%purePython.TheydependonnonstandardbutcommonpackagesonWindows(pywin32)andJython(JavaComm).POSIX(Linux,BSD)usesonlymodulesfromthestandardPythondistribution)Theportissetupforbinarytransmission.NoNULLbytestripping,CR-LFtranslationetc.(whicharemanytimesenabledforPOSIX.)Thismakesthismoduleuniversallyuseful.⿊体字的部分注意⼀下,特别是最后⼀项.全部是⼆进制传输.sudoeasy_installpyserial然后测试⼀下sudopython(这是为了⼀会给串⼝读写权限)>>>importserial>>>ser=serial.Serial('/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0')>>>serSerial(port='/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0',baudrate=9600,bytesize=8,parity='N',stopbits=1,timeout=None,xonxoff=False,rtscts=False,dsrdtr=False)>>>ser.timeout=1>>>serSerial(port='/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0',baudrate=9600,bytesize=8,parity='N',stopbits=1,timeout=1,xonxoff=False,rtscts=False,dsrdtr=False)有时候如果你使⽤/dev/ttyUSB0⾏不通的话,就直接使⽤/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0timeout=None,#setatimeoutvalue,Noneforwaitingforever上⾯timeout的意思是,如果是none,那么程序永远会死在这⾥.⼀直等待timeout=0#non-blockingmode(returnimmediatelyonread)timeout=0:⾮阻塞形式(读完之后就返回,费时的io操作,就直接交给后台处理了)

timeout=x#settimeouttoxseconds(floatallowed)超时时间⼀到,程序就是继续执⾏————————–没错我是分割线–开始————————————⼩知识:读写串⾏⼝时,既可以同步执⾏,也可以重叠(异步)执⾏。在同步执⾏时,函数直到操作完成后才返回。这意味着在同步执⾏时线程会被阻塞,从⽽导致效率下降。在重叠执⾏时,即使操作还未完成,调⽤的函数也会⽴即返回。费时的I/O操作在后台进⾏,这样线程就可以⼲别的事情。例如,线程可以在不同的句柄上同时执⾏I/O操作,甚⾄可以在同⼀句柄上同时进⾏读写操作。”重叠”⼀词的含义就在于此。阻塞的定义:对于read,block指当串⼝输⼊缓冲区没有数据的时候,read函数将会阻塞在这⾥,⼀直到串⼝输⼊缓冲区中有数据可读取,read读到了需要的字节数之后,返回值为读到的字节数;对于write,block指当串⼝输出缓冲区满,或剩下的空间⼩于将要写⼊的字节数,则write将阻塞,⼀直到串⼝输出缓冲区中剩下的空间⼤于等于将要写⼊的字节数,执⾏写⼊操作,返回写⼊的字节数。⾮阻塞的定义:对于read,noblock指当串⼝输⼊缓冲区没有数据的时候,read函数⽴即返回,返回值为0。对于write,noblock指当串⼝输出缓冲区满,或剩下的空间⼩于将要写⼊的字节数,则write将进⾏写操作,写⼊当前串⼝输出缓冲区剩下空间允许的字节数,然后返回写⼊的字节数。—————————-没错我是分割线–结束———————————-Becarefullywhenusing“readline”.Dospecifyatimeoutwhenopeningtheserialportotherwiseitcouldblockforeverifnonewlinecharacterisreceived.Alsonotethat“readlines”onlyworkswithatimeout.“readlines”dependsonhavingatimeoutandinterpretsthatasEOF(endoffile).Itraisesanexceptioniftheportisnotopenedcorrectly.ParametersfortheSerialclassser=serial.Serial(port=None,#numberofdevice,numberingstartsat#zero.ifeverythingfails,theuser#canspecifyadevicestring,note#thatthisisn'tportableanymore#ifnoportisspecifiedanunconfigured#anclosedserialportobjectiscreatedbaudrate=9600,#baudratebytesize=EIGHTBITS,#numberofdatabitsparity=PARITY_NONE,#enableparitycheckingstopbits=STOPBITS_ONE,#numberofstopbitstimeout=None,#setatimeoutvalue,Noneforwaitingforeverxonxoff=0,#enablesoftwareflowcontrolrtscts=0,#enableRTS/CTSflowcontrol

interCharTimeout=None#Inter-charactertimeout,Nonetodisable)MethodsofSerialinstancesopen()#openportclose()#closeportimmediatelysetBaudrate(baudrate)#changebaudrateonanopenportinWaiting()#returnthenumberofcharsinthereceivebufferread(size=1)#read"size"characterswrite(s)#writethestringstotheportflushInput()#flushinputbuffer,discardingallit'scontentsflushOutput()#flushoutputbuffer,abortoutputsendBreak()#sendbreakconditionsetRTS(level=1)#setRTSlinetospecifiedlogiclevelsetDTR(level=1)#setDTRlinetospecifiedlogiclevelgetCTS()#returnthestateoftheCTSlinegetDSR()#returnthestateoftheDSRlinegetRI()#returnthestateoftheRIlinegetCD()#returnthestateoftheCDlineAttributesofSerialinstancesreadonlyportstr#devicenameBAUDRATES#listofvalidbaudratesBYTESIZES#listofvalidbytesizesPARITIES#listofvalidparitiesSTOPBITS#listofvalidstopbitwidthsNewvaluescanbeassignedtothefollowingattributes,theportwillbereconfigured,evenifit’sopenedatthattime:(即使是打开的情况下也会重新配置liub)port#portname/numberassetbytheuserbaudrate#currentbaudratesettingbytesize#bytesizeinbitsparity#paritysettingstopbits#stopbitwith(1,2)timeout#timeoutsettingxonxoff#ifXon/Xoffflowcontrolisenabled

rtscts#ifhardwareflowcontrolisenabled居然还有这么多好东西,看看下⾯:TCP/IP–serialbridgeThisprogramopensaTCP/IPport.Whenaconnectionismadetothatport(e.g.withtelnet)itforwardsalldatatotheserialportandviceversa.Thisexampleonlyexportsaraws

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论