参考:
1. https://blog.csdn.net/k_atherine/article/details/50264663?utm_source=blogxgwz9#commentBox
https://blog.csdn.net/k_atherine/article/details/50264663
2.http://outofmemory.cn/code-snippet/1662/python-string-str-zijieshu-group-together-zhuanhua
3.https://blog.csdn.net/github_40094105/article/details/78080647
4.https://blog.csdn.net/weixin_41656968/article/details/80042554
内容:
1. 基本上是按照参考1完成的,只是我的树莓派是3b+,发光二极管是七彩的(有正负)。
2. 其他的区别还有PC端是Python2.7,PI端是3.5.3。两者语法有些不同,虽然我没系统的学习过。具体的是PC端发过来的字符串到Pi端都变成了: b'*',*是传输的字符串。只有强制转化
bytes.decode(data)
才能使用。如果要从PI再传输到PC还要在转化回去,参考2。
3. 参考1把核心函数写出了,如果要做稳定还有很多细节需要添加。如第二次或第n次连接时提示“Address already in use ... ...”,是因为没有允许IP和端口地址复用引起的,这个地址一直会被占用,除非重启。利用参考3,可以解决这个问题。
4. 多次连接的过程中也出现了“GPIO This channel is already in use ... ...”的提示,还真如百度所说的一样,别管他!于是我就没管,程序照样运行。但作为有些强迫症的我,还是想着哪次把它给解决了。虽然有人说添加
GPIO.cleanup()
但没啥用,反而还有其他的warning。
5.“如果我们想要在树莓派运行server端电脑上运行client端,让树莓派来作为服务器接收用户数据的话,我们就可以在树莓派上ifconfig一下查看一下它的地址,并将两个程序的IP地址都改为它,接着先运行树莓派上的server后运行电脑上的client就行了”--参考4
6. 接下来要仔细学习下这次用到的socket通信!
Socket树莓派实时传输到PC上
https://blog.csdn.net/weixin_42430904/article/details/83474837
【玩树莓】编程篇(二)Python TCP、UDP 网络编程
https://www.eefocus.com/nightseas/blog/15-03/310847_48fe6.html,除本文之外还有Server端和Client端的双向数据收发,以及UDP协议的编程(包括数据传输和多线程操作)
其他用C/C++进行的类似控制
1.https://www.cnblogs.com/computer1-2-3/p/6875367.html
2.https://blog.csdn.net/qq_38880380/article/details/80238127
基于 Socket 的 Android与树莓派通信
https://blog.csdn.net/qq_23320955/article/details/80141775,介绍了socket通信模型。
源代码:
PI作server:
#import necessary package
import socket
import time
import sys
import RPi.GPIO as GPIO
GPIO.cleanup()
#define host ip: Rpi's IP
HOST_IP = "169.254.64.199"
HOST_PORT = 8881
print("Starting socket: TCP...")
#1.create socket object:socket=socket.socket(family,type)
socket_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("TCP server listen @ %s:%d!" %(HOST_IP, HOST_PORT) )
# 在绑定前调用setsockopt让套接字允许地址重用
socket_tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
host_addr = (HOST_IP, HOST_PORT)
#2.bind socket to addr:socket.bind(address)
socket_tcp.bind(host_addr)
#3.listen connection request:socket.listen(backlog)
socket_tcp.listen(1)
#4.waite for client:connection,address=socket.accept()
socket_con, (client_ip, client_port) = socket_tcp.accept()
print("Connection accepted from %s." %client_ip)
socket_con.send(b'Welcome to RPi TCP server!')
#5.handle
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.OUT)
print("Receiving package...")
while True:
try:
data=socket_con.recv(512)
data_trans=bytes.decode(data)
print(data_trans)
if len(data_trans)>0:
print("Received:%s"%data_trans)
if data_trans=='1':
GPIO.output(12,GPIO.HIGH)
elif data_trans=='0':
GPIO.output(12,GPIO.LOW)
socket_con.send(data)
time.sleep(1)
continue
except Exception:
GPIO.cleanup()
socket_tcp.close()
sys.exit(1)
PC作client:
import socket
import time
import sys
#RPi's IP
SERVER_IP = "169.254.64.199"
SERVER_PORT = 8881
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 server,try it latter!")
time.sleep(1)
continue
print("Please input 1 or 0 to turn on/off the led!")
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)