f



Serial port and USB

I read some text about serial port and still confused whether USB is a serial port method(or just a serial communication?,although i dont know the difference).Besides this, I wonder whether we can get input from USB using MATLAB.For instance,can I catch the pushes of the user that uses a joystick (like the ones that people use for soccer games) using MATLAB?

Thanks in advance for your future answers..
0
engin
12/6/2010 10:02:05 PM
comp.soft-sys.matlab 211265 articles. 25 followers. lunamoonmoon (257) is leader. Post Follow

9 Replies
776 Views

Similar Articles

[PageSpeed] 19

On 10-12-06 04:02 PM, engin caglar wrote:
> I read some text about serial port and still confused whether USB is a
> serial port method(or just a serial communication?,although i dont know
> the difference).Besides this, I wonder whether we can get input from USB
> using MATLAB.For instance,can I catch the pushes of the user that uses a
> joystick (like the ones that people use for soccer games) using MATLAB?

USB is not in itself a serial port. USB is "Universal Serial Bus", and it is 
hardware and protocols for sharing serial bandwidth between several different 
devices.

One variety of device that is not uncommon with USB is USB to Serial port 
converters. Because of the design of USB, it is not possible to get serial 
port data over USB immediately (like with a real serial port): the serial port 
device has to wait until the next time that it is specifically asked if it has 
available data, or at least until the next time the USB bus asks whether any 
devices want to interrupt.

With real serial ports, there are a number of analog signals carried on 
individual wires. With a USB based USB to serial converter, the more important 
of those analog signals are sampled, 1 bit per line, with 8 different signals 
available. For example, where on a real serial port, RTS/DSR would fluctuate 
up and own according to whether the port was ready to send data, on USB you 
would just get occasional packets saying "Yup, RTS/DSR is still high." *Not* 
what you want for handling real serial ports.

User presses of joystick buttons get transformed in to a small amount of data. 
I haven't read up on the protocol in a while, but I seem to remember that the 
last time I looked, it was common for joysticks to report back 3 bytes of data 
per press; if these are more advanced joysticks with multiple buttons or which 
can detect Z as well, then 4 or even 6 bytes would not surprise me.

USB serial port buffer data, and send it along when the buffer is full, or 
when there has been no new data for a period (40 ms I think it is.) The more 
intensely you use the joystick, the more likely there are to be additional 
events that reset the 40 ms timer, leading to increased delays in sending the 
data. Joystick data is not usually time-stamped.

There is an alternative USB "interrupt" mode that can carry up to 8 bytes of 
data; it still requires that the controller specifically ask if there is 
anything waiting. There is, though, no mechanism for a device such as a 
joystick to somehow interrupt a transfer that is already going on (e.g., 
to/from a hard drive), and no mechanism for a device to signal the controller 
"I know you're busy now, but as soon as the bus is free, I have something for 
you". I do not know enough about Matlab's USB control to know if this mode is 
supported.

It is also theoretically possible for the controller to send a serial device a 
signal saying, "Send me whatever you have in the buffer so far", without 
waiting for the buffer to fill and without waiting for the 40 ns of no data; I 
have not seen any hints of it being possible to configure this kind of 
"polling" and I don't think it is supported (but I don't know.)
0
Walter
12/6/2010 10:27:56 PM
Thanks for your long and revealing answer and for your time.As you mentioned,I will look for a USB-serial converter and try to read the data using MATLAB.I will give feedback if I success.
0
engin
12/7/2010 11:07:04 AM
I thought such a solution below,can you give me suggestion whether it is applicable or not?
Assume that somehow i managed to write data coming from joystick to a file named joy.txt continuously.For instance ,if we open the file we will see something like
u
r
u
d
s
as the user presses buttons,which means up arrow button,right arrow button,down arrow button,start button ,respectively.And think that using matlab we read data from that file continuously.What i consider as a problem is,since i didnt experience before,confliction.In other words what if i try to right the data to that file (with another application),and matlab tries to open the file at the same time?Also,is deleting the lines from the data file as we read using matlab a good solution?
0
engin
12/7/2010 9:04:04 PM
On 10-12-07 03:04 PM, engin caglar wrote:
> I thought such a solution below,can you give me suggestion whether it is
> applicable or not?
> Assume that somehow i managed to write data coming from joystick to a
> file named joy.txt continuously.For instance ,if we open the file we
> will see something like
> u
> r
> u
> d
> s
> as the user presses buttons,which means up arrow button,right arrow
> button,down arrow button,start button ,respectively.

Could be done, but joy-sticks most often used for two-axis differential 
positioning (e.g., hold toward left to keep going left), which needs either X 
& Y coordinates or two angle coordinates (or one angle and one radius.) You 
would thus need to enhance the above with coordinates.

> And think that using
> matlab we read data from that file continuously.What i consider as a
> problem is,since i didnt experience before,confliction.In other words
> what if i try to right the data to that file (with another
> application),and matlab tries to open the file at the same time?

That is a problem in Windows. In Unix-type systems (e.g., Linux), that is not 
a problem (or at least the nature of the problem changes.) There is, I 
understand, a Windows utility that can "unlock" a file, allowing it to be used 
by multiple programs simultaneously. I admit to some confusion in this area, 
as the documented behaviour of Windows XP SP2 and later is to be POSIX 
compliant, and POSIX compliance requires that mandatory locking not be the 
default.


 > Also,is
> deleting the lines from the data file as we read using matlab a good
> solution?

No, deleting variable-length lines "in place" from a text file is quite 
difficult; deleting fixed-length lines or fixed-length binary data from a file 
is not _necessary_ (as long as you keep track of which parts are in use.)


Using a file as an intermediate buffer would not solve any of the difficulties 
I described before, and would make the timing difficulties worse. For example, 
in the use of a joystick it is not only important to know that one button is 
pressed and then another one is pressed: it is important to know the relative 
times at which they were pressed. You press the fire button, you move the 
joystick to the right, you wait just a little and you fire again and you hit 
the Space Invader just so; without that pause, your shot whizzes to the side 
of the Invader.

If you were to time-stamp everything and you were wanting to "replay a game" 
later rather than play it in real-time, then a file as a buffer would make 
sense; it would not make much sense otherwise.

If the issue was that you had some program that could access the data in some 
way that matlab could not, then using a pipe would probably make more sense 
than a file as a buffer.

The program that would write the file: where would it be? If it is on the same 
system that Matlab is running on, the two programs would be competing for 
control of the CPU. If the program was on a different system, then in order 
for the file to be accessible, you would need to be using a networked file 
system, which tends to be problematic for real-time use.


Checking some of my previous postings, I found that a few years ago I wrote,

"You can also use joysticks with the Aerospace blockset,
or the Virtual Reality Toolbox, and you can use analog joysticks
with the Real Time Workshop.

There are also facilities available with the psychophysics.org
toolbox, most directly if you are using OS-X. "

0
Walter
12/7/2010 10:15:26 PM
> Could be done, but joy-sticks most often used for two-axis differential ..........
Actually , i dont know what kind of data joysticks send,I will deal with data  type after I solve basic problems related to this method.


> That is a problem in Windows....................
I'm using Matlab on windows XP.I hope that issue you mentioned wont be a problem for me.

> No, deleting variable-length lines "in place" from a text file is quite 
> difficult..........................
So,instead of using  plain text file should i use binary formatted file and not delete lines?
Also , is it possible to create a virtual serial port to write data,and then use matlab to read that data?What i mean is actually there wont be any device that will send data from serial port,but instead joystick will send data from USB,and the mysterious program that i will write take it and write to that port or buffer.


> The program that would write the file: where would it be? If it is on the same 
> system that Matlab is running on, the two programs would be competing for 
> control of the CPU............................
I 'm familiar with SDL(simple directmedia layer) a little bit.It allows to track user inputs (including joystick support).So,I thought that maybe I write a little program(indeed i can) with SDL as executable that will open  a small window and  ONLY track joystick events(wont check mouse or keyboad,which may conflict with matlab event queue?) and will write the obtained data in a file.I dont know if  there is a more efficient way of it,I mean if that application can send those obtained data to matlab in a better/fast/organized way,since we are no more dealing with USB in that case.I think that program can be called from matlab using system('...&') to run it in back of matlab. 

> Checking some of my previous postings, I found that a few years ago I wrote,
> 
> "You can also use joysticks with the Aerospace blockset.....

Yes i saw such a post in my search (maybe it was yours) and immediately checked it in simulink.But after searching i found that its not useful for my case(i remember that the problem is USB) ,and really related to aerospace applications.
The joystick that I will use is an ordinary one and something like that (used for soccer games)  
http://www.galeria-kaufhof.de/images/1031/4/9/44292.jpg


Again thanks for your time & long answer.
0
engin
12/7/2010 11:35:22 PM
By the way,let me add 2-3 things to my last post,hoping that you will answer them together.(Since noone else seems interested in the problem)

Firstly,Im planning to use that joystick for the control of 3D motion mechanism(not for a game or graphics).
Second,as you mentioned in your last answer,I dont need exact time of joystick pushes.I mean no need "real time" data acquisition from usb.For example ,up to 0.5 second latency between the push and actual mechanism motion is acceptable for me.
Finally,is there a way of talking with MATLAB and saying "Do whatever you should,and whenever you are free read data from xxx.txt" ?If so,can you give a small code example for such a purpose,maybe in a loop?

Thanks in advance...
0
engin
12/8/2010 2:19:24 AM
On 07/12/10 5:35 PM, engin caglar wrote:

> Actually , i dont know what kind of data joysticks send,I will deal with
> data type after I solve basic problems related to this method.

> The joystick that I will use is an ordinary one and something like that
> (used for soccer games)
> http://www.galeria-kaufhof.de/images/1031/4/9/44292.jpg

http://blog.eu.playstation.com/2010/09/16/the-evolution-of-the-playstation-controller/

Comparing the images, I can see that what you have pictured is a Sony 
Playstation Controller, either the Dual Shock or DualShock2 model. I 
cannot tell with certainty which of the two models you have, but the 
color is an exact match for the image at 
http://ecx.images-amazon.com/images/I/412Q3RFHZVL.jpg which is for the 
DualShock2 Black. The DualShock2 is old enough and not significantly 
more expensive than the Dual Shock, so I believe it likely the 
DualShock2 has effectively replaced the Dual Shock at the retail level. 
We can, though, see clearly that you do not have the DualShock3 
(wireless) version, as the DualShock3 does not have the "analog" button.

Playstation.com says this of the DualShock2:

"It looked the same as its predecessor but the first PlayStation 2 
controller was built for accuracy. Its stiff analogue sticks tracked 
degrees of movement and pressure, allowing games like Gran Turismo 4 to 
become masterpieces of realistic physics, and the textured analog sticks 
were built with greater grip in mind. Also, the face buttons were 
pressure sensitive so that your PS2 could detect how firmly you pressed 
them."

Clearly then if you have a DualShock2, the protocol must be much more 
complex than just which buttons are pressed.

The DualShock and DualShock2 use Sony Playstation Controller Port 
interfaces, which are a proprietary interface.
http://www.hardwarebook.info/Sony_Playstation_Controller_Port
A quick examination shows that that interface is not a USB interface and 
is not anything like an RS232 or PS/2 interface. It isn't even an 
asynchronous interface.

So... unless you are planning to use something *quite* different than 
the picture you showed, you are going to have difficult interfacing 
troubles that are going to slow the project substantially, and which 
have nothing to do with the limitations of USB to serial converters.


0
Walter
12/8/2010 2:53:40 AM
On 07/12/10 8:19 PM, engin caglar wrote:

> Finally,is there a way of talking with MATLAB and saying "Do whatever
> you should,and whenever you are free read data from xxx.txt" ?If so,can
> you give a small code example for such a purpose,maybe in a loop?

Not in any easy way that I know of. I am not sure that it is definitely 
impossible, but it might involve mechanisms such as creating another 
Java thread.

If you have the Instrument Control Toolbox, then reading from *devices* 
is easy in a way that would suit your needs (but not what you asked for 
:) ) You create the device object and you set a buffer size on it and 
you set it up for asynchronous reading (the default). Whenever data is 
ready, the device will interrupt Matlab and write the data in to the 
buffer. You can control what happens when there is device in the buffer: 
you can check from time to time how much data is available; you can read 
a specific number of bytes of data (which will read from the buffer if 
enough is available and will wait for more data if needed); you can set 
things up so that when a certain amount of data is available, a callback 
will be invoked.

A lot of the issues I was warning about with respect to serial devices 
are not important to you with your 1/2 second latency allowance. On the 
other hand, you *do* have problems if the device does not follow a pure 
async serial format.
0
Walter
12/8/2010 5:30:13 AM
"engin caglar" wrote in message <idmpvc$gpu$1@fred.mathworks.com>...
> By the way,let me add 2-3 things to my last post,hoping that you will answer them together.(Since noone else seems interested in the problem)
> 
> Firstly,Im planning to use that joystick for the control of 3D motion mechanism(not for a game or graphics).
> Second,as you mentioned in your last answer,I dont need exact time of joystick pushes.I mean no need "real time" data acquisition from usb.For example ,up to 0.5 second latency between the push and actual mechanism motion is acceptable for me.
> Finally,is there a way of talking with MATLAB and saying "Do whatever you should,and whenever you are free read data from xxx.txt" ?If so,can you give a small code example for such a purpose,maybe in a loop?
> 
> Thanks in advance...

Let me add two things:

For reading data from a USB device in MATLAB, you can often get pretty far using the "virtual COM port" (or "VCP") if the USB driver supports this feature. If the USB driver supports it, then you can use the MATLAB "serial" command to work with the USB device. For example,

myjoystick = serial('COM4'); % or whatever COM port
fprintf(myjoystick,'command'); % send data to USB device
fscanf(myjoystick); % read data. Also fread(myjosytick) for binary data

The MATLAB help file for serial.m has more information. You will have to know the correct communication settings (baud rate, etc). This does not require the Instr. Control Toolbox. You can tell if the USB driver supports this feature if you see a new COM port in the Windows device manager (in Windows). In OSX/Unix, you will see a new tty device in /dev; use this file name for the serial port name. Also, serial.m is only supported in OSX since version 2009a (I think).

For reading data from a file in a loop, if you include a "pause" or "drawnow" statement in the loop, it will allow other things to happen before restarting the loop.

good luck!
0
M
1/19/2011 11:48:05 PM
Reply:

Similar Artilces:

Connecting two HP50g by usb-usb or usb-serial or serial-serial
Hi I'am Manel, I wonder if connecting 2 hp50g's by USB-->SERIAL; SERIAL-->SERIAL is Posible. I have a adapter USB to SERIAL(HP) Thank you On 06/02/2010 18:13, MANELVC VC wrote: > Hi I'am Manel, > > I wonder if connecting 2 hp50g's by USB-->SERIAL; SERIAL-->SERIAL is > Posible. > > I have a adapter USB to SERIAL(HP) > > Thank you Serial to serial is possible and there is a special cable for this. Not sure how to order it from HP though. USB to USB is not possible - this is a limitation of the USB standard. -- Bruce Horrocks Surrey Eng...

More Problems with Serial Port on Mac OS X Intel and USB Serial port Keyspan
I just received a keyspan USB serial convertor and I can now communicate with numerous devices use the terminal. However the TCL/TK applications still can not communicate with the devices. Is there some special command for communicating with a serial device properly or is just broke in TCL/TK Aqua on the mac? I used the test script from the tcl.tk web site with these results: Error in startup script: bad option "-mode": should be one of -blocking, -buffering, -buffersize, -encoding, -eofchar, or -translation while executing "fconfigure $serial -mode "57600,n,8...

Can use MATLAB with RS232 Serial and USB Port?
Dear all, Any one use MATLAB to read / write data through RS232 Serial Port or USB port? Can share your MATLAB source code with me? Thanks a lot! Warm regards. Kam Kam, Check this discution, where you can find an example for serial communication. <http://newsreader.mathworks.com/WebX/.ef4d969?50@@> Pedro On Feb 23, 3:53 am, "pedro villena" <pedro.vill...@ieee.org> wrote: > Kam, > > Check this discution, where you can find an example forserial > communication. > > <http://newsreader.mathworks.com/WebX/.ef4d969?50@@> > > Pedro Check ...

USB port adapter -> Multi USB ports existing ? Extending number of USB ports possible ?
Unfortunately my notebook has only ONE USB port. Is there something like an USB port adapter which I can plug in into the one available USB port and which offers at the other side multiple USB ports ? Something like a simple, portable USB hub ? Tom [ excessive crossposting trimmed ] ["Followup-To:" header set to comp.os.linux.hardware.] On Sun, 14 Dec 2003 11:17:11 +0100, Thomas Jerkins staggered into the Black Sun and said: > Unfortunately my notebook has only ONE USB port. Is there something > like an USB port adapter which I can plug in into the one available > USB ...

need to make my usb port emulate a serial port
The problem i'm having is i have a program called lemmiwinks whic connects to my car for tuning. The program only recognizes seria com ports, and the cable that plugs into my car terminates in a usb. I have heard about emulators that can make my computer regonize a us port as a serial. Having trouble finding anything though. Any hel would be much appreciate audia4tuner wrote: > > The problem i'm having is i have a program called lemmiwinks which > connects to my car for tuning. The program only recognizes serial > com ports, and the cable that plugs into my car termi...

1 Port PCI Serial Card with MS Serial.sys
Hi, I got 1 port PCI serial Card installed on my machine, which UART is 16550 compatible. Is that possible to load that PCI card directly by using Microsoft Serial.sys driver using MSPorts.inf? Or do I need to write PCI bus driver for that card? Thank you very much, Bandu Bandu wrote: > Hi, > I got 1 port PCI serial Card installed on my machine, which UART is > 16550 compatible. Is that possible to load that PCI card directly by > using Microsoft Serial.sys driver using MSPorts.inf? > > Or do I need to write PCI bus driver for that card? > > Than...

USB to serial converter working ? and USB hubs attached to usb-serial converter
Hello, I have seen some references in the newgroups where people have shelled out some money for USB to rs-232 adapters/converters in order for their serial sensors (gps, other...) to be connected to the newly set of laptops that have no longer serial nor parallel outputs. Can any of you share his/her experience with a particularly bad piece of hardware and the type of the sensors it did not work with ? My own interest is to connect not one sensor, but two using the serial output (i.e. I have two sensors needing rs-232 output) I also have only 1 usb connection on that lapttop an...

serial port in matlab
does the serial port in matlab only support the rs232 ? ...

Serial Port and MatLab
Hi, i have a very simple problem, i think, but i cant resolve him. I have an atmega8 sending an array of informations every 5 seconds. What i pretend is, with a Gui read this array, and writing it in a edit box. What i have? I have already a gui with 2 buttons, one for open serial port and another so close. What i pretend, every 5 seconds or when bytes available in serial port, read them and send them to the edit box. Hellp me. lol Best regards Jorge Luis On Dec 25, 12:51=A0am, "Jorge Luis" <jorge...@hotmail.com> wrote: > Hi, i have a very simple problem, i think, but...

USB Serial Port
Hopefully this will save someone some grief. After much messing around with junk that did not work, I finally found a USB serial port that works properly and is fully-functional. The break function even works for password recovery on Cisco routers. Manufacturer: BAFO Technologies Model: BF-810 That is the 9 pin version. There is a 25 pin version which I assume works as well. --- Mooron Thank you. I love these tidbits of info. I hate messing with stuff when you someone else has already gone through the pain. <mooron@mooron.com> wrote in message news:1136327147.971109.243950@z...

USB-Serial Port
Hi, everyone I belive this group is the correct one to post my problem with usb-serial port. I am currently developed application for writing and reading from usb-serial port. That is connected to handphone. My problem is any time after writing, in this case AT command, to serial port device, and wait for the reply from the device(handphone), I am always got unstructure reply, many time lost the first character. This behavior happened if the setting of SerialPort timeout is: READINTERVALTIMEOUT = MAXDWORD READTOTALTIMEOUTMULTIPLIER = MAXDWORD READTOTALTIMEOUTCONSTANT = 120...

usb to serial port
i've got matlab on a computer without any available serial ports (there is one, but it is connected to something else). I'd like to get a usb-to-serial port adapter, but i don't know if matlab will be able to control it the same way it does a normal serial port. anyone know the answer? Some years ago I did exactly what you describe, and it wasn't a problem. As far as I remember, the driver for the USB->serial gadget just made it look as if there was a normal serial port, and I opened it from Matlab and communicated with it as for a standard hardware port. "David...

Can i communicate by the USB port, if my application is set up to communicate with the serial RS232 port???
Hi to all labview users... Please help me with my deadly problem. Last Deadline is on monday next week!. &nbsp; I got a complete program now, written and tested OK. It communicates with a slave to control a motor. I've set up the program to communicate with the slave by the VISA RS232 Serial&nbsp;Port commuication.&nbsp; and all works fine... Now the problem is.. that The communication should have been by the USB port instead. So what I have done now, is that i used a USB to RS232 converter.&nbsp; So that the USB Plug is plugged to the PC, and the RS232 end to my&nbsp;S...

Matlab Serial Port
I'm using a serial port interface with Matlab. To creat a serial port object in Matlab, the following command is used, for example: s = serial('COM1'); This command works in the command line, however when I try to use this command in a script file it gives me the following error: ??? Error using ==> serial.serial The PORT must be specified. Any ideas? Thanks in advance. That is what I am using in my m-file. Are you sure it is this line that is giving you that error? s = serial('COM1') -or- s = serial('com1') both work. T Conrad Donovan wrote: > > > I...

Web resources about - Serial port and USB - comp.soft-sys.matlab

Serial - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Serial Innovation
This is a very interesting site done by Brendan Steidle. He doesn’t just point out problems with a company or industries, he tries to offer solutions ...

Serial child molester walks free in Qld
A Brisbane serial child sex offender has walked free despite admitting to having molested a young boy seven years ago.

Baton Rouge serial killers murder almost 70 victims
... south is stalked by death. Over the past two decades, the citizens of Baton Rouge and its outskirts have been hunted by no less than five serial ...


Serial to Release Episodes Every Other Week
Serial has scaled back its publishing frequency to every other week. The New York Times reports that the fifth episode of Serial’s second season—which ...

Alleged Murder In Scarsdale! I See A Prestige Murder Porn Serial In Our Future!
Scarsdale, a town in Westchester County that looks down its nose at my town in Westchester County, has arrested a man for murder. This is significant ...

‘Arrested Development’ Season 5 will parody ‘Making a Murderer’ and ‘Serial’
Rumors surrounding a fifth season of Arrested Development have been swirling around ever since Netflix did the world a favor and rebooted the ...

Do the Taliban listen to "Serial" and do they like it? Yes!
Stephen Colbert interviews Sarah Koenig about the second season of "Serial," which examines the case of Bowe Bergdahl.

Conan announce North American tour with Serial Hawk
... UK doom metal band Conan went on their first ever U.S. tour in 2015, and they'll be back in 2016 for a run of dates with Seattle doomers Serial ...

Resources last updated: 1/28/2016 4:33:26 PM