LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 04-12-2007, 05:52 PM   #1
Adony
Member
 
Registered: Apr 2004
Location: Portugal
Distribution: Gentoo 2004.2 - working on LFS 6
Posts: 126

Rep: Reputation: 15
Emulate/simulate an USB (or COM) device


Hello!


I need to do a program that reads and processes data coming from an USB(or COM) port using some kind of device that, in a perfect world, I would have physical access to it.
As I don't have the device while I am doing the program I want to emulate/simulate it so that the "faked device" can generate arbitrary data. That data would then be read and processed by the program.
The format of the data is not relevant at this time, all that I need is to simulate the device and throw bytes (or whatever it comes out of the ports).
I have done some search and could not find anything suitable to do the job.

Is anything out there that can do this for me?

Thanks in advance.

MM
 
Old 04-14-2007, 01:32 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
If your plan is to access the USB or COM device through it's '/dev' file pseudo device, then you could create a virtual device using a virtual filesystem. The fuse (filesystem in userspace) system can be used to do this. Using fuse, you create a virtual filesystem, which is simply a defined set of callbacks that implement the services performed by all of the usual filesystem functions: open, read, write, close, seek, stat, ioctl, etc. In your case, most of these will be empty stubs that just return success status codes. The read(), write(), and perhaps ioctl() services will do most or all of the work, simply swallowing written data (maybe examining and storing it), and producing your fake data in response to read() requests. The emulation of character devices using this block oriented filesystem approach may be sub-optimal, with respect to such things as timing issues, device configuration (eg baud, parity, etc.).
I don't know of any standard virtual character device.
I believe I understand your overall intent, and now I will tell you my story, which I include mainly to provoke some thought, but not saying this is how you should proceed.
I once tried to create a virtual instrument that talked on RS-232. My code was intended to to be a general implementation that could emulate almost any instrument by receiving character data on a serial port, and responding with appropriate actions or replies. My approach used a configuration file that defined a set of instrument 'emulations'. An emulation was a set of 'behaviors', which are simply regular expressions that are matched on the input stream, and an associated set of actions performed in response. Each behavior has a set of three potential actions that can be performed. One action is to echo something to the console; could be a constant/literal string, or it could have some component(s) of the incoming matched data. The second potential action would be to define a format string for any possible reply to be emitted. This string would be a 'printf()' compatible format string, with '%' placeholders for reply data. The last possible action would be a list of 'things to do with the data'. This could be to store the data or some part of the data in a named 'register', manipulate the data arithmetically, or 'emit' something (send response data out the serial port). The tool was developed, never documented or fully debugged, and because it never got finished in time for the intended purpose, abandoned. Much of the work was done using lex and yacc, and some features never did work as I intended. I include a sample configuration file, as an example of some emulations and their respective behaviors. These emulations actually worked to emulate a high voltage power supply, the exact description of which I forget.
Code:
#
# Configuration file for 'Serem': serial device emulation tool
#
[serial port]
    name = /dev/ttyS1
    baudrate = 9600
    parity = none
    stopbits = 1
    databits = 8



[device emulation]
    name= pseudoh
    #
    # Behavior = regex( stimulus ),   string( echo ),           format_string( reply ),     action-keyword [arg0[,argn..]]
    #            from device         to text console            reply to device             function to perform
    #
    behavior = /U([0-9.+-Ee]+)/, "Voltage setpoint command",        "",                 "register(Voltage) = $"
    behavior = /I([0-9.+-Ee]+)/, "Current setpoint command",        "$",                "register( Current ) = ($)"
    behavior = /F([01])/,          "On/Off command",                "",                 NOTHING
    behavior = /P([01])/,         "Polarity command",               "",                 NOTHING
    behavior = /N0/,         "Readback mode command",          "",                 "register(Readback) = register(Voltage)"
    behavior = /N1/,         "Readback mode command",          "",                 "register(Readback) = register(Current)"
    behavior = /\?/,            "Trigger readback command",         "",                 "emit register(Readback)"

[ device emulation                ]
    name=kwozzi
    behavior = /U[0-9.+-Ee]/, "Voltage setpoint command", "", NOTHING
    behavior = /I[0-9.+-Ee]/, "Current setpoint command", "", NOTHING
    behavior = /P[01]/, "Polarity command", "", NOTHING
    behavior = /N[01]/, "Readback mode command", "", NOTHING


[ device emulation ]
    name =Big Shiny Black Box model 123XYZ
    behavior = /U[0-9.+-Ee]/, "Voltage setpoint command", "", NOTHING
    behavior = /I[0-9.+-Ee]/, "Current setpoint command", "", NOTHING
    behavior = /P[01]/, "Polarity command", "", NOTHING
    behavior = /N[01]/, "Readback mode command", "", NOTHING
    behavior = /F[01]/, "On/Off command", "", NOTHING
Suffice to say, I will be watching with interest to see what other people have done in this area. I hope my ramblings are not too far out in left field.
--- rod.
 
Old 04-14-2007, 02:17 PM   #3
lazlow
Senior Member
 
Registered: Jan 2006
Posts: 4,362

Rep: Reputation: 172Reputation: 172
Wouldn't it just be easier to get a usb flash drive and plug it in? Just a thought.
 
Old 04-14-2007, 02:23 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by lazlow
Wouldn't it just be easier to get a usb flash drive and plug it in? Just a thought.
Easier, yes. But how would it serve the purpose or emulating or simulating anything?
--- rod.
 
Old 04-15-2007, 04:51 PM   #5
Adony
Member
 
Registered: Apr 2004
Location: Portugal
Distribution: Gentoo 2004.2 - working on LFS 6
Posts: 126

Original Poster
Rep: Reputation: 15
Thanks for you help theNbomr I think I can do something with that info
What I am trying to achieve is a virtual device for only emitting data it doesn't have to worry about some kind of input data.
I will see what I can do!
Thanks again.
 
  


Reply

Tags
emulation, usb


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
USB drive not working properly, read only device? (USB key storage device) Arodef Linux - Hardware 14 01-01-2010 08:32 AM
Linux Mini Distro Suggestion: Emulate USB Flash Disk layanto Linux - Software 4 04-25-2009 04:54 PM
Emulate USB floppy to boot from USB.. feasible? Q4U Linux - Hardware 1 01-31-2007 04:24 PM
use linux to simulate usb thumb drive enocompton Linux - Software 5 09-19-2006 11:10 AM
device to simulate physical memory with PCI device kevinsung Linux - Software 1 12-14-2002 11:11 AM


All times are GMT -5. The time now is 05:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration