Reading USB port

N

news.microsoft.com

Hi Experts, I need help with USB port.

The task is very simple: A cash register in a supermarket is connectd to a
receipt printer throuhg a USB cable. Instead, the cable should go from the
cash register to another computer. The raw printer data is processed (we are
not interested in the processing),and sent furthere (again, we don't care
wgere).

I am completely new to USB programing. I imagin the method should look like
this:

private void Handle USBData()
{
Read USB port from configuration; (1)
Do Forever
{
try to connect to port; (2)
if (SUCCESS)
{
// Exit Connect loop when connected
break;
}

// Read hand handle data
Do Forever
{
if (STOP flag is raised OR connection to port lost)
{
// If the user terminates the program, or something wrong
happend with the port - Stop
break;
}

Read from port;
Handle data;
}
}


As you see, I have three questions:

(1) - How are USB ports named?

(2) - How do I cvonnect to a USB port? Is is some kind of OpenFile() like
with serial port?

(3) - Ho do I read data from a USB port? (gues: some kind of Read() from a
file).

Is there a good code example (preferably C#, but not mandatory) that
demonstrate this task?


Thanks in advance

Boaz Ben-Porat
Milestone Systems A/S
Denmark
 
P

Peter Duniho

[...]
(1) - How are USB ports named?

They're not. USB is used for a wide variety of things, and naming a USB
device is entirely dependent on the kind of device and the associated
driver.
(2) - How do I cvonnect to a USB port? Is is some kind of OpenFile() like
with serial port?

If you are dealing with a USB device that has a driver that exposes the
device as a serial port, then you connect just as you would to a serial
port. To your application, is _is_ a serial port. You can even use the
SerialPort class to access the device.

If you're not dealing with a USB device that has a driver that exposes the
device as a serial port, then it could be anything. Some USB devices act
like a removable disk. Others act like a network adapter. Still others
act like an audio device. Etc. In each case, assuming you have a driver
for the device, then you will access the device exactly as you'd access
any other identical kind of device.

If you don't have a driver for the device, then you will have to write
one. That's not something you're likely to be doing with .NET, but
however you do it, you then get to determine exactly how the application
will access the device, either by mapping the device to some known API in
the OS, or by providing your own.
(3) - Ho do I read data from a USB port? (gues: some kind of Read() from
a
file).

Depends entirely on how the USB winds up appearing to the application. If
the USB device is enabled as a serial port, then you can use the methods
on the SerialPort class to read from it.
Is there a good code example (preferably C#, but not mandatory) that
demonstrate this task?

No. There is far too much vagueness in the question for it to be possible
for any specific code example to exist.

Pete
 
B

Ben Voigt [C++ MVP]

news.microsoft.com said:
Hi Experts, I need help with USB port.

The task is very simple: A cash register in a supermarket is connectd
to a receipt printer throuhg a USB cable. Instead, the cable should
go from the cash register to another computer. The raw printer data
is processed (we are not interested in the processing),and sent
furthere (again, we don't care wgere).


First, you need a USB device controller. Almost all PCs with USB support
have host controllers only. Your cash register, expecting to talk to a USB
printer, cannot talk to a second host.
 
G

Greg Miller

The task is very simple: A cash register in a supermarket is connectd to a
receipt printer throuhg a USB cable. Instead, the cable should go from the
cash register to another computer. The raw printer data is processed (we are
not interested in the processing),and sent furthere (again, we don't care
wgere).

It's not going to be easy. You can get around accessing it at
the application level by writing your own USB driver for it. That'd at
least let you read all the data, but to send it on to another computer
and make it appear like the cash register, you'd have to develop your
own USB device hardware and drivers for both the sending and receiving
computer.
I suppose you could technically write your own driver for the
USB controller, and get around making any new hardware. But my guess
is, neither of those solutions are within the scope of what you planned
for this project. And you'd likely have a really tough time finding
enough information about your device to make it feasible.
There are programs which "spy" on the USB data. So you could
connect the cash register to the machine it's supposed to, then "spy"
the data and send it to another computer using typical networking calls.
But you'd still be hard pressed to make sense of the raw data.
 
T

Tim Roberts

news.microsoft.com said:
The task is very simple: A cash register in a supermarket is connectd to a
receipt printer throuhg a USB cable. Instead, the cable should go from the
cash register to another computer. The raw printer data is processed (we are
not interested in the processing),and sent furthere (again, we don't care
wgere).

You cannot do this in software. You need additional hardware. USB is a
protocol-based master/slave bus. On each bus, there is exactly one master.
When there is a PC, the PC is the master. ALL of the other devices are
slaves. You can't plug a PC port to a PC port without hardware in the
middle.

So, you would need to create a device with two USB connectors: one that
looked like the printer to the cash register, and one custom that you could
plug into your "other computer". Then, you could plug the printer into the
second computer.

You will have to have a good understanding of the USB and the USB Printer
Class specifications to do this.

If you're really interested in creating hardware to do this, contact me by
e-mail.
 
B

Ben Voigt [C++ MVP]

Greg Miller said:
It's not going to be easy. You can get around accessing it at
the application level by writing your own USB driver for it. That'd at
least let you read all the data, but to send it on to another computer
and make it appear like the cash register, you'd have to develop your
own USB device hardware and drivers for both the sending and receiving
computer.

The word "instead" in the original post makes me think this completely
replaces the existing register (host) <-> printer (device) connection. The
send to another computer should then be wrapping in a TCP socket or some
such. But I could be misunderstanding the original post which wasn't
perfectly clear.


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4236 (20090712) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
B

Ben Voigt [C++ MVP]

Tim Roberts said:
You cannot do this in software. You need additional hardware. USB is a
protocol-based master/slave bus. On each bus, there is exactly one
master.
When there is a PC, the PC is the master. ALL of the other devices are
slaves. You can't plug a PC port to a PC port without hardware in the
middle.

So, you would need to create a device with two USB connectors: one that
looked like the printer to the cash register, and one custom that you
could
plug into your "other computer". Then, you could plug the printer into
the
second computer.

You will have to have a good understanding of the USB and the USB Printer
Class specifications to do this.

You definitely need a USB client device (slave) emulating the printer, to
handshake with the cash register. Depending on the driver support in the
cash register, just any implementation of the printer class may not be
sufficient.

For the connection to the computer, you've got a number of options depending
on the data rate (USB as Tim suggested, Ethernet, RS-232, Bluetooth, etc).
Probably a second USB would be the cheapest in terms of hardware cost, while
RS-232 or Ethernet would probably be significantly less NRE.
If you're really interested in creating hardware to do this, contact me by
e-mail.
--
Tim Roberts, (e-mail address removed)
Providenza & Boekelheide, Inc.

__________ Information from ESET NOD32 Antivirus, version of virus
signature database 4236 (20090712) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4237 (20090712) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top