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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-12-2007, 04:52 PM
|
#1
|
|
Member
Registered: Apr 2004
Location: Portugal
Distribution: Gentoo 2004.2 - working on LFS 6
Posts: 126
Rep:
|
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
|
|
|
|
04-14-2007, 12:32 PM
|
#2
|
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,259
|
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.
|
|
|
|
04-14-2007, 01:17 PM
|
#3
|
|
Senior Member
Registered: Jan 2006
Posts: 4,362
Rep: 
|
Wouldn't it just be easier to get a usb flash drive and plug it in? Just a thought.
|
|
|
|
04-14-2007, 01:23 PM
|
#4
|
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,259
|
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.
|
|
|
|
04-15-2007, 03:51 PM
|
#5
|
|
Member
Registered: Apr 2004
Location: Portugal
Distribution: Gentoo 2004.2 - working on LFS 6
Posts: 126
Original Poster
Rep:
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:59 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|