Python Forum
Serial to telnet using Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Serial to telnet using Python
#1
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?
Reply
#2
Charles you may have more luck in networking forum, have you seen the telnetlib module?
Reply
#3
You can look here: https://pythonhosted.org/pyserial/examples.html
You are already using this library.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 3,884 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  Python Serial: How to read the complete line to insert to MySQL? sylar 1 817 Mar-21-2023, 10:06 PM
Last Post: deanhystad
  python serial port barryjo 2 1,653 Dec-27-2021, 11:09 PM
Last Post: barryjo
  Telnet command in Python 3.9 Dre83 0 1,955 Nov-11-2020, 11:42 AM
Last Post: Dre83
  for loop script over telnet in Python 3.5 is not working abhijithd123 1 2,895 May-10-2020, 03:22 AM
Last Post: bowlofred
  Sending command using telnet mradovan1208 1 3,952 Apr-24-2020, 07:09 PM
Last Post: Larz60+
  Can't transmit serial fast Python to Arduino pyserial mRKlean 0 2,354 Mar-29-2020, 08:12 PM
Last Post: mRKlean
  Python Read/Write serial dlizimmerman 0 6,620 Jan-31-2019, 03:12 AM
Last Post: dlizimmerman
  Receive Serial Data and store in different Variables in Python jenkins43 5 5,608 Dec-28-2018, 01:33 PM
Last Post: snippsat
  Multi thread telnet issue anna 0 2,122 Mar-29-2018, 05:35 PM
Last Post: anna

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020