资源简介

电脑和树莓派在同一局域网内,先在电脑和树莓派创建python运行环境,然后在树莓派中用python运行rpi.py;在电脑上运行computer.py;电脑上输入字符即可在树莓派上即时显示!

资源截图

代码片段和文件信息

import socket
import time
import sys

SERVER_IP = “192.168.31.151“   #树莓派的IP地址
SERVER_PORT = 8888

print(“Starting socket: TCP...“)
server_addr = (SERVER_IP SERVER_PORT)
socket_tcp = socket.socket(socket.AF_INET socket.SOCK_STREAM)

while True:
    try:
        print(“Connecting to server @ %s:%d...“ %(SERVER_IP SERVER_PORT))
        socket_tcp.connect(server_addr)
        break
    except Exception:
        print(“Can‘t connect to servertry it latter!“)
        time.sleep(1)
        continue
print(“Please input:“)
while True:
    try:
        data = socket_tcp.recv(512)
        if len(data)>0:
            print(“Received: %s“ % data)
            command=raw_input()
            socket_tcp.send(command)
            time.sleep(1)
            continue
    except Exception:
        socket_tcp.close()
        socket_tcp=None
        sys.exit(1)

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件         896  2019-01-11 13:28  computer.py
     文件        1104  2019-01-11 13:30  rpi.py

评论

共有 条评论