0

I have a socket server running on an Arduino board and am trying to control it via a Python script. Using the basic example socket documentation, I have this:

import socket 
import sys
TCP_IP = '192.168.254.100'
TCP_PORT = 5012
BUFFER_SIZE = 1024
MESSAGE = "Status"
# Create a TCP/IP socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(MESSAGE)
# data = s.recv(BUFFER_SIZE)
# print (data)
s.close()
sys.exit()

My script works fine when I comment out the lines to receive the response of the server. However, if I attempt to read the response, my server and python script hangs.

On the server side, here is a snippet of what the code looks like:

void loop() {
 // listen for incoming clients
 client = server.available();
 if (client){
 Serial.println("Client connected");
 while (client.connected()){
 // Read the incoming TCP command
 String command = ReadTCPCommand(&client);
 // Debugging echo command to serial
 command.trim();
 Serial.println(command);
 // Debugging echo command back to client
 client.println(command);
 // Phrase the command
 PhraseTCPCommand(&client, &command);
 }
 // Stop the client
 client.stop();
 Serial.println("Client disconnected");
 }
 }

The library I am utilising for the server is the Arduino WiFi library. The function PhraseTCPCommand, takes the command and triggers external events with the GPIO pins of the board. This action is performed fine by the Python script when the recv() is commented out. The response string sent from the server is terminated with a newline and carriage return. Could that be causing issues?

Additionally, I am able to connect and receive responses from the server with no issues using either telnet, netcat or PuTTY, which leads me to believe it's something to do with the way my Python script attempts to read the response from the server.

asked Dec 7, 2015 at 22:52
1
  • 1
    Would you mind showing us your code for Arduino? Commented Dec 7, 2015 at 23:48

2 Answers 2

1

The response string sent from the server is terminated with a newline and carriage return. Could that be causing issues?

No, what is causing the issue is possibly that the command MESSAGE is not terminated with a newline and the function ReadTCPCommand() expects one. Change to:

MESSAGE = "Status\n"
answered Sep 1, 2017 at 13:44
Sign up to request clarification or add additional context in comments.

Comments

0

The issue here could be that your message has not been fully sent, and reflects a common misunderstanding of socket programming. Low level calls such as send do not promise to send the full message. Instead the caller must check the number of bytes actually sent (from the return value) and continue to iteratively call the function until the full message is sent.

In general, a bare send call without such iteration is a mistake.

Python does provides a higher level function socket.sendall however that will perform this job for you.

answered Dec 7, 2015 at 23:23

1 Comment

Thanks for that pointer, but I am unsure if that is the cause of my issue. When I monitor the server, I see that that message is received from the Python scrip, and the server functions as expected.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.