Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Spoof /dev/ttyS0

by akujbida (Acolyte)
on Sep 24, 2008 at 11:06 UTC ( [id://713398]=perlquestion: print w/replies, xml ) Need Help??

akujbida has asked for the wisdom of the Perl Monks concerning the following question:

We have systems with RS-485 (ie serial) networks, and I'd like to simulate these devices. To achieve this without modifying our serial drivers, we must take over /dev/ttyS0. In other words, have something perlish listening on /dev/ttyS0 and providing responses that simulate an RS485 network device. Anyone seen or done anything like this? There's lots on talking to the serial ports; now I want to BE the serial port. Sincerely, Aleksander

Replies are listed 'Best First'.
Re: Spoof /dev/ttyS0
by grinder (Bishop) on Sep 24, 2008 at 14:05 UTC

    Point of linguistic order: you don't want to spoof /dev/ttyS0 because that would mean you want to fool it into accepting an alternate version of reality. In the context of a device, this might mean getting it to read or write bytes it wasn't meant to, which doesn't make a whole lot of sense.

    What you really want to do is to mock /dev/ttyS0, so that innocent bystanders think they are writing to a serial device driver, when in fact they're writing to your Perl program.

    At one level this is rather trivial, just move the existing /dev/ttyS0 out of the way and install a FIFO in its place. On the other hand, this might annoy real users of the real serial port.

    Can you not arrange your code to use some existsing test harness like Test::Device::SerialPort?.

    • another intruder with the mooring in the heart of the Perl

      Quite right about the terminology, thanks. FIFO is just the pointer I wanted to hear. I tried the example on p433 of the Camel, and it told me I "will visit the Dung Pits of Glive soon", which is the best news I've heard about this all day. Many thanks, I'm on the road. BTW, the test harness, which I've previously considered, is very nice but requires change to the drivers.
Re: Spoof /dev/ttyS0
by Corion (Patriarch) on Sep 24, 2008 at 13:10 UTC

    Possibly you can supply a different device name to your drivers and trick them into opening a pipe to your Perl simulator. Also consider whether you really want to do such a low-level emulation and can't (for example) override the read_serial() function in your program.

      Indeed, this is what I had been doing, but I am not happy about modifying the driver code, then having to have config setting somewhere to flip between test mode and production mode. There's several drivers and it starts to get messy, and introduces uncertainty about what is being tested (I did have an instance of getting all okay then finding out in the field that it was only okay with the mockup).
Re: Spoof /dev/ttyS0
by MadCat (Initiate) on Sep 25, 2008 at 05:21 UTC

    I've done something like this a long time ago to fool a DosEMU emulated app into thinking I had a real modem, I just can't recall the exact details but it worked by hooking your perl "emulator" to /dev/pty(a..z)(0..9) (pick one). This enables the pty's slave device on /dev/tty(a..z)(0..9).

    Different *nixes implement pseudo-terms differently, apparently on MacOS it's possible to have a /dev/ttyS0 backed by a /dev/ptyS0 but not entirely sure on that, it's been a while :)

    Anyhow, you can read/write to the pty from your perl script, and anything that goes there ends up on the corresponding slave term where the application you're testing can read/write from it, thinking you've got a network device behind it.

    As long as you can properly "emulate" things, it shouldn't be a problem.

    (It is, however, probably not the nicest way to do this)

      This also sounds attractive, but now that I have the amazingly simple FIFO up, I'll pass on trying to figure this out. Your suggestion, however, is much appreciated and copied to my file of solutions, in case I need it.
        Yeah, if a FIFO works then stick with that, it's a lot simpler to deal with. The pty/tty thing will work when an application expects to be able to do some specific ioctl calls on the device (such as DosEMU). I dug up some more info on what I did and it was basically to use DosEMU to run a DOS based BBS software and make it telnettable (by fooling the BBS software into thinking it was talking to a modem when in reality it was talking to a telnet<->"modem" gateway thing). Anyway, rambling on here... :D

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://713398]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 13:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found