If you have a problem or need to report a bug please email : support@dsprobotics.com
There are 3 sections to this support area:
DOWNLOADS: access to product manuals, support files and drivers
HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects
USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here
NEW REGISTRATIONS - please contact us if you wish to register on the forum
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
Ruby Decode Bytes from COM Port Module
19 posts
• Page 1 of 2 • 1, 2
Ruby Decode Bytes from COM Port Module
Hi,
I am using the Com Port Module to read in byte (hex) data from a digitizing tablet (and 16 button cursor). been banging my head for too long on this and need a bit of Ruby help please.
I can read in the data but I know I am not getting the next values correctly. If I was using BASIC or C++ I would do this differently, but need to learn Ruby since it is in Flowstone my favorite programming system ever !
Here is my code, The digitizer tablet data comes in as 6 bytes of hex data, one of which contains a 1 in the MSB position to sync off of... Normally I would keep reading the input value until I get a "sync" then grab the next 5 bytes (and of coarse the first byte) for a total of all 6 bytes. But I cannot seem to get this working without the ability to jump back to a "label" (like in BASIC or C) and repeat the test to see if I have "sync".
The next issue is getting the next bytes in order... storing them in an array, then decoding them (still to be done), then repeating this process to keep streaming the data in, in order to be able to track the digitizer cursor and then when a user presses one of 16 cursor keys, that data coordinate pair is saved... ruby in Flowstone seems to be a little different than some of the online tutorials especially with inputting and outputting data, at least for me...
Here is a snapshot of the code but it is not syncing or putting the values into the array properly ??? All of the output values are the same, and thus the event is not getting updated with the most current data value.
Thanks for any Ruby help please
Aron
I am using the Com Port Module to read in byte (hex) data from a digitizing tablet (and 16 button cursor). been banging my head for too long on this and need a bit of Ruby help please.
I can read in the data but I know I am not getting the next values correctly. If I was using BASIC or C++ I would do this differently, but need to learn Ruby since it is in Flowstone my favorite programming system ever !
Here is my code, The digitizer tablet data comes in as 6 bytes of hex data, one of which contains a 1 in the MSB position to sync off of... Normally I would keep reading the input value until I get a "sync" then grab the next 5 bytes (and of coarse the first byte) for a total of all 6 bytes. But I cannot seem to get this working without the ability to jump back to a "label" (like in BASIC or C) and repeat the test to see if I have "sync".
The next issue is getting the next bytes in order... storing them in an array, then decoding them (still to be done), then repeating this process to keep streaming the data in, in order to be able to track the digitizer cursor and then when a user presses one of 16 cursor keys, that data coordinate pair is saved... ruby in Flowstone seems to be a little different than some of the online tutorials especially with inputting and outputting data, at least for me...
Here is a snapshot of the code but it is not syncing or putting the values into the array properly ??? All of the output values are the same, and thus the event is not getting updated with the most current data value.
Thanks for any Ruby help please
Aron
-
aronb - Posts: 154
- Joined: Sun Apr 17, 2011 3:08 am
- Location: Florida, USA
Re: Ruby Decode Bytes from COM Port Module
I couldn't test my changes, but you can
The logic should work, but there might be typos.
The logic should work, but there might be typos.
- Attachments
-
- CalComp_02 [tula].fsm
- (56.43 KiB) Downloaded 806 times
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: Ruby Decode Bytes from COM Port Module
tulamide,
Moved and added a few things around to get rid of a method error (added @'s) but output still populates the same number value in all instances, like the triggering isn't happening. Its like it reads the same number 6 times... ?
I'm still at it - I feel dumb
Thanks for your help !
Aron
Moved and added a few things around to get rid of a method error (added @'s) but output still populates the same number value in all instances, like the triggering isn't happening. Its like it reads the same number 6 times... ?
I'm still at it - I feel dumb
Thanks for your help !
Aron
-
aronb - Posts: 154
- Joined: Sun Apr 17, 2011 3:08 am
- Location: Florida, USA
Re: Ruby Decode Bytes from COM Port Module
Hey aronb,
the @s are very important. They make the variables instance variables. Without it they are local variables, which means they will be initialized each time the event method is called - which is not what you want. So make sure that each and every variable that is not just used once, but should keep its value, is an instance variable!
EDIT: I missed another one. You bitwise and a number with a string (a is a string). Exchange the event code with this one:
the @s are very important. They make the variables instance variables. Without it they are local variables, which means they will be initialized each time the event method is called - which is not what you want. So make sure that each and every variable that is not just used once, but should keep its value, is an instance variable!
EDIT: I missed another one. You bitwise and a number with a string (a is a string). Exchange the event code with this one:
- Code: Select all
def event (i,v)
#convert input value to binary format "0b00000000"
a = get_data()
# if MSB is 1 then sync
if (a.to_i & 0b10000000) then
@buffer = true
end
if @buffer == true
@calcomp << a
end
if @calcomp.length == 6
0..5.each do |n|
output n, @calcomp[n]
end
@buffer = false
@calcomp.clear
end
end
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: Ruby Decode Bytes from COM Port Module
also, take look here to use an better way to convert to binary, and other facility trough the pack/unpack methods.
http://blog.bigbinary.com/2011/07/20/ru ... npack.html
http://blog.bigbinary.com/2011/07/20/ru ... npack.html
- Tronic
- Posts: 539
- Joined: Wed Dec 21, 2011 12:59 pm
Re: Ruby Decode Bytes from COM Port Module
Thanks for the help, I really appreciate it !
But... It is still not working, so I made a very simple simulator so others can try this code in hopes of getting this to work... I've tried everything I can think of.
You can switch from off, manual stepping, or auto stepping through a few simulated (valid) values.
Please take a look and see what I am doing wrong, I am unclear if watching a number is the "string" value or the "number" value because of the last comment. I thought the conversion was done correctly, but it must not be since it still isn't functioning properly.
The watched value should only be returning 128 (or 0b10000000) since that is the sync value, the sync is "true", but the value is not incorrect.
Thanks again...
Aron
But... It is still not working, so I made a very simple simulator so others can try this code in hopes of getting this to work... I've tried everything I can think of.
You can switch from off, manual stepping, or auto stepping through a few simulated (valid) values.
Please take a look and see what I am doing wrong, I am unclear if watching a number is the "string" value or the "number" value because of the last comment. I thought the conversion was done correctly, but it must not be since it still isn't functioning properly.
The watched value should only be returning 128 (or 0b10000000) since that is the sync value, the sync is "true", but the value is not incorrect.
Thanks again...
Aron
-
aronb - Posts: 154
- Joined: Sun Apr 17, 2011 3:08 am
- Location: Florida, USA
Re: Ruby Decode Bytes from COM Port Module
Now with actual data incoming I saw immediatly that I made another mistake. If you bitwise and two numbers the result is a number and not true or false.
This should work now (it gets true, if the result of a & b is exactly 128, which is the case for all numbers whose 8th bit is set).
I'm sorry for all the confusion, I did a bad job at helping. And I hope this time it's right!
EDIT: You will always see the type that you are watching. If you watch a string, you'll see a string, if you watch a numeric you'll see a numeric (converted to a string, so that you can see it), if you watch a boolean, you see a boolean (converted to a string), and so on.
If you are unsure what you are seeing you can always call variable.class
This should work now (it gets true, if the result of a & b is exactly 128, which is the case for all numbers whose 8th bit is set).
I'm sorry for all the confusion, I did a bad job at helping. And I hope this time it's right!
EDIT: You will always see the type that you are watching. If you watch a string, you'll see a string, if you watch a numeric you'll see a numeric (converted to a string, so that you can see it), if you watch a boolean, you see a boolean (converted to a string), and so on.
If you are unsure what you are seeing you can always call variable.class
- Attachments
-
- CalComp_Data_Sim [tula].fsm
- (5.95 KiB) Downloaded 774 times
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: Ruby Decode Bytes from COM Port Module
sorry I do not have time to see the latest solutions, however post my version
- Attachments
-
- CalComp_Data_Sim(Tronic).fsm
- (6.1 KiB) Downloaded 759 times
- Tronic
- Posts: 539
- Joined: Wed Dec 21, 2011 12:59 pm
Re: Ruby Decode Bytes from COM Port Module
I just got back to this and am working on it now.
I will get back to the forum in an hour or so.
Thanks for all of the help - I may be learning some Flowstone Ruby finally !!!
Aron
I will get back to the forum in an hour or so.
Thanks for all of the help - I may be learning some Flowstone Ruby finally !!!
Aron
-
aronb - Posts: 154
- Joined: Sun Apr 17, 2011 3:08 am
- Location: Florida, USA
Re: Ruby Decode Bytes from COM Port Module
I am getting very close to finishing and now this error popped up and I cant figure out why
"wrong number of arguments (1 for 0)" maybe the input data is empty sometime???
As I understand it the error comes from passing something to a function when it wasn't expecting it - passed an argument to a function which doesn't take any...
The code is basically the same but I am adding masking to pull out the required data.
I added the buffer clear, and while it does keep the buffer from overflowing, the error still occurs. If the data stream gets out of sync, has more than the expected data it gets the error.
NOTE: You must toggle the Ruby Counter Mode in the simulator - it doesn't start counting correctly.
Thanks again...
Aron
"wrong number of arguments (1 for 0)" maybe the input data is empty sometime???
As I understand it the error comes from passing something to a function when it wasn't expecting it - passed an argument to a function which doesn't take any...
The code is basically the same but I am adding masking to pull out the required data.
I added the buffer clear, and while it does keep the buffer from overflowing, the error still occurs. If the data stream gets out of sync, has more than the expected data it gets the error.
NOTE: You must toggle the Ruby Counter Mode in the simulator - it doesn't start counting correctly.
Thanks again...
Aron
-
aronb - Posts: 154
- Joined: Sun Apr 17, 2011 3:08 am
- Location: Florida, USA
19 posts
• Page 1 of 2 • 1, 2
Who is online
Users browsing this forum: No registered users and 50 guests