Python Forum

Full Version: Serial to telnet using Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Most people need a telnet to serial adapter. That is, they need a program to act as a telnet host, and when you telnet in - you get connected to a serial device. They want their device to act as a telnet SERVER.

I need the opposite. I want to have a computer automatically telnet into a server, and then I want my serial port to be connected to that server. So I need my device to be a telnet CLIENT.

Since I can find no software that does what I want, I started looking at Python, and modified something that I found on the net. For right now, I enter the server and port # on the command line. Later I'll just hard-code it.

This is running on a Raspberry pi (Raspbian_)

# telnet program example
import socket, select, string, sys, serial

#main function

ser = serial.Serial('/dev/serial0', 115200, timeout = 1)

if __name__ == "__main__":

        if(len(sys.argv) < 3) :
                print 'Usage : python telnet.py hostname port'
                ser.close()
                sys.exit()

        host = sys.argv[1]
        port = int(sys.argv[2])

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(2)

        # connect to remote host
        try :
                s.connect((host, port))
        except :
                print 'Unable to connect'
                ser.close()
                sys.exit()

        print 'Connected to remote host'

        while 1:
                socket_list = [sys.stdin, s]

                # Get the list sockets which are readable
                read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])

                for sock in read_sockets:
                        #incoming message from remote server
                        if sock == s:
                                data = sock.recv(4096)
                                if not data :
                                        print 'Connection closed'
                                        ser.close()
                                        sys.exit()
                                else :
                                        #print data
#                                       sys.stdout.write(data)
                                        ser.write(data)

                      #incoming message                       
                        else :
#                               msg = sys.stdin.readline()
                                msg = ser.read(4096)
                                s.send(msg)
This almost works, but the "turn around" time is too long. I need to send/receive 60-100 byte strings, and the program seems to send only one byte at a time. That doesn't work.

Also, I need a timer that will try to automatically reconnect if no data is received in 10 seconds or so.

Can someone tell me how to fix this?
Charles you may have more luck in networking forum, have you seen the telnetlib module?
You can look here: https://pythonhosted.org/pyserial/examples.html
You are already using this library.