[Tutor] serial device emulator

Adam Bark adam.jtm30 at gmail.com
Tue Jul 5 14:18:27 CEST 2011


What Chris is getting at is that you'll use some module, eg pyserial, to 
interface with the serial port. So if you write a little module that has 
the same interface then you can pretend you have a serial port attached 
device and then switch over to an actual one without changing anything 
else in your code but the import.

## myserial.py ##
class Serial:
     def __init__(self, port=None):
         pass
     def open(self):
         pass
     def close():
         pass
     def read(size=1):
         return 'a' * size
     def write(data):
         return len(data)
#####

## foo.py ##
import myserial as serial
#import serial ## Use this in production

port = serial.Serial()
port.open()
print port.write("hello, world")
print port.read(3)
####

I hope that makes it clearer to you.
Adam.
P.S. none of that code has been tested


On 05/07/11 13:03, Edgar Almonte wrote:
> thanks chirs but i think  i don't follow you , can you elaborate more ?
>
> Thanks
>
> On Mon, Jul 4, 2011 at 8:49 PM, Chris Fuller
> <cfuller084 at thinkingplanet.net>  wrote:
>> You don't need to emulate a serial port (since you're writing the code; you'd
>> have to emulate the port if it was standalone software), only your serial port
>> library.  Write a class that has the same methods as the serial port library
>> you're using (you only need the methods you're using or think you might use
>> later), and fill them in with the appropriate code so it behaves like your real
>> device.
>>
>> Cheers
>>
>> On Monday 04 July 2011, Edgar Almonte wrote:
>>> Hello list need some advice/help with something, i am doing a program
>>> in python that send some command via serial to a device so far so good
>>> , the thing is not have the program so i need make another program
>>> that emulate the behavior of this serial device ( is quiet simple ) to
>>> test my app
>>> i read abou that i can use pseudo terminal in linux but not sure how
>>> attatch the pseudo terminal /dev/pts5 example to a fake_device.py file
>>> or something like that.
>>>
>>> maybe this question is not so python but i will appreciate some help.
>>>
>>>
>>> Thanks
>>> _______________________________________________
>>> Tutor maillist  -  Tutor at python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list