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

catalinanesia
Posts: 83
Joined: Sun Feb 16, 2014 10:08 pm

Re: Help extracting information from USB port data

Post by catalinanesia » Wed Aug 19, 2015 11:37 pm

Hi everyone,
just to continue on the topic: I have an Xbee attached to the USB port seen as COM3 in Windows7 and another Xbee sending data to the Coordinator (at PC on the USB)
but my biggest problem is in LiveCode (Community 6.5) and whatever I read and tested does not work ... so here is what I have close to Richmond62 script.

Code: Select all

on mouseUp
   //global gSerialData
   //set the serialControlString to "BAUD=9600 DATA=8 STOP=1 PARITY=N"
   open driver "COM3:" for binary read
   read from driver "COM3:" until empty
   put it into field "fCOM3"
   //put it into gSerialData
   //put gSerialData into field "fCOM3"
end mouseUp
Thanks for all the advice's ...

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 Aug 20, 2015 2:58 pm

From http://livecodeitalia.blogspot.it/2014/ ... ta_95.html :
I would use:

Code: Select all

open driver "COM3:" for binary update
read from driver "COM3:"  for 1 line in 100 milliseconds
put it 
Adjust time and number of lines depending what you need.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

catalinanesia
Posts: 83
Joined: Sun Feb 16, 2014 10:08 pm

Re: Help extracting information from USB port data

Post by catalinanesia » Thu Aug 20, 2015 7:49 pm

Ciao MaxV,
Thanks for the replay and I am glad to see that your blog link works, fore some reason yesterday night I could not load that page (error 404 on your side)
One question: "put it" - will do what? I need to see the data coming in into a field ...
what is wrong with : put it into the field "xxx" ?

Graz. 1000 y buona notte!
Catalin

catalinanesia
Posts: 83
Joined: Sun Feb 16, 2014 10:08 pm

Re: Help extracting information from USB port data

Post by catalinanesia » Fri Aug 21, 2015 4:56 am

Well unfortunately it does not work.
I have this script as advised by MaxV but with no results ...

Code: Select all

on mouseUp
    open driver "COM3:" for binary update
    read from driver "COM3:" for 10 line in 100 milliseconds
    put it into field "fCOMdata"
end mouseUp


Is there anyone who can read the COM ports in Windows 7 with LC (I use v6.5) ?
Please help ...

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 » Fri Aug 21, 2015 12:29 pm

Try this one:

Code: Select all

on mouseUp
    open driver "COM3:" for binary update
    read from driver "COM3:" for 1 line in 100 milliseconds
    put it into temp
   set the text of field "fCOMdata" to temp
end mouseUp
Are you sure that some data is arriving to COM3?
What string do you expect?
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

catalinanesia
Posts: 83
Joined: Sun Feb 16, 2014 10:08 pm

Re: Help extracting information from USB port data

Post by catalinanesia » Fri Aug 21, 2015 9:05 pm

Dear MaxV,

I definitely have confection on the COM3 and see data input (I use some software I found to check that and it clearly shows me data)
What I expect is this:
7E, 0, 12, 92, 0, 13, A2, 0, 43, 78, FF, 79, 36, 6A, 1, 1, 0, 10, 0, 0, 10, C6
7E, 0, 12, 92, 0, 13, A2, 0, 43, 78, FF, 79, 36, 6A, 1, 1, 0, 10, 0, 0, 10, C6
7E, 0, 12, 92, 0, 13, A2, 0, 43, 78, FF, 79, 36, 6A, 1, 1, 0, 10, 0, 0, 10, C6
7E, 0, 12, 92, 0, 13, A2, 0, 43, 78, FF, 79, 36, 6A, 1, 1, 0, 10, 0, 0, 10, C6
7E, 0, 12, 92, 0, 13, A2, 0, 43, 78, FF, 79, 36, 6A, 1, 1, 0, 10, 0, 0, 0, D6
7E, 0, 12, 92, 0, 13, A2, 0, 43, 78, FF, 79, 36, 6A, 1, 1, 0, 10, 0, 0, 0, D6
7E, 0, 12, 92, 0, 13, A2, 0, 43, 78, FF, 79, 36, 6A, 1, 1, 0, 10, 0, 0, 0, D6
7E, 0, 12, 92, 0, 13, A2, 0, 43, 78, FF, 79, 36, 6A, 1, 1, 0, 10, 0, 0, 10, C6
7E, 0, 12, 92, 0, 13, A2, 0, 43, 78, FF, 79, 36, 6A, 1, 1, 0, 10, 0, 0, 10, C6
7E, 0, 12, 92, 0, 13, A2, 0, 43, 78, FF, 79, 36, 6A, 1, 1, 0, 10, 0, 0, 10, C6
7E, 0, 12, 92, 0, 13, A2, 0, 43, 78, FF, 79, 36, 6A, 1, 1, 0, 10, 0, 0, 10, C6

and unfortunately for me your latest code post does not work neither :(

Thanks!

catalinanesia
Posts: 83
Joined: Sun Feb 16, 2014 10:08 pm

Re: Help extracting information from USB port data

Post by catalinanesia » Tue Aug 25, 2015 5:31 am

Hi everyone,
Thanks to the stack I found here (see below link) now I am able to read the COM port with LC 7.06 in Win7. - Thanks HUTCH for the published stack!
http://quality.runrev.com/show_bug.cgi?id=12545
Now my problem is the type of data I get in my field on LC that looks like this: (ASCII)
~..ƒ.......>
~..ƒ.......O
~..ƒ.......N
~..ƒ.......N
~..ƒ.......O
~..ƒ.......>
~..ƒ.......>
~..ƒ.......>
(..... represents SPACE's)

and I am expecting to see something like this: (HEX, the bold 10 represents a Xbee button press that I need to catch in LC so I can trigger a Handler accordingly)
7e 00 0a 83 00 00 1d 00 01 00 10 00 10 3e
7e 00 0a 83 00 00 1c 00 01 00 10 00 00 4f
7e 00 0a 83 00 00 1d 00 01 00 10 00 00 4e
7e 00 0a 83 00 00 1d 00 01 00 10 00 00 4e
7e 00 0a 83 00 00 1c 00 01 00 10 00 00 4f
7e 00 0a 83 00 00 1d 00 01 00 10 00 10 3e
7e 00 0a 83 00 00 1d 00 01 00 10 00 10 3e
7e 00 0a 83 00 00 1d 00 01 00 10 00 10 3e

How can I convert or get the data from the COM in the format I need ?
Below is the link to the Xbee project I try to recreate but instead of using Arduino to read the action I want to use LC.
https://youtu.be/jh-GIaghIjw

Attached is my current LC stack. (put 1 in the field Repeat and press Repeat button after the Connect button is pressed; to read the COM port every second)
Any help is welcomed!
Attachments
Xbee USB v1.rar
(1.19 KiB) Downloaded 273 times

catalinanesia
Posts: 83
Joined: Sun Feb 16, 2014 10:08 pm

Re: Help extracting information from USB port data

Post by catalinanesia » Thu Aug 27, 2015 3:10 pm

No one ...
No solution to read in HEX from the USB COM port? instead of ASCII ...
No other approach ? do I hit a dead end ?

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 Aug 27, 2015 4:43 pm

Livecode read numbers correctly, but you are trying to see them as text, so it's normal.
What you think is a space is a not-printable char of the ASCII table.
You have to convert it with chartonum() function, char by char.
With chartonum you'll get the number in base 10, but do you want them in base 16?
If so you have to use baseconvert() function.
For exampe:

Code: Select all

baseconvert(charTonum(">"),10,16) = "3E"
P.S. every char in a PC is represented by a number of the ASCII table, ASCII table contains also many not-printable characters like "return line".
For example:
1010100 bit sequence is 84 (base 10) or 54 (base 16) or the letter "T"? You have to decide how the PC has to interpreter the bit sequence
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

catalinanesia
Posts: 83
Joined: Sun Feb 16, 2014 10:08 pm

Re: Help extracting information from USB port data

Post by catalinanesia » Thu Aug 27, 2015 5:44 pm

Thanks! MaxV
I have been thinking that it is a conversion trick and I am glad you came app with an approach that I can test and move on.
Why do I have this error on the code? it should work like a charm ...

Code: Select all

local lChar1
on mouseUp
   put char 1 of field "fCOMdata" into lChar1
   baseConvert(charToNum(lChar1),10,16)
   put it into field "fConvert"
end mouseUp
Error is:
button "Convert HEX": execution error at line 4 (Handler: can't find handler) near "baseConvert", char 1

Any idea ...

catalinanesia
Posts: 83
Joined: Sun Feb 16, 2014 10:08 pm

Re: Help extracting information from USB port data

Post by catalinanesia » Thu Aug 27, 2015 6:01 pm

Right, I need to think before jumping into conclusions ...
here is the working one:

Code: Select all

local lChar1
on mouseUp
   put char 1 of field "fCOMdata" into lChar1
   put baseConvert(charToNum(lChar1),10,16) into field "fConvert"
end mouseUp

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Help extracting information from USB port data

Post by bn » Thu Aug 27, 2015 6:01 pm

Hi catalina,

you forgot "get"

try this

Code: Select all

local lChar1
on mouseUp
   put char 1 of field "fCOMdata" into lChar1
   get baseConvert(charToNum(lChar1),10,16)
   put it into field "fConvert"
end mouseUp
or even shorter:

Code: Select all

local lChar1
on mouseUp
   put char 1 of field "fCOMdata" into lChar1
   put baseConvert(charToNum(lChar1),10,16) into field "fConvert"
end mouseUp
Please note that charToNum and numToChar are not recommended in LC 7, use byteToNum and numToByte instead for binary data. See dictionary.

Kind regards
Bernd

Post Reply

Return to “Talking LiveCode”