views:

251

answers:

2

I know that I can use e.g. pySerial to talk to serial devices, but what if I don't have a device right now but still need to write a client for it? How can I write a "virtual serial device" in Python and have pySerial talk to it, like I would, say, run a local web server? Maybe I'm just not searching well, but I've been unable to find any information on this topic.

+1  A: 

It may be easier to using something like com0com (if you're on Windows) to set up a virtual serial port, and develop on that.

mtrw
+1  A: 

It depends a bit on what you're trying to accomplish now...

You could wrap access to the serial port in a class and write an implementation to use socket I/O or file I/O. Then write your serial I/O class to use the same interface and plug it in when the device is available. (This is actually a good design for testing functionality without requiring external hardware.)

Or, if you are going to use the serial port for a command line interface, you could use stdin/stdout.

Or, there's this other answer about virtual serial devices for linux.

Dave Bacher
Yes, write a "virtual serial device" class that duplicates the pySerial interface. Then your code can use either your "virtual device" class, or the real pySerial interface, interchangeably.
Craig McQueen