資源簡介
Python串口操作.zip

代碼片段和文件信息
#!/usr/bin/python
#?coding=UTF-8
import?serial
###################################################
#
#?功?能:?將接收到的數據已hex顯示
#?參?數:?串口接受到的數據
#?返?回:?轉換后的數據
#
###################################################
def?hexshow(data):
????hex_data?=?‘‘
????hLen?=?len(data)
????for?i?in?xrange(hLen):
????????hvol?=?ord(data[i])
????????hhex?=?‘%02x‘?%?hvol
????????hex_data?+=?hhex+‘?‘
????print?‘hexshow:‘?hex_data
###################################################
#
#?功?能:?將需要發送的字符串以hex形式發送
#?參?數:?待發送的數據
#?返?回:?轉換后的數據
#
###################################################
def?hexsend(string_data=‘‘):
????hex_data?=?string_data.decode(“hex“)
????return?hex_data
if?__name__?==?‘__main__‘:
????serial?=?serial.Serial(‘/dev/ttyS0‘?115200)
????print?serial
????if?serial.isOpen():
???????print(“open?success“)
????else:
????????print(“open?failed“)
????try:
????????while?True:
????????????count?=?serial.inWaiting()
????????????if?count?>?0:
????????????????data?=?serial.read(count)
????????????????if?data?!=?b‘‘:
????????????????????print(“receive:“?data)
????????????????????serial.write(data)
????????????????else:
????????????????????serial.write(hexsend(data))
????except?KeyboardInterrupt:
????????if?serial?!=?None:
????????????serial.close()
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-07-20?01:06??Python串口操作\
?????目錄???????????0??2018-07-20?00:39??Python串口操作\usart\
?????目錄???????????0??2018-07-20?01:05??Python串口操作\usart\.idea\
?????目錄???????????0??2018-08-04?09:57??Python串口操作\usart\.idea\inspectionProfiles\
?????文件?????????205??2018-07-19?23:03??Python串口操作\usart\.idea\misc.xm
?????文件?????????262??2018-07-19?23:03??Python串口操作\usart\.idea\modules.xm
?????文件?????????459??2018-07-19?23:03??Python串口操作\usart\.idea\usart.iml
?????文件???????15999??2018-07-20?01:05??Python串口操作\usart\.idea\workspace.xm
?????文件????????1384??2018-07-20?00:39??Python串口操作\usart\uarttest.py
評論
共有 條評論