0

for getting in touch with Pyserial I wanted to set up a very small test environment. My basic idea was just opening two python consoles, one for writing at a port and one for reading from the same port. Unfortunately it did not work:

Here what I did:

Console 1:

import serial
serwrite = serial.Serial()
serwrite.port = 0
serwrite.open()
serwrite.write("testtext")
serwrite.close()

Console 2:

import serial
serread = serial.Serial()
serread.port = 0
serread.timeout = 15
serread.open()
a = serread.read()

but then the output of a is ' ' .

I am assuming that it is not that easy as I imagined. I based this test on the idea that a port is more or less like a pin I can write on and read from.

Thanks a lot!

Lukas
  • 1
  • 1
  • What return are you expecting from the read? – RageCage Jul 31 '14 at 16:15
  • there are software for faking serial ports ... but this code will not work ... http://com0com.sourceforge.net/ for example – Joran Beasley Jul 31 '14 at 16:18
  • I would expect "testtext" in a – Lukas Jul 31 '14 at 16:19
  • Is whatever connected to Port 0 writing back whatever you write to it? – RageCage Jul 31 '14 at 16:23
  • No, I have nothing connected to the port. As I have written, my idea was that the behaviour of a port is similar to a pin, where i publish things, not caring about if or how many are listening to it. But maybe that idea was too simple / wrong. – Lukas Jul 31 '14 at 16:29
  • Ooooh I get what you're trying to do. Something of that manner isn't possible with Pyserial to my knowledge. I'm pretty sure it's meant to speak to devices through serial ports, not write to them in the same way as pins. However, using that software posted earlier in the comments to simulate/fake a port, you may be able to accomplish what you're trying to do. – RageCage Jul 31 '14 at 16:31
  • Okay thanks a lot! Can you recommend by chance an ubuntu equivalent? ... found it by myself socat – Lukas Jul 31 '14 at 16:39
  • possible duplicate of [Virtual Serial Device in Python?](http://stackoverflow.com/questions/2291772/virtual-serial-device-in-python) – Matthew Rankin Jul 31 '14 at 17:49

1 Answers1

0

Thanks to @Joran and @Batman and this I found an easy way to test it (on ubuntu) by creating two virtual ports and bridge them.

Console 0

socat -d -d PTY: PTY:

which gives you two connected ports. In my case /dev/pts/6 and /dev/pts/8

Console 1

import serial
serwrite = serial.Serial()
serwrite.port = '/dev/pts/6'
serwrite.open()
serwrite.write("testtext")

Console 2

import serial
serread = serial.Serial()
serread.port = '/dev/pts/8'
serread.timeout = 15
serread.open()
a = serread.read()

well, a contains only a t due to no further options given in .read(), but the test setup for further testing works...

Lukas
  • 1
  • 1