1

I would like to write some python code to download data from a pressure sensor we have, a Vaisala PTU300. This sensor connects to our router and can be contacted via a telnet session where simple commands spit out data on request, or it can spit out a continuous stream of data. Page 94 of the manual lists the commands you can send to the instrument. I'm quite new to python but do have some programming experience in Fortran so understand some structuring of programming languages.

I was hoping someone could help me with some ideas of what modules i should implement to do this. I've done a little research on the topic and found these past questions which have some relevance (How can I use python's telnetlib to fetch data from a device for a fixed period of time?, How to save the output of a telnet command by using python for an infinite time [closed]).

I have started to play with trying to use telnetlib to connect to the instrument with the following commands

import sys

import telnetlib

host = 'xxx.xxx.xxx.xxx'

tn=telnetlib.Telnet(host)

tn.open

tn.write('r')

tn.read_very_eager()

This results in one line of data being read

e.g.

2000-01-06 23:43:04 P= 980.0 T= -4.3 'C \r\n>r

however subsequent tn.read_very_eager() does not return anything.

I have tried different [variations][4] on this read command, e.g.

tn.read_until('C')

but any subsequent reads would not return anything. I believe some "blocking" might be occurring? not that i really know what that is.

I have also tried requesting just one reading using

tn.write('send')

tn.read_until('C')

but this would only return one reading and subsequent reads would return nothing.

[One][5] of the past stackoverflow questions suggested using the [asynchat][6] or [socket][7] modules to perform this kind of action. I have not tried using them yet. Do people think this is a better line of attack? The instrument requires no username or password to connect to it.

So my question would be, how can i best connect to this instrument (which package, telnetlib, asynchat, socket)?

How can I avoid the telnet connection being blocked? (i.e. only being able to retrieve one line of data)?

Any advice would be greatly appreciated, i appologise if this question has already been answered.

Regards

Oman

ps, sorry it wouldn't allow me to attach all my links

Community
  • 1
  • 1
Oman
  • 23
  • 4
  • I know I'm a little of topic, but have you tried (py)expect ? I find it a little easier to use when dealing with terminals – Lohmar ASHAR May 06 '17 at 23:48

2 Answers2

1

Thanks Jim,

Thanks for the tips, i actually ended up going with the socket module to connect, just because it seemed to offer more functions (not that i need them).

I figured out that my problem was that i wasn't sending a carriage return with my "send" command (as you had pointed out. What had been occurring is that the system would output one line of measurements when you connect to it. This would be the line i received. However when i sent a "send' command to it i never put the \r on the end and the command was not executed on the instrument.

For others who might google this in relation to their Vaisala PTU300 here's the code that works for me (I cut out a lot of the loops and just kept this basic). Note that i've put the insturment in STOP mode and turned ECHO off. I also found that the first few lines of data retrieved were hieroglyphs and i threw them into a variable called garbage.

import socket import sys

Load configuration parameters - to be imported from file in future

ip = 'xxx.xxx.xxx.xxx'

port = 23

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

s.connect((ip , port))

garbage = s.recv(1024)

SMODE set to STOP

s.send('send\r')

garbage = s.recv(1024)

s.send('send\r')

data = s.recv(128)

print data

Oman
  • 23
  • 4
0

I've been using telnetlib for a little over a week now, and here's what I can share.

To avoid the call to .read_until blocking, include the number of seconds to wait as a second argument. Like tn.read_until('C',10) will wait for 10 seconds for 'C' to show up in the buffer. If it finds it in 0.5 seconds, it will only block for 0.5 seconds. If it doesn't find 'C' in that time, it will return whatever is in the buffer so far, or '' if there's nothing.

In my case, the devices I'm connecting to use the DOS-style newline, which is '\r\n', so when I need to send a line with a newline, I have to use: tn.write('request\r').

Hope that helps some.

Jim N
  • 28
  • 4