telnet to Cognex In-Sight 4001 camera

Discussion in 'Python' started by chris.annin@gmail.com, Feb 25, 2013.

  1. Guest

    Hello, ive been struggling with this for a couple weeks now and was hopingsomeone might be able to help. I have an older Cognex camera that I need to communicate with via telnet. I can get a response from the camera when I initiate a telnet session but I dont seem to get any response when I write the user name to the camera - I havnt been able to get any response writing anything. Im using python 2.7 and windows xp. here is the code:
    Code:
    [color=blue][color=green][color=darkred]
    >>>import sys
    >>>import telnetlib
    >>>HOST = "10.31.18.21"
    >>>USER = "admin"
    >>>tn = telnetlib.Telnet(HOST)
    >>>tn.read_until("Login: ")[/color][/color][/color]
    "Welcome to In-Sight(R) 4001 Session 1\r\nUser:"[color=blue][color=green][color=darkred]
    >>>tn.write(USER + "\r\n")
    >>>tn.read_until("User: ")[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "C:\Python27\lib\telnetlib.py", line 319,
    return self.read_very_lazy()
    File "C:\Python27\lib\telnetlib.py", line 395,
    raise EOFError, 'telnet connection closed'
    EOFError: telnet connection closed[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]
    [\code]
    
    if i do a read_all instead of read_until for user I just get "..." returned..  Im assuming tn.write command isnt working?  any help would be greatly appreciated.
    
    thank you
    
    Chris
    , Feb 25, 2013
    #1
    1. Advertising

  2. MRAB Guest

    On 2013-02-25 17:02, wrote:
    > Hello, ive been struggling with this for a couple weeks now and was hoping someone might be able to help. I have an older Cognex camera that I need to communicate with via telnet. I can get a response from the camera when I initiate a telnet session but I dont seem to get any response when I write the user name to the camera - I havnt been able to get any response writing anything. Im using python 2.7 and windows xp. here is the code:
    >
    Code:
    [color=green][color=darkred]
    >>>>import sys
    >>>>import telnetlib
    >>>>HOST = "10.31.18.21"
    >>>>USER = "admin"
    >>>>tn = telnetlib.Telnet(HOST)
    >>>>tn.read_until("Login: ")[/color][/color]
    > "Welcome to In-Sight(R) 4001 Session 1\r\nUser:"[/color]
    
    The returned string ends with "User:", presumably the prompt to enter
    the username, which makes we wonder whether you should be reading until
    "User:" instead of until "Login: ".
    [color=blue][color=green][color=darkred]
    >>>>tn.write(USER + "\r\n")[/color][/color][/color]
    
    Should you be ending that with just "\r" or just "\n" instead? (That
    would be equivalent to typing the username and then pressing the Return
    key.)
    [color=blue][color=green][color=darkred]
    >>>>tn.read_until("User: ")[/color][/color][/color]
    
    Haven't you already seen "User: "?
    [color=blue]
    > Traceback (most recent call last):
    >    File "<stdin>", line 1, in <module>
    >    File "C:\Python27\lib\telnetlib.py", line 319,
    >      return self.read_very_lazy()
    >    File "C:\Python27\lib\telnetlib.py", line 395,
    >      raise EOFError, 'telnet connection closed'
    > EOFError: telnet connection closed[color=green][color=darkred]
    >>>>[/color][/color]
    > [\code]
    >
    > if i do a read_all instead of read_until for user I just get "..." returned.  Im assuming tn.write command isnt working?  any help would be greatly appreciated.
    >[/color]
    MRAB, Feb 25, 2013
    #2
    1. Advertising

  3. Guest

    On Monday, February 25, 2013 9:29:54 AM UTC-8, MRAB wrote:
    > On 2013-02-25 17:02, wrote:
    >
    > > Hello, ive been struggling with this for a couple weeks now and was hoping someone might be able to help. I have an older Cognex camera that I need to communicate with via telnet. I can get a response from the camera when I initiate a telnet session but I dont seem to get any response when I write the user name to the camera - I havnt been able to get any response writing anything. Im using python 2.7 and windows xp. here is the code:

    >
    > >
    Code:
    [/color]
    > [color=green][color=darkred]
    > >>>>import sys[/color][/color]
    > [color=green][color=darkred]
    > >>>>import telnetlib[/color][/color]
    > [color=green][color=darkred]
    > >>>>HOST = "10.31.18.21"[/color][/color]
    > [color=green][color=darkred]
    > >>>>USER = "admin"[/color][/color]
    > [color=green][color=darkred]
    > >>>>tn = telnetlib.Telnet(HOST)[/color][/color]
    > [color=green][color=darkred]
    > >>>>tn.read_until("Login: ")[/color][/color]
    > [color=green]
    > > "Welcome to In-Sight(R) 4001 Session 1\r\nUser:"[/color]
    > 
    > 
    > 
    > The returned string ends with "User:", presumably the prompt to enter
    > 
    > the username, which makes we wonder whether you should be reading until
    > 
    > "User:" instead of until "Login: ".
    > 
    > 
    > [color=green][color=darkred]
    > >>>>tn.write(USER + "\r\n")[/color][/color]
    > 
    > 
    > 
    > Should you be ending that with just "\r" or just "\n" instead? (That
    > 
    > would be equivalent to typing the username and then pressing the Return
    > 
    > key.)
    > 
    > 
    > [color=green][color=darkred]
    > >>>>tn.read_until("User: ")[/color][/color]
    > 
    > 
    > 
    > Haven't you already seen "User: "?
    > 
    > 
    > [color=green]
    > > Traceback (most recent call last):[/color]
    > [color=green]
    > >    File "", line 1, in [/color]
    > [color=green]
    > >    File "C:\Python27\lib\telnetlib.py", line 319,[/color]
    > [color=green]
    > >      return self.read_very_lazy()[/color]
    > [color=green]
    > >    File "C:\Python27\lib\telnetlib.py", line 395,[/color]
    > [color=green]
    > >      raise EOFError, 'telnet connection closed'[/color]
    > [color=green]
    > > EOFError: telnet connection closed[/color]
    > [color=green][color=darkred]
    > >>>>[/color][/color]
    > [color=green]
    > > [\code][/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > if i do a read_all instead of read_until for user I just get "..." returned.  Im assuming tn.write command isnt working?  any help would be greatly appreciated.[/color]
    > [/color]
    
    yea you are right I shouldnt be doing read_until("User: ") I tried read_until("Password: ") instead but didnt make any difference.  If I use read_all() I just get ,,, like there is nothing to read.  in hyperterminal I had to set the “Send Line Ends with Line Feeds” option for it to work.  I thought by using "\r\n" would emulate the same thing?
    , Feb 25, 2013
    #3
  4. Guest

    On Monday, February 25, 2013 9:29:54 AM UTC-8, MRAB wrote:
    > On 2013-02-25 17:02, wrote:
    >
    > > Hello, ive been struggling with this for a couple weeks now and was hoping someone might be able to help. I have an older Cognex camera that I need to communicate with via telnet. I can get a response from the camera when I initiate a telnet session but I dont seem to get any response when I write the user name to the camera - I havnt been able to get any response writing anything. Im using python 2.7 and windows xp. here is the code:

    >
    > >
    Code:
    [/color]
    > [color=green][color=darkred]
    > >>>>import sys[/color][/color]
    > [color=green][color=darkred]
    > >>>>import telnetlib[/color][/color]
    > [color=green][color=darkred]
    > >>>>HOST = "10.31.18.21"[/color][/color]
    > [color=green][color=darkred]
    > >>>>USER = "admin"[/color][/color]
    > [color=green][color=darkred]
    > >>>>tn = telnetlib.Telnet(HOST)[/color][/color]
    > [color=green][color=darkred]
    > >>>>tn.read_until("Login: ")[/color][/color]
    > [color=green]
    > > "Welcome to In-Sight(R) 4001 Session 1\r\nUser:"[/color]
    > 
    > 
    > 
    > The returned string ends with "User:", presumably the prompt to enter
    > 
    > the username, which makes we wonder whether you should be reading until
    > 
    > "User:" instead of until "Login: ".
    > 
    > 
    > [color=green][color=darkred]
    > >>>>tn.write(USER + "\r\n")[/color][/color]
    > 
    > 
    > 
    > Should you be ending that with just "\r" or just "\n" instead? (That
    > 
    > would be equivalent to typing the username and then pressing the Return
    > 
    > key.)
    > 
    > 
    > [color=green][color=darkred]
    > >>>>tn.read_until("User: ")[/color][/color]
    > 
    > 
    > 
    > Haven't you already seen "User: "?
    > 
    > 
    > [color=green]
    > > Traceback (most recent call last):[/color]
    > [color=green]
    > >    File "", line 1, in [/color]
    > [color=green]
    > >    File "C:\Python27\lib\telnetlib.py", line 319,[/color]
    > [color=green]
    > >      return self.read_very_lazy()[/color]
    > [color=green]
    > >    File "C:\Python27\lib\telnetlib.py", line 395,[/color]
    > [color=green]
    > >      raise EOFError, 'telnet connection closed'[/color]
    > [color=green]
    > > EOFError: telnet connection closed[/color]
    > [color=green][color=darkred]
    > >>>>[/color][/color]
    > [color=green]
    > > [\code][/color]
    > [color=green]
    > >[/color]
    > [color=green]
    > > if i do a read_all instead of read_until for user I just get "..." returned.  Im assuming tn.write command isnt working?  any help would be greatly appreciated.[/color]
    > [/color]
    
    yea you are right I shouldnt be doing read_until("User: ") I tried read_until("Password: ") instead but didnt make any difference.  If I use read_all() I just get ,,, like there is nothing to read.  in hyperterminal I had to set the “Send Line Ends with Line Feeds” option for it to work.  I thought by using "\r\n" would emulate the same thing?
    , Feb 25, 2013
    #4
  5. Guest

    On Monday, February 25, 2013 9:02:54 AM UTC-8, wrote:
    > Hello, ive been struggling with this for a couple weeks now and was hoping someone might be able to help. I have an older Cognex camera that I need to communicate with via telnet. I can get a response from the camera when I initiate a telnet session but I dont seem to get any response when I write the user name to the camera - I havnt been able to get any response writing anything. Im using python 2.7 and windows xp. here is the code:
    >
    >
    Code:
    > [color=green][color=darkred]
    > >>>import sys[/color][/color]
    > [color=green][color=darkred]
    > >>>import telnetlib[/color][/color]
    > [color=green][color=darkred]
    > >>>HOST = "10.31.18.21"[/color][/color]
    > [color=green][color=darkred]
    > >>>USER = "admin"[/color][/color]
    > [color=green][color=darkred]
    > >>>tn = telnetlib.Telnet(HOST)[/color][/color]
    > [color=green][color=darkred]
    > >>>tn.read_until("Login: ")[/color][/color]
    > 
    > "Welcome to In-Sight(R) 4001 Session 1\r\nUser:"
    > [color=green][color=darkred]
    > >>>tn.write(USER + "\r\n")[/color][/color]
    > [color=green][color=darkred]
    > >>>tn.read_until("User: ")[/color][/color]
    > 
    > Traceback (most recent call last):
    > 
    >   File "<stdin>", line 1, in <module>
    > 
    >   File "C:\Python27\lib\telnetlib.py", line 319,
    > 
    >     return self.read_very_lazy()
    > 
    >   File "C:\Python27\lib\telnetlib.py", line 395,
    > 
    >     raise EOFError, 'telnet connection closed'
    > 
    > EOFError: telnet connection closed
    > [color=green][color=darkred]
    > >>>[/color][/color]
    > 
    > [\code]
    > 
    > 
    > 
    > if i do a read_all instead of read_until for user I just get "..." returned.  Im assuming tn.write command isnt working?  any help would be greatly appreciated.
    > 
    > 
    > 
    > thank you
    > 
    > 
    > 
    > Chris[/color]
    
    After much goofing around I figured out that every time I read from this Cognex 4000 series camera the connection either goes dead or disconnects.  IfI simply stop trying to read_until or read_all and just write everything it works fine.  here is what worked for me:
    
    import sys
    import telnetlib
    host = "10.31.18.21"
    tn = telnetlib.Telnet(host)
    tn.write("admin\r\n") #the user name is admin
    tn.write("\r\n") #there is no password - just return - now logged in
    tn.write("SO0\r\n") #"SO"=cognex "set online" therefore "SO0" = camera offline
    tn.write("LFsomevisionjob.job\r\n") #"LF" = cognex native command "load file"
    tn.write("SO1\r\n") #"SO"=cognex "set online" therefore "SO1" = camera online
    tn.close()
    
    I doubt anyone has a camera this old they are trying to telnet to in pythonbut if so maybe this thread will help someone out.
    
    thanks again for all your replies - really appreciate you guys helping me out.
    Chris
    , Feb 26, 2013
    #5
  6. Roy Smith Guest

    In article <>,
    wrote:

    > On Monday, February 25, 2013 9:02:54 AM UTC-8, wrote:
    > > Hello, ive been struggling with this for a couple weeks now and was hoping
    > > someone might be able to help. I have an older Cognex camera that I need
    > > to communicate with via telnet. I can get a response from the camera when
    > > I initiate a telnet session but I dont seem to get any response when I
    > > write the user name to the camera - I havnt been able to get any response
    > > writing anything.


    Two suggestions I can make.

    One is to use the command-line telnet utility to talk to the camera.
    Verify that you can at least make a connection and authenticate that way.

    Second, use a packet sniffer to watch the actual traffic going back and
    forth to the camera. On a Unix box, I would do:

    $ tcpdump -s 0 -A host 10.31.18.21

    but it looks like you're on windows, so you'll want to use Wireshark
    (http://www.wireshark.org/) instead of tcpdump. If you're not familiar
    with TCP protocol details, you'll want to read up on it so you can
    properly interpret what tcpdump or Wireshark is telling you.
    Roy Smith, Feb 26, 2013
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newbie
    Replies:
    6
    Views:
    467
    The Artist Formerly Known as Kap'n Salty
    Feb 2, 2005
  2. Carcarius
    Replies:
    0
    Views:
    283
    Carcarius
    Dec 6, 2007
  3. Replies:
    0
    Views:
    105
  4. Chris Annin
    Replies:
    0
    Views:
    136
    Chris Annin
    Feb 25, 2013
  5. MRAB
    Replies:
    2
    Views:
    107
Loading...

Share This Page