简体   繁体   English

Python TCP 客户端停止从 TCP 服务器接收数据

[英]Python TCP CLient stops receiving data from TCP Server

I'm currently developing project using Raspberry Pi 4 and it's purpose is to listen to a mobile app and receive data using tcp protocol.我目前正在使用 Raspberry Pi 4 开发项目,其目的是收听移动应用程序并使用 tcp 协议接收数据。

import socket
from time import sleep
import RPi.GPIO as GPIO
import os
host="192.168.99.146"
port=11000

leftBackwardPin=26
leftForwardPin=21
rightBackwardPin=13
rightForwardPin=19
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(20,GPIO.IN)


GPIO.setup(26, GPIO.OUT) # Connected to PWMA
GPIO.setup(19, GPIO.OUT) # Connected to AIN2
GPIO.setup(13, GPIO.OUT) # Connected to AIN1
GPIO.setup(21, GPIO.OUT) # Connected to STBY


while True:
        with socket.socket(socket.AF_INET,socket.SOCK_STREAM) as s:
            s.connect((host,port))
            data=s.recv(1)
            if data==b'':
                print("no data")
            print(data)
            if data==b'1':
                print("forward")
                GPIO.output(leftForwardPin,GPIO.LOW)
                GPIO.output(rightForwardPin,GPIO.LOW)
                GPIO.output(leftBackwardPin,GPIO.LOW)
                GPIO.output(rightBackwardPin,GPIO.LOW)
                GPIO.output(leftForwardPin,GPIO.HIGH)
                GPIO.output(rightForwardPin,GPIO.HIGH)
            elif data==b'2':
                print("backward")
                GPIO.output(leftForwardPin,GPIO.LOW)
                GPIO.output(rightForwardPin,GPIO.LOW)
                GPIO.output(leftBackwardPin,GPIO.LOW)
                GPIO.output(rightBackwardPin,GPIO.LOW)
                GPIO.output(leftBackwardPin,GPIO.HIGH)
                GPIO.output(rightBackwardPin,GPIO.HIGH)
            elif data==b'3':
                print("left")
                GPIO.output(leftForwardPin,GPIO.LOW)
                GPIO.output(rightForwardPin,GPIO.LOW)
                GPIO.output(leftBackwardPin,GPIO.LOW)
                GPIO.output(rightBackwardPin,GPIO.LOW)
                GPIO.output(leftForwardPin,GPIO.HIGH)
                GPIO.output(rightBackwardPin,GPIO.HIGH)
            elif data==b'4':
                print("right")
                GPIO.output(leftForwardPin,GPIO.LOW)
                GPIO.output(rightForwardPin,GPIO.LOW)
                GPIO.output(leftBackwardPin,GPIO.LOW)
                GPIO.output(rightBackwardPin,GPIO.LOW)
                GPIO.output(leftBackwardPin,GPIO.HIGH)
                GPIO.output(rightForwardPin,GPIO.HIGH)
            else:
                GPIO.output(leftForwardPin,GPIO.LOW)
                GPIO.output(rightForwardPin,GPIO.LOW)
                GPIO.output(leftBackwardPin,GPIO.LOW)
                GPIO.output(rightBackwardPin,GPIO.LOW)
            sleep(0.2)

This is my python code on the Raspberry.这是我在 Raspberry 上的 python 代码。 It is basically tcp socket client which connects to my mobile app on my phone on port 110000. Everything works as it should, but the communication shows glitches which I think is normal for network connection, but the problem is that after some time it stops receiving any packages and do not connects to the mobile app.它基本上是 tcp 套接字客户端,它通过端口 110000 连接到我手机上的移动应用程序。一切正常,但通信显示故障,我认为这是网络连接的正常现象,但问题是一段时间后它停止接收任何软件包,并且不连接到移动应用程序。

using Microsoft.Maui.Controls;
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Timers;
using SuperSimpleTcp;
using System.Threading;

namespace MauiApp1;

using Microsoft.Maui.Controls;
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Timers;
using SuperSimpleTcp;
using System.Threading;

namespace MauiApp1;

public partial class MainPage : ContentPage
{
int command = 0;
SimpleTcpServer server = null;
public MainPage()
{
    InitializeComponent();
    NetworkAccess accessType = Connectivity.Current.NetworkAccess;
    
    if (accessType == NetworkAccess.Internet)
    {
        server = new SimpleTcpServer("192.168.99.146:11000");

        server.Events.ClientConnected += ClientConnected;
        server.Events.ClientDisconnected += ClientDisconnected;
        server.Events.DataReceived += DataReceived;

        server.Start();

    }
}

private void DataReceived(object sender, SuperSimpleTcp.DataReceivedEventArgs e)
{
    throw new NotImplementedException();
}

private void ClientDisconnected(object sender, ConnectionEventArgs e)
{
    throw new NotImplementedException();
}

private void ClientConnected(object sender, ConnectionEventArgs e)
{
    server.Send(e.IpPort, command.ToString());
}

private void Forward(object sender, EventArgs e)
{
    command = 1;
}

private void Backward(object sender, EventArgs e)
{
    command = 2;
}

private void Left(object sender, EventArgs e)
{
    command = 3;
}

private void Right(object sender, EventArgs e)
{
    command = 4;
}

private void Released(object sender, EventArgs e)
{
    command = 0;
}

} }

This is my .NET MAUI App's C#.这是我的 .NET MAUI 应用程序的 C#。 Here I am using package SimpleTCP, because it is easy to work with and I tested it and I am sure the problem is not there.在这里,我使用的是 package SimpleTCP,因为它易于使用并且我对其进行了测试,我确信问题不存在。 The c# program is TCPListener that listens on port 11000 and sends command of which of the apps buttons is clicked. c# 程序是 TCPListener,它在端口 11000 上进行侦听,并发送单击哪个应用程序按钮的命令。 The data is respresented using only one digit for the four buttons.四个按钮仅使用一位数字表示数据。 Please tell me where is my mistake and why the connection just stops and never resets again.请告诉我我的错误在哪里以及为什么连接停止并且不再重置。

I thought that the whole connecting and disconnecting the server was to difficult for the system to handle, so I change the things a little bit.我认为整个连接和断开服务器对于系统来说很难处理,所以我稍微改变了一些东西。

import socket
from time import sleep
import RPi.GPIO as GPIO
import os
host="10.0.0.9"
port=10000

leftBackwardPin=26
leftForwardPin=21
rightBackwardPin=13
rightForwardPin=19
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(20,GPIO.IN)


GPIO.setup(26, GPIO.OUT) # Connected to PWMA
GPIO.setup(19, GPIO.OUT) # Connected to AIN2
GPIO.setup(13, GPIO.OUT) # Connected to AIN1
GPIO.setup(21, GPIO.OUT) # Connected to STBY

with socket.socket(socket.AF_INET,socket.SOCK_STREAM) as s:
            s.connect((host,port))
            sleep(0.5)
            while True:
               data=s.recv(1)
               if data==b'':
                  print("no data")
               print(data)
               if data==b'1':
                print("forward")
                GPIO.output(leftForwardPin,GPIO.LOW)
                GPIO.output(rightForwardPin,GPIO.LOW)
                GPIO.output(leftBackwardPin,GPIO.LOW)
                GPIO.output(rightBackwardPin,GPIO.LOW)
                GPIO.output(leftForwardPin,GPIO.HIGH)
                GPIO.output(rightForwardPin,GPIO.HIGH)
               elif data==b'2':
                print("backward")
                GPIO.output(leftForwardPin,GPIO.LOW)
                GPIO.output(rightForwardPin,GPIO.LOW)
                GPIO.output(leftBackwardPin,GPIO.LOW)
                GPIO.output(rightBackwardPin,GPIO.LOW)
                GPIO.output(leftBackwardPin,GPIO.HIGH)
                GPIO.output(rightBackwardPin,GPIO.HIGH)
               elif data==b'3':
                print("left")
                GPIO.output(leftForwardPin,GPIO.LOW)
                GPIO.output(rightForwardPin,GPIO.LOW)
                GPIO.output(leftBackwardPin,GPIO.LOW)
                GPIO.output(rightBackwardPin,GPIO.LOW)
                GPIO.output(leftForwardPin,GPIO.HIGH)
                GPIO.output(rightBackwardPin,GPIO.HIGH)
               elif data==b'4':
                print("right")
                GPIO.output(leftForwardPin,GPIO.LOW)
                GPIO.output(rightForwardPin,GPIO.LOW)
                GPIO.output(leftBackwardPin,GPIO.LOW)
                GPIO.output(rightBackwardPin,GPIO.LOW)
                GPIO.output(leftBackwardPin,GPIO.HIGH)
                GPIO.output(rightForwardPin,GPIO.HIGH)
               else:
                GPIO.output(leftForwardPin,GPIO.LOW)
                GPIO.output(rightForwardPin,GPIO.LOW)
                GPIO.output(leftBackwardPin,GPIO.LOW)
                GPIO.output(rightBackwardPin,GPIO.LOW)
               sleep(0.1)

This is my update on the code.这是我对代码的更新。 Instead of connecting and disconnecting, which I find everywhere in the net as a example of socket connections in python, I decided to keep the connection without disconnecting again and again and that way the two devices keep the connection and there is no package lost and the system is barely asleep during the program so I am happy with the result.而不是连接和断开连接,我在网络上到处都可以找到 python 中的套接字连接示例,我决定保持连接而不会一次又一次地断开连接,这样两个设备就会保持连接并且没有 package 丢失和系统在程序期间几乎没有睡着,所以我对结果很满意。 And I've done some test and after 15 minutes the connection still stays alive.我做了一些测试,15 分钟后连接仍然存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM