0

I'm having some trouble with talking to a cisco device over serial using pyserial. I'm getting the response I expect some times, but not always, and I have no idea what I'm doing wrong. Sometimes I just get empty responses multiple times in a row. I'm running windows 7, python 2.7, pyserial 2.7.

Here's some sample code to get some response.

ser = serial.Serial("COM20")
ser.timeout = 2
ser.parity = 'N'
ser.write("\nenable\nsh ip int brief\n")
print ser.read(10**8)

The responses I get look something like this, as expected: Router(config)#

The settings required by cisco:

Bits per sec    :  9600 
Data bits       :     8 
Parity          :  none 
Stop bits       :     1 
Flow control    :  none 

As far as I can tell, they match the defaults for pyserial.

Filip Haglund
  • 13,919
  • 13
  • 64
  • 113

1 Answers1

0

Perhaps the baudrate is incorrect? It defaults to 9600. If that is incorrect, try setting it with the keyword argument:

ser = serial.Serial("COM20", baudrate=<baud rate here>)

An incorrect baudrate can effect readings in strange ways.

Philip Massey
  • 1,401
  • 3
  • 14
  • 24
  • There's a chance that the device operates slowly over serial? What happens if you add a time.sleep(1) call in between the write and read? It is consistent then? What about time.sleep(10)? The fact that is it returning multiple times makes it seems like you're trying to read before the buffer is filled, then when you read a second time you get both. – Philip Massey Jul 25 '14 at 17:51
  • I'm sending multiple commands, to login and such. I've gotten it to work using telnet, but the serial connection is still like this. It takes about 0.3 seconds to receive the response I'm expecting, so the 5 seconds I tried with should be enough. Also, I'm getting empty responses multiple times in a row for different commands. – Filip Haglund Jul 25 '14 at 19:05
  • That's strange. Although I don't think it's to do with PySerial. Trying using a tool to monitor the comport traffic. You can use this to see if Python is failing to send / sending too much. Also, when do you wait the 5 seconds? I don't see it in the code above. – Philip Massey Jul 25 '14 at 19:37