简体   繁体   中英

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.

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. 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.

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#. 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. The c# program is TCPListener that listens on port 11000 and sends command of which of the apps buttons is clicked. 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. And I've done some test and after 15 minutes the connection still stays alive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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