Help extracting information from USB port data

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9387
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Help extracting information from USB port data

Post by richmond62 » Mon Jan 12, 2015 11:23 pm

I wonder if it would be possible for Livecode to capture raw data from a USB input device (such as
a keyboard: i.e. intercepting that data before the operating system had played about with it.

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Help extracting information from USB port data

Post by MaxV » Tue Jan 27, 2015 3:52 pm

I don't know if this link is what you need: http://livecodeitalia.blogspot.it/2014/ ... porta.html
Best regards
Max
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9387
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Help extracting information from USB port data

Post by richmond62 » Tue Jan 27, 2015 9:32 pm

Thanks MaxV: certainly worth a look :)

Mind you, I have no Italian, so things will take a bit of decoding.

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Help extracting information from USB port data

Post by MaxV » Wed Jan 28, 2015 12:47 pm

richmond62 wrote:Thanks MaxV: certainly worth a look :)

Mind you, I have no Italian, so things will take a bit of decoding.
I suggest you to use the google translator button, it's in the page. :wink:
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9387
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Help extracting information from USB port data

Post by richmond62 » Wed Jan 28, 2015 6:33 pm

Google translate as Google translate, but does it TRANSLATE?

Here are some of Google's attempts:

Google translate traducir como Google, pero ¿traducir?

Google translate come Google Translate, ma vuol tradurre?

Google превежда като Google превежда, но не го преведе?

And . . . back again:

from "Spanish" : Google translate translated as Google, but translate?

from "Italian" : Google translate translated as Google, but translate?

from "Bulgarian" : Google translate Google translate as, but not translate it?

QUOD ERAT DEMONSTRANDUM

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Help extracting information from USB port data

Post by MaxV » Thu Jan 29, 2015 11:49 am

Hi richmond62,
on the right column, just before the stats, there is a Google Translate tool, if you choose "English" this is the output (I use class=notranslate for code, so code is unchanged):
Communicate with a port
Perhaps the title may seem dubious, but in this post I will try to expose an overview on how to read the data coming from a USB, serial, or even by an Arduino board.
Before you start it is better to understand what good is all this ?
Imagine having to deal with something that is beyond just reading and writing files, imagine having to know the value of a sensor: the need to know how much has been rotated the wheel of a car, the temperature with a thermometer, the intensity light, etc. In this context we must read an input that we can get to the computer in various ways, the easiest way was analyzed and digested and comes to us as a string of information on the USB port.
By connecting the USB port to the system that reads the data, as such as Arduino, our computer does not see almost nothing, there is a file to be read; there is a communication port that shoots continuously and data that can also accept data
will not go into programming Arduino, which is beyond the scope of this post, there are also other sensors with USB port in addition to Arduino; I will mention to indicate http://bitlash.net/ as one of the easiest to program it.
The command to communicate with a door is open driver device , depending on the operating system the string device is different. On Windows systems all ports are COM, then you can write:

Code: Select all

open driver "COM2:" for binary update
while on Linux ports are tty (or something similar) into the folder / dev , then:

Code: Select all

open driver "/ dev / tty2" for binary update
Mac is a bit more complicated, because the name and position changes with each version, however, for the rest it is like on Linux:

Code: Select all

# Try one of these: /dev/cu.usbmodem2, /dev.tty.usbmodem2, /dev.cu.usbserial2,
open driver "/dev/cu.usbmodem2" for binary update

mode "for binary" (dialogue in binary) is the most used, but you can open it in plain text.
Now comes the tricky part, reading and writing. And 'complicated because you have to understand when they begin and end the data.
If your signal is single channel (then receive information from a single sensor), usually the end character information is a carriage return. In this case, just: (by replacing usbserial with your device )

Code: Select all

read from driver usbserial for 1 line in 100 milliseconds
put it into theInput
As you see, you must specify a maximum time in which the program starts to listen, otherwise if there is a transmission error the program stops because it does not get why the end of the data (carriage return).
This command can repeat it every 30 milliseconds to update the value read.
If you want to send a command to your sensor / transducer, such as "I am ready to receive data", just use write , here's the code:

Code: Select all

write "run lsd, 500" & return to driver usbserial
If you read from several sensors, the USB port will multichannel information, like: a1 = 22 34 a2 = a3 = 18 ... The roads here are many and depends largely on the update time you need. Take all the rows together and then analyze them is the worst, you risk losing a lot of time to figure out if you have received all the data. Usually the best strategy is to read one line at a time, analyze it and read the next one. The same goes for writing. Another typical mistake is to want to immediately want to communicate at very high speeds, experiment and see if you really need it . Usually the base speed of the door (9600) is more than sufficient for almost all applications. Increase speed increases synchronization problems between reading and writing, the time decreases in provisions and you can not figure out if you have received all the data correctly.
Let me know if you encounter other problems with translation. :D
P.S.Did you study Latin? I did, in Italy it is mandatory in high school... :( hoc tempus porto cicatrices
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Help extracting information from USB port data

Post by MaxV » Thu Jan 29, 2015 11:54 am

Hi richmond62,
thank for your suggestion, I moved the translate widget in top right position, so it's easier to see it and use it. :D
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9387
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Help extracting information from USB port data

Post by richmond62 » Thu Jan 29, 2015 8:50 pm

Thanks MaxV: BUT, I didn't have any real problem spotting the Google Translate button . .

The only problem is, that in my experience, Google does NOT translate because a computer cannot do what a human
brain can do, nor will it ever.

I do a lot of work, along with my wife, doing very high-powered translations between Bulgarian and English and know what
does constitute translation and what doesn't. What all machine 'translation' does is most definitely NOT translation, it produces
some sort of pale simulcrum of the original version that often as not loses half of its meaning in the exercise.

GSA_DC
Posts: 35
Joined: Thu Nov 27, 2014 2:07 pm

Re: Help extracting information from USB port data

Post by GSA_DC » Tue Mar 24, 2015 3:49 pm

a computer cannot do what a human brain can do, nor will it ever.
This is worthy of a new thread in the 'Consciousness, AI and Machine Learning with Livecode' board.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9387
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Help extracting information from USB port data

Post by richmond62 » Wed Mar 25, 2015 7:57 pm

De Bello Gallico is not great help with Italian articles about Livecode :)

Mind you, Plautus's plays, while being totally irrelevant to Livecode are hilarious.
Last edited by richmond62 on Wed Mar 25, 2015 8:02 pm, edited 1 time in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9387
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Help extracting information from USB port data

Post by richmond62 » Wed Mar 25, 2015 8:01 pm

"open driver "COM2:" for binary update"

OK: that supposes that the USB device is always going to be in position 2 in the COM queue.

It seems that one of the largest problems is working out which of the COM connexions is the USB device one wants to communicate with.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9387
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Help extracting information from USB port data

Post by richmond62 » Thu Apr 02, 2015 9:32 pm

Capture8.PNG
I'm doing something wrong, and would be grateful for some help.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Help extracting information from USB port data

Post by Simon » Thu Apr 02, 2015 10:30 pm

Hi Richmond,

Code: Select all

read from file "COM3:" until end
but I think "end" may actually be either CRLF or LF or numToChar(13)

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9387
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Help extracting information from USB port data

Post by richmond62 » Fri Apr 03, 2015 10:38 am

Thanks, Simon: obviously there are some errors in the Documentation . . . LOL

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Help extracting information from USB port data

Post by SparkOut » Fri Apr 03, 2015 11:51 am

I haven't checked the docs (on a mobile phone here) but EOF might be relevant?

Post Reply

Return to “Talking LiveCode”