Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5969081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:10:48+00:00 2026-05-22T20:10:48+00:00

I am expanding on a GUI program for a remotely controlled robot. The idea

  • 0

I am expanding on a GUI program for a remotely controlled robot. The idea is to program a simple GUI client in Python that connects to a remote server, also written in Python. The client would send simple messages to the server, the server would receive the message, and then transmit it via serial to an Arduino Mega that is connected via USB.

I have gotten the code to work, kind of.

Right now, I can connect one time from my client Python GUI, send one message, then it loses connection. Then I must halt the server, restart it, and then send another single message.

What is happening here?

Here is the server script, adopted from another source.

import serial
import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('', 9000))
sock.listen(1)
print "Listening on TCP 9000"
motor = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
print "Connected to Arduino Mega at: /dev/ttyUSB0"
while(1):
    print "Waiting For Connection..."
    connection, addr = sock.accept()
    connection.setblocking(0)
    print "Connected by", addr[0]
    while(1):
        try:
            sockdata = connection.recv(1)
            break
        except:
            pass
    if sockdata == 'X':
        print "Exiting"
        break
    if sockdata == '0':
        print "Stopping"
        motor.write(sockdata)
    if sockdata == '1':
        print "Forward"
        motor.write(sockdata)
    if sockdata == '2':
        print "Left"
        motor.write(sockdata)
    if sockdata == '3':
        print "Right"
        motor.write(sockdata)
    if sockdata == '4':
        motor.write(sockdata)
        print "Backwards"
    else:
        pass

And here is my client code, minus the resource file of course.

from socket import *
from PythonCard import model
HOST = ''
PORT = 9000
ADDR = (HOST,PORT)

Client = socket (AF_INET,SOCK_STREAM)
Client.connect((ADDR))

class MainWindow(model.Background):
    def on_FwdBtn_mouseClick(self, event):
        Client.send('1')
    def on_LftBtn_mouseClick(self, event):
        Client.send('2')
    def on_RitBtn_mouseClick(self, event):
        Client.send('3')
    def on_RevBtn_mouseClick(self, event):
        Client.send('4')
    def on_StpBtn_mouseClick(self, event):
        Client.send('0')
    def on_GetPing_mouseClick(self, event):
        Client.send('~')


app = model.Application(MainWindow)
app.MainLoop()

EDIT

After looking into the below fixes or at least some of them, here is my code for the server now, the client remains the same.

import serial
import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('', 9001))
sock.listen(1)
print "Listening on TCP 9001"
motor = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
print "Connected to Motor Controller: /dev/ttyUSB0"
print "Waiting For Connection..."
connection, addr = sock.accept()
connection.setblocking(0)
while(1):

    print "Connected by", addr[0]
    while(1):
        try:
            sockdata = connection.recv(1024)
            break
        except:
            pass
    if sockdata == 'X':
        print "Exiting"
        break
    if sockdata == '0':
        print "Stopping"
        motor.write(sockdata)
    if sockdata == '1':
        print "Forward"
        motor.write(sockdata)
    if sockdata == '2':
        print "Left"
        motor.write(sockdata)
    if sockdata == '3':
        print "Right"
        motor.write(sockdata)
    if sockdata == '4':
        motor.write(sockdata)
        print "Backwards"
    else:
        pass

This works a little better, but still not right I don’t think. When I run this I get the server running, and after starting my GUI, I can watch my terminal window on the server and will get the actual commands as they come through

“Connected from 127.0.0.1” “Forward”
“Connected from 127.0.0.1” “left”
“Connected from 127.0.0.1” “right”
“Connected from 127.0.0.1”

It looks as though each time I send a command I get reconnected. I want to maintain a connection once I start my GUI, not disconnect and reconnect each time I send a command.
Sorry if I sound kind of stupid in this, but I just started with Python about three weeks ago.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:10:49+00:00Added an answer on May 22, 2026 at 8:10 pm

    You are calling accept() inside the loop, which will block for another connection. The original connection will be dereferenced and automatically closed when/if another connection comes in. Normally, you spawn an independent handler (fork, thread, or add to async event handler) to handle new connections that does the work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app is expanding, but the reqs dictate that there should be only one
I've written a few desktop applications in .NET that provide both a front-end GUI
I'm expanding on what used to be a pretty simple jQuery accordion script, and
I am expanding forms that are created with Zend_Dojo , for this i have
I have a bunch of divs that I am expanding/retracting individually using .toggle and
Considering that the family of iDevices is expanding, I guess this is a problem
I have a poker game where I designed a nice pretty GUI that displays
The code below plots some simple x-y data, but it has two problems that
I got this idea of expanding my trace() messages. Why trace() is all over
I wanted to say best python GUI compared to Swing... but was sternly told

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.