Discussion:
how to read usb port
(too old to reply)
Ivan
2006-07-07 23:42:09 UTC
Permalink
I am working on a project trying to setup a telemetry system on a Mac
OS X. What I am currently trying to accomplish is reading a continuous
stream of data from the USB port and have it display on the console. I
am currently using Apple's sample code in order to access a USB serial
adapter controller.

http://developer.apple.com/samplecode/USBPrivateDataSample/index.html

After modifying the vendor and product ID to that of the controller I
have gotten it to work like it should. However, the current problem I
am having is that I am not familiar on how to read the USB port once it
has been detected. I have tried creating methods to read the USB port
using ReadPipe but I get an error once its executed and Xcode opens a
debugger window with assembly code. The type of code method I tried
implementing was like the section of "Working With Interfaces" from
Apple's USB Device Interface Guide. Like this:

kr = (*interface)->ReadPipe(interface, 9, gBuffer, &numBytesRead);

Can anybody help me out with some pseudo code or a sample method I
could use as reference?

Ivan
Paul Russell
2006-07-08 08:03:11 UTC
Permalink
Post by Ivan
I am working on a project trying to setup a telemetry system on a Mac
OS X. What I am currently trying to accomplish is reading a continuous
stream of data from the USB port and have it display on the console. I
am currently using Apple's sample code in order to access a USB serial
adapter controller.
http://developer.apple.com/samplecode/USBPrivateDataSample/index.html
After modifying the vendor and product ID to that of the controller I
have gotten it to work like it should. However, the current problem I
am having is that I am not familiar on how to read the USB port once it
has been detected. I have tried creating methods to read the USB port
using ReadPipe but I get an error once its executed and Xcode opens a
debugger window with assembly code. The type of code method I tried
implementing was like the section of "Working With Interfaces" from
kr = (*interface)->ReadPipe(interface, 9, gBuffer, &numBytesRead);
Can anybody help me out with some pseudo code or a sample method I
could use as reference?
If it's just a normal USB-serial adapter that you're using (e.g.
Keyspan) then you shouldn't going going direct to USB - just treat it
like a normal serial port - the USB driver will take care of the serial
port emulation.

Paul
Ivan
2006-07-10 22:06:01 UTC
Permalink
Thanks for the fast response. I spent my lunch break implementing the
reading of the port using the serial port method, but I am still having
problems reading data. I dont know if this is the problem, but I am
using a serial/usb game controller just to test the code. I did change
the product and vendor ID to this controller. Anyways, I tried opening
ttys0 through ttys5 and ttyu0 through ttyu5, but my data output is
always 0. I'll try to do it again when I get home with the actual
telemetry system to make sure its not the game controller, but if any
of you guys notice something wrong with this method can you point it
out.

void ReadPort()
{
int fd, data;
char *result;

printf("Step 1: Open port\n");
fd = open("/dev/ttyu0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
printf("File open failed\n");
return 0;
}

printf("Step 2: Read Data\n");

data = read(fd, result, 254);

printf("Step 3: Output Data\n");

printf("Data: %d\n", data);

printf("Step 4: Close port\n");

close(fd);

}
Patrick Machielse
2006-07-10 22:41:34 UTC
Permalink
if any of you guys notice something wrong with this method can you point
it out.
void ReadPort()
{
int fd, data;
char *result;
[]
data = read(fd, result, 254);
You have to allocate a result buffer before you can successfully read
into it.

char result[254];

This probably won't solve your problem, but I think it's a good start
;-)

patrick
Reinder Verlinde
2006-07-11 16:17:12 UTC
Permalink
Post by Ivan
Thanks for the fast response. I spent my lunch break implementing the
reading of the port using the serial port method, but I am still having
problems reading data. I dont know if this is the problem, but I am
using a serial/usb game controller just to test the code. I did change
the product and vendor ID to this controller. Anyways, I tried opening
ttys0 through ttys5 and ttyu0 through ttyu5,
Chances are that your port is not called even remotely like that. It
probably looks more like /dev/cu.whatever or /dev/tty.whatever. The
right way to discover this is to ask the system. I think
<http://developer.apple.com/samplecode/SerialPortSample/index.html>
should have all info you need.

Reinder
Ivan
2006-07-13 05:31:18 UTC
Permalink
Okay guys after many attempts to access the my usb port I think i found
the problem. I think its because I never installed the drivers for the
usb/serial controller. That's because i figure that because Mac OS X
recognized in under system profiler, there was no reason to install any
drivers. So then I started doing a search for Mac OS X 10.4.7 drivers
for my prolific tech chip controller model cp-us-03. I know, dont get
upset, but I purchased this adapter during early stages of working on
this project before i read the comments about keyspan. It seems that
they arent supporting the latest versions of Mac OS X for this
controller from this website i found. Just my luck.

http://www.perceptiveautomation.com/phpBB2/viewtopic.php?t=240

So because i didnt install the controller drivers i never got a
/dev/tty.usbSomething that i could use to access the port. Could
someone please vouch this before i go out and buy the keyspan model
USA-19HS.
Ivan
2006-07-18 05:10:52 UTC
Permalink
Wow! That was it! I can't believe I made this project more
complicated that it should be. Thanks to all who responded. Once I
install the Keyspand USA-19HS and the drivers, a new tty.Keyspand1 port
became availabe in /dev. However, another problem was using the game
controller to test my code. By using Windows Hyper Terminal I was able
to capture the output I sent out through the USB port on the Mac. I
just had to make a null modem cable in order to connect serial/usb
converter to the machines COM port. Hopefully, this discussion will
help others. Thanks again.
Paul Russell
2006-07-18 09:26:39 UTC
Permalink
Post by Ivan
Wow! That was it! I can't believe I made this project more
complicated that it should be. Thanks to all who responded. Once I
install the Keyspand USA-19HS and the drivers, a new tty.Keyspand1 port
became availabe in /dev. However, another problem was using the game
controller to test my code. By using Windows Hyper Terminal I was able
to capture the output I sent out through the USB port on the Mac. I
just had to make a null modem cable in order to connect serial/usb
converter to the machines COM port. Hopefully, this discussion will
help others. Thanks again.
Glad to hear you got it working Ivan.

Paul

David Phillip Oster
2006-07-08 18:36:45 UTC
Permalink
Post by Ivan
I am working on a project trying to setup a telemetry system on a Mac
OS X. What I am currently trying to accomplish is reading a continuous
stream of data from the USB port and have it display on the console. I
am currently using Apple's sample code in order to access a USB serial
adapter controller.
http://developer.apple.com/samplecode/USBPrivateDataSample/index.html
After modifying the vendor and product ID to that of the controller I
have gotten it to work like it should. However, the current problem I
am having is that I am not familiar on how to read the USB port once it
has been detected. I have tried creating methods to read the USB port
using ReadPipe but I get an error once its executed and Xcode opens a
debugger window with assembly code. The type of code method I tried
implementing was like the section of "Working With Interfaces" from
kr = (*interface)->ReadPipe(interface, 9, gBuffer, &numBytesRead);
Can anybody help me out with some pseudo code or a sample method I
could use as reference?
As Russell pointed out, since you are using a serial adapter, you can
just treat this as a serial device. Apple has sample code on using IOKit
to discover which serial ports the USB adapter publishes. Once you have
the name of a port, you just open() it and read() from it. You can use
select() to poll until either there is data to read or it times out, and
ioctl(... FIONREAD ...) to find out how many bytes have been buffered
up, so you'll know how much you can read() before read() blocks, making
you wait for more data.

I've also used Apple's HID Explorer source code example for inspiration
to install callbacks that get called when USB devices send packets of
data (You have to be careful about runloop modes.) for joysticks, a
different kind of data generating usb device.

For higher bandwidth A to D bytestreams, Quicktime Sequence Grabbers are
a convenient API that let you discover and read from audio and video
inputs. Apple's Whack TV is a good example here.
Russell Sheptak
2006-07-09 18:02:37 UTC
Permalink
Post by David Phillip Oster
Post by Ivan
I am working on a project trying to setup a telemetry system on a Mac
OS X. What I am currently trying to accomplish is reading a continuous
stream of data from the USB port and have it display on the console. I
am currently using Apple's sample code in order to access a USB serial
adapter controller.
http://developer.apple.com/samplecode/USBPrivateDataSample/index.html
After modifying the vendor and product ID to that of the controller I
have gotten it to work like it should. However, the current problem I
am having is that I am not familiar on how to read the USB port once it
has been detected. I have tried creating methods to read the USB port
using ReadPipe but I get an error once its executed and Xcode opens a
debugger window with assembly code. The type of code method I tried
implementing was like the section of "Working With Interfaces" from
kr = (*interface)->ReadPipe(interface, 9, gBuffer, &numBytesRead);
Can anybody help me out with some pseudo code or a sample method I
could use as reference?
As Russell pointed out, since you are using a serial adapter, you can
just treat this as a serial device. Apple has sample code on using IOKit
to discover which serial ports the USB adapter publishes. Once you have
the name of a port, you just open() it and read() from it. You can use
select() to poll until either there is data to read or it times out, and
ioctl(... FIONREAD ...) to find out how many bytes have been buffered
up, so you'll know how much you can read() before read() blocks, making
you wait for more data.
I've also used Apple's HID Explorer source code example for inspiration
to install callbacks that get called when USB devices send packets of
data (You have to be careful about runloop modes.) for joysticks, a
different kind of data generating usb device.
For higher bandwidth A to D bytestreams, Quicktime Sequence Grabbers are
a convenient API that let you discover and read from audio and video
inputs. Apple's Whack TV is a good example here.
To read serial data, you can start with the AMSerialPort class available
at:

http://www.harmless.de/cocoa.html

rus
Continue reading on narkive:
Loading...