0

I need to send AT commands over ethernet to a device to configure it. Here is what I have right now:

import socket
import sys
import time

host = "192.168.0.99"
port = 8080

msg = 'at\n'

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.send(msg.encode())

data = s.recv(1024).decode()
print (data)
s.close()

I can putty to it, open a raw connection and it works.

putty

I type in "at", the controller echos "at" followed by an "ok" to acknowledge.

When I run the above script, I get "at" back. Is it printing the echo and not the following line (the ok line) or is it just printing out the command I sent out? How do I get it to read all the data that's received?

nearbyatom
  • 37
  • 4
  • 1
    That `.recv()` is going to return as soon as *any* data is available from the device - that `1024` is a maximum amount it will return, not an amount that it will wait for. If there is even a tiny delay between the echo and the command response, you're likely to get only the echo on the first call - and in fact, there's no guarantee that you'd even get the entire echo. You need to call `.recv()` in a loop. – jasonharper Aug 25 '20 at 13:26
  • Makes sense. So now I have this: – nearbyatom Aug 25 '20 at 13:46
  • try to terminate your AT command with `\r\n` – Aleksey Aug 25 '20 at 13:50
  • Adding the \r\n worked! What is \r? – nearbyatom Aug 25 '20 at 13:57
  • \r - carrier return, \n - new line – Aleksey Aug 25 '20 at 14:11
  • \r\n (CR+LF) is the line ending in DOS/Windows environment. A lot of devices use that instead of the Unix/Linux world's \n (LF). – Nyos Aug 25 '20 at 14:23
  • Regardless of what unix/dos/windows does, for *AT commands* the **only** relevant reference is the [V.250 standard](http://www.itu.int/rec/T-REC-V.250-200307-I/en). And it is leaves no room for doubt: "5.2.1 Command line general format": "A command line is made up of three elements: the prefix, the body, and the termination character." Notice here a *single termination character*, which value should be '\r' ("The termination character may be selected by a user option (parameter S3), the default being CR (IA5 0/13)."). – hlovdal Aug 26 '20 at 21:35

1 Answers1

0

How do I get it to read all the data that's received?

In general, after a program sends an AT command line to a modem, it should do absolutely nothing other than reading and parsing responses sent back from the modem until it receives a Final result code. The final result code indicates that the modem is finished processing the AT command line and is now ready to receive a new command line.

Your code for receiving data,

data = s.recv(1024).decode()

is just picking up one single, arbitrary chunk of data sent over the network (which might be split up in a number of different ways). As jasonharper already has commented, this must be done in a loop, specifically a loop that checks for and wait till it receives a final result code.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hlovdal
  • 26,565
  • 10
  • 94
  • 165