Raspberry Pi 之间的 Python 套接字连接

泰莫

我是编程的初学者。我目前正在尝试使用两个 Raspberry Pi 创建一个警报系统。我想出了如何设置套接字连接并通过 tcp 从客户端发送到服务器。

现在的问题是:如何设置不同的“tcp 类型”以便调用函数:

StartAlarm()onRPI1(client)将调用函数StartAlarm()来运行RPI2(server)

StopAlarm()onRPI1(client)将调用StopAlarm()运行onRPI2(server)

请随时纠正我的语法和拼写:)

服务提供商

您可以创建一个不断等待这些消息指令的线程或子进程,因此如果套接字中的消息是Nonedo nothing if the message is 'stop the alarm'then do stopAlarm()else if the message 'start the alarm'then do startAlarm()既然到目前为止你还没有展示你做了什么,我只能帮你。我希望它有帮助!

更新

在 RPI1 上:

import socket

def sendTCP(message=''):
   HOST, PORT = "192.168.43.136", 9999
   sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   try:
      sock.connect((HOST, PORT))
   except socket.error:
      pass
   else:
      sock.send(message) #the message is the order
   sock.close()

在 RPI2 上:

import socket
import threading


def start_server(host='', port=1060):
   global sock_server
   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
   s.bind((host, port))
   s.listen(1)
   while True:
      sock_server, sockname = s.accept()
      break


def receiveTCP(sock):
   message = sock.recv(32)
   return message


def stopAlarm():
   pass


def startAlarm():
   pass


class OrderWaiter(threading.Thread):
   def __init__(self, **kwargs):
      super(OrderWaiter, self).__init__(**kwargs)

   def run(self):
      global sock_server
      while True:
         try:
            message = receiveTCP(sock_server)
         except Exception:
            pass
         else:
            if message != '':
               if message == 'stop alarm':
                  stopAlarm()
               elif message == 'start alarm':
                  startAlarm()
               sock_server.close()
               break

if __name__ == '__main__':
   sock_server = None
   start_server() # pass the host and the port as parameters
   OrderWaiter().start() #start the thread which will wait for the order

我希望这有帮助

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python,Windows 和 Raspberry pi 3 之间的套接字

来自分类Dev

Raspberry Pi和Arduino中的Web套接字之间的区别

来自分类Dev

在运行Node.js的Raspberry Pi和服务器之间创建持久套接字连接的最佳方法

来自分类Dev

Python和TCL之间的套接字连接

来自分类Dev

Windows 和 Linux 之间的 Python 套接字连接

来自分类Dev

在Raspberry Pi上使用Python连接到wifi

来自分类Dev

Python Raspberry Pi GPIO错误

来自分类Dev

Raspberry Pi Python ValueError吗?

来自分类Dev

BluePy频繁BLE断开Raspberry Pi4和ESP32之间的连接-(Bluetooth)

来自分类Dev

两个Raspberry Pi之间的蓝牙6LoWPAN连接

来自分类Dev

Raspberry Pi板载USB和插入式USB连接之间的零干扰

来自分类Dev

Java和Python之间的套接字连接-struct.unpack错误

来自分类Dev

与Raspberry Pi的SSH连接:连接被拒绝

来自分类Dev

Python套接字连接异常

来自分类Dev

Python套接字无法连接

来自分类Dev

python套接字拒绝连接

来自分类Dev

Python套接字重新连接

来自分类Dev

套接字未连接 Python

来自分类Dev

Raspberry Pi闪烁的LED问题-Python与Java

来自分类Dev

Raspberry Pi Camera自动捕获python脚本

来自分类Dev

Raspberry Pi Python脚本未定义?

来自分类Dev

python + raspberry pi gpio无效语法

来自分类Dev

是否安装了Python:(Raspberry Pi Ubuntu)

来自分类Dev

Raspberry Pi Zero上的Python非法指令

来自分类Dev

处于 Sl 状态的 Raspberry Pi python 脚本

来自分类Dev

在Raspberry PI上管理Wifi连接

来自分类Dev

来自Outsitde LAN的MySql Raspberry Pi连接

来自分类Dev

无法通过SSH连接到Raspberry Pi

来自分类Dev

Raspberry Pi Edimax Wlan Stick断开连接