Demo entry 6354681

Python_Serial

   

Submitted by anonymous on Apr 10, 2017 at 10:29
Language: Python. Code size: 618 Bytes.

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import serial
import msvcrt
 
ser = serial.Serial('COM5', 9600)  # 这里需要修改自己板子对应的COM口,这是WINDOWS环境下
# ser = serial.Serial('/dev/ttyS1', 9600)  # 这是Linux环境下,也需要修改对应的串口
 
def process(line):
    temp = line
    text = ""
 
    while temp:
        text = text + "\u" + temp[:4]
        temp = temp[4:]
 
    return text[:len(text) - len(text) % 6].decode('unicode-escape').encode('utf-8')
 
 
print "Press 'D' to exit..."
 
while True:
    print "短信内容是: " + process(ser.readline())
 
    if ord(msvcrt.getch()) in [68, 100]:
        break
 
ser.close()

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).