orange pi串口屏

发布时间:2016-12-09 11:37:47 阅读:693次

https://item.taobao.com/item.htm?ut_sk=1.UoVkCheXF8sDAJg4JSic%2Bk0X_21380790_1481253283711.TaoPassword-QQ.1&id=522166536324&sourceType=item&price=25&suid=B6EAFABC-DF25-4E47-A1D9-1E3D620D82DC&cpp=1&shareurl=true&spm=a313p.22.2jf.18368149593&short_name=h.1J7y3f&cv=AAiiezBC&sm=a5ebda&app=chrome

https://item.taobao.com/item.htm?spm=a1z09.2.0.0.IXHvC4&id=43644958114&_u=91nj374751f


#!/usr/bin/python
# coding=utf-8

#本程序需要python安装pyserial扩展
#python2.x 请执行 :
# apt-get install python-pip
# pip install pyserial

#python3.x 请执行 :
# apt-get install python3-pip
# pip3 install pyserial

#调用方法
#   python  screen.py
# 或
#   python3 screen.py

#程序可接受一下参数:
#参数 -n 设备名称,作显示用 ,默认为 OrangePi
#参数 -p 串口路径 ,默认为 /dev/ttyS1
#参数 -s 串口速率 ,默认为 115200
#参数 -e 网卡名称 ,默认为 eth0
#参数 -r 信息刷新频率,单位:秒 ,默认为 1
#参数 -w 屏幕宽度 ,默认为 220
#参数 -h 屏幕高度 ,默认为 176
#参数 -d 屏幕方向 ,0,1,2,3  代表不同方向
#参数 -b 屏幕亮度 ,0~100  越大越亮

#例如: python  screen.py -n "OrangePi PC2" -e eth0 -s 115200 -p /dev/ttyS1 -r 1 -w 240 -h 320 -d 3  -b 80

#如果希望程序在后台执行,可在命令最后加 &
#例如:  python  screen.py &

#如需系统启动时自动执行,可根据需要将命令和参数加入/etc/rc.local中
#       python  /your_path/screen.py -n "OrangePi" -e eth0 -s 115200 -p /dev/ttyS1 &

import os,sys, getopt
import time
import datetime
import serial

deviceName="OrangePi"

tty="/dev/ttyS1"
#tty = "/dev/tty.usbserial"
speed=115200

ethName="eth0"

sleepStep=1

sWidth=220
sHeight=176

sDirect=0
sBright=50

opts, args = getopt.getopt(sys.argv[1:], "p:s:e:n:r:w:h:d:b:")

try:
    for op, value in opts:
        if op == "-p":
            tty = value
        elif op == "-s":
            speed = int(value)
        elif op == "-e":
            ethName = value
        elif op == "-n":
            deviceName = value
        elif op == "-r":
            sleepStep = int(value)
        elif op == "-w":
            sWidth = int(value)
        elif op == "-h":
            sHeight = int(value)
        elif op == "-d":
            sDirect = int(value)
        elif op == "-b":
            sBright = int(value)
except:
    speed=speed
    sleepStep=sleepStep

#print(deviceName,tty,speed,ethName)


def connectSerial(t, s):
    return serial.Serial(t, s)

def sendMessage(port,prog):
    n = port.write((prog+"\r\n").encode("gbk"))
    #print(port.portstr)
    #print(n)

def getSysDatetime():
    return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

def getSysDate():
    return datetime.datetime.now().strftime("%Y-%m-%d")

def getSystime():
    return datetime.datetime.now().strftime("%H,%M,%S")

def getLocalIp(ifname = 'eth0'):
    shellStr = "/sbin/ifconfig "+ifname+" |sed -n '/inet addr/s/^[^:]*:\\([0-9.]\\{7,15\}\\) .*/\\1/p'"
    output = os.popen(shellStr)
    ip=output.readline().strip()
    return ip

def getTrans(ifname = 'eth0'):
    shellStrSend = "/sbin/ifconfig "+ifname+" | grep bytes | awk '{print $6}' | awk -F : '{print $2}'"
    shellStrRecv = "/sbin/ifconfig " + ifname + " | grep bytes | awk '{print $2}' | awk -F : '{print $2}'"
    outputSend = os.popen(shellStrSend)
    outputRecv = os.popen(shellStrRecv)
    return (outputSend.readline().strip(),outputRecv.readline().strip())


def getCpuUse():
    shellStr = "/usr/bin/top -n 1 -b |grep Cpu | cut -d \",\" -f 1 | cut -d \":\" -f 2 |cut -d \"u\" -f 1"
    output = os.popen(shellStr)
    cpu=output.readline().strip()
    #print(cpu)
    return cpu


def cpuTem():
    shellStr = "cat /sys/class/thermal/thermal_zone0/temp"
    ret = os.popen(shellStr)
    temp=ret.readline().strip()
    return temp

def getMem():
    shellStrTotal = "echo `free -m | grep Mem | awk '{print $2}'`"
    shellStrUsed = "echo `free -m | grep Mem | awk '{print $3}'`"
    outputTotal = os.popen(shellStrTotal)
    outputUsed = os.popen(shellStrUsed)
    return (outputTotal.readline().strip(), outputUsed.readline().strip())


def initScreen(t):
    prog = "DR"+str(sDirect)+";SEBL("+str(sBright)+");"
    prog += "CLS(0);"
    prog += "BOX(0,0,"+str(sWidth-1)+","+str(sHeight-1)+",15);" \
            "BOX(1,1,"+str(sHeight-2)+","+str(sHeight-2)+",0);" \
            "BOXF(2,2,"+str(sWidth-3)+",25,3);PL(2,26,"+str(sWidth-3)+",26,0);"
    prog += "SHTM(16,"+str(sWidth-70)+","+str(sHeight-20)+",1,0);STIM("+getSystime()+");"
    sendMessage(t, prog)

def createMessage(t):
    prog = "SBC(3);LABL(16,2,4,"+str(sWidth-3)+",'"+deviceName+u" 状态',4,1);"
    prog += u"SBC(0);LABL(16,4,30,"+str(sWidth-3)+u",'CPU占用率: "+getCpuUse()+" %',4,0);"
    prog += u"SBC(0);LABL(16,4,50,"+str(sWidth-3)+u",'CPU温度  : "+cpuTem()+u"℃',4,0);"
    prog += "SBC(0);LABL(16,4,70,"+str(sWidth-3)+",'"+ethName+" IP  : "+getLocalIp(ethName)+"',4,0);"
    trans=getTrans(ethName)
    prog += u"SBC(0);LABL(16,4,90,"+str(sWidth-3)+u",'     发送 : "+trans[0]+"',4,0);"
    prog += u"SBC(0);LABL(16,4,110,"+str(sWidth-3)+u",'     接收 : " + trans[1] + "',4,0);"
    mem=getMem()
    prog += u"SBC(0);LABL(16,4,130,"+str(sWidth-3)+u",'内存 : "+mem[1]+" / "+mem[0]+" M ',4,0);"
    prog += "SBC(0);LABL(16,4," + str(sHeight - 20) + ",90,'" + getSysDate() + "',1,2);STIM("+getSystime()+");"
    #prog += "SBC(0);LABL(16,4,"+str(sHeight-20)+","+str(sWidth-3)+",'"+getSysDatetime()+"',1,2);"
    sendMessage(t, prog)

p=connectSerial(tty,speed)
initScreen(p)

while (1):
    createMessage(p)
    time.sleep(sleepStep)

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询
下一篇:香橙派听歌

转载请注明:orange pi串口屏 出自老鄢博客 | 欢迎分享