Basic Help, Please.
I’m having some problems with a subroutine in the small program I’ve been working on mikroBasic for the Honda Music Link stuffs I’ve been doing.
Could any of you help me?
To start, here’s a copy of the program in a text file: hml_mikrobasic_help.txt
Here’s the project itself, zipped up: hml_mikrobasic_help.zip
The IDE / compiler itself, mikroBasic, is available here.
(I’m just using the demo version, and the limit is only on code size, so feel free to install it. I don’t come anywhere near the code limit, and when one does it’s rather obvious, as the compiler just refuses to compile it.
The problem I’m having is with sendCommand(), and specifically the variable iPodCommand, which is the first one passed to it. What I expect to happen is that whatever is passed as the first argument to sendCommand() will be set and available within the procedure. Instead, I seem to be getting something else.
As the program is currently listed, I would expect the output sendCommand(powerOn, 0) to be 0xFF 0x55 0x04 0x00 0x00 0x00 0x04 0xF8. However, the output I receive is 0xFF 0x55 0x02 0xFE 0xB9 0x47.
The expected packet is broken down as follows:
0xFF – Header 1
0x55 – Header 2
0x04 – Data Length
0x00 – Data 1
0x00 – Data 2
0x00 – Data 3
0x04 – Data 4
0x08 – Checksum (0x100 – Data Length – All Data Packets)
The received packet breaks down this way:
0xFF – Expected Header 1
0x55 – Expected Header 2
0x02 – Length appropriate if the constant referenced by iPodCommand were two bites in length.
0xFE – Data 1 (Not sure where this comes from.)
0xB9 – Data 2 (This one neither…)
0x47 – Checksum, calculated properly off of unexpected Data bytes.
When I run things through the hardware debugger I’m not seeing the variable iPodCommand as an array, and I’m not sure why or how (it just shows a value of 0xDF). That, though, and it’s not working as expected, is leading me to believe that I’m doing something wrong in setting up sendCommand().
So, could any of you please help me? I just don’t know what I’m doing wrong.
Thanks!
My immediate guess would be that byte[n] and string[10] aren’t compatible types. I’m not sure about this specific dialect, but I’d imagine string[10] to be an array of 10 variable length strings, rather than a string 10 characters long.
From the docs:
So I don’t think it’s that. I think the problem is more… I’m not sure. :\
From the docs:
So I don’t think it’s that. I think the problem is more… I’m not sure. :\
My immediate guess would be that byte[n] and string[10] aren’t compatible types. I’m not sure about this specific dialect, but I’d imagine string[10] to be an array of 10 variable length strings, rather than a string 10 characters long.
based on how i understand your problem and the symptoms, it sounds like how you are passing your command string is not right.
perhaps you are sending the data as the address instead of by reference. i would explain the wrong data and unexpected checksum.
another thing you could consider, precalc the checksum so you don’t have to spend time doing this each time.
I’m thinking the same thing, but I can’t figure out how to fix it. :\
I’m thinking the same thing, but I can’t figure out how to fix it. :\
Oh, and I could do the checksums ahead of time… I’m just not sure if it’s really needed. The absolute fastest I’ll be sending commands is about two per second. Also, once i add in the mode 4 commands and responses there will be a lot more to do. Although… It wouldn’t be difficult to switch to precalculated ones once I know exactly what I need to send in each direction. Hmm.
Oh, and I could do the checksums ahead of time… I’m just not sure if it’s really needed. The absolute fastest I’ll be sending commands is about two per second. Also, once i add in the mode 4 commands and responses there will be a lot more to do. Although… It wouldn’t be difficult to switch to precalculated ones once I know exactly what I need to send in each direction. Hmm.
based on how i understand your problem and the symptoms, it sounds like how you are passing your command string is not right.
perhaps you are sending the data as the address instead of by reference. i would explain the wrong data and unexpected checksum.
another thing you could consider, precalc the checksum so you don’t have to spend time doing this each time.
It seems that the first parameter is passed in as a reference. Try sending it as a normally a copy instead of by reference.
Since it’s an array it has to be sent as a reference. :\
There are ways around this, you can pass each element seperately and recreate the array (lame, but it works), or wrap the array in a struct, which can be passed by value.
Oh, wait, I take that back, I really know nothing about ASM programming… I assumed you were using a C compiler… carry on.
I’m not using either — it’s Basic, specifically mikroBasic.
Oh, gotcha, the only file format I recognized in the zip was .asm
Passing each element should apply, then, and I believe most basic variants have some equivalent of a struct, though it’s not always called that or done exactly like C.
Hmm… I hope I’m not being too ignorant or demanding here, but can you point me to an example of that?
Page 73 of this mikrobasic manual has the info you need.
Make an array inside your structure and pass the structure to the function, access the array by using structureName.arrayName, should work, I think.
<sigh> Thank you. :)
Now I feel dumb. For some reason I thought all the docs were in the help file…
Hell if I knew any of that before this came up, thank google, not me ;0)
you are such a good little boy.
you are such a good little boy.
Gur. I’m still not getting this, and I can’t quite find an example of it. heh.
Looking at the code (hard to read, I’m spoiled with color-coding)… are you using some kind of promotion to cast byte to “string”? You’re passing in an array of bytes (powerOn, a byte[4]) and receiving an array of strings (there is a native string type in basic? I’m not really used to using basic, but I see iPodCommand as string[10])…
That’s one possible garbling point…
Yes, I think that’s where the problem is. I thought it’d work correctly as I wrote it, but I now see that it doesn’t.
What I need to understand is how to reference the actual const (as a string of bytes) when the procedure is called with the name of the const.
Have you tried just matching your data types to see what happens, ie changing it to sub procedure sendCommand(dim byref iPodCommand as byte[10], dim destUart as byte)?
Or am I missing something crucial here? Also, you could use the var stored in [2] of your array to know the length of your byte array at any time, if you were using the null-termination of strings for that before…
I’m going to be leaving for school in a bit, so I’ll be missing for many hours. Good luck and happy futzing!
Yep, I’d tried that first change mentioned. Makes no difference. :(
Yep, I’d tried that first change mentioned. Makes no difference. :(
Have you tried just matching your data types to see what happens, ie changing it to sub procedure sendCommand(dim byref iPodCommand as byte[10], dim destUart as byte)?
Or am I missing something crucial here? Also, you could use the var stored in [2] of your array to know the length of your byte array at any time, if you were using the null-termination of strings for that before…
I’m going to be leaving for school in a bit, so I’ll be missing for many hours. Good luck and happy futzing!
Yes, I think that’s where the problem is. I thought it’d work correctly as I wrote it, but I now see that it doesn’t.
What I need to understand is how to reference the actual const (as a string of bytes) when the procedure is called with the name of the const.
Looking at the code (hard to read, I’m spoiled with color-coding)… are you using some kind of promotion to cast byte to “string”? You’re passing in an array of bytes (powerOn, a byte[4]) and receiving an array of strings (there is a native string type in basic? I’m not really used to using basic, but I see iPodCommand as string[10])…
That’s one possible garbling point…
of course, I started looking at the code and forgot why, I was going to write an example of how to use a structure, duh. Hold on…
of course, I started looking at the code and forgot why, I was going to write an example of how to use a structure, duh. Hold on…
I’ve never used basic much, so going with your style as an example…
‘ define your user data type structure
structure fourByteCommand
dim instruction as byte[4]
end structure
‘ create an instance variable of that structure
dim powerOn as fourByteCommand
powerOn.instruction[0]=0x00
powerOn.instruction[1]=0x00
powerOn.instruction[2]=0x00
powerOn.instruction[3]=0x80
‘ down in the subroutine pass it in
sub procedure sendCommand(dim iPodCommand as fourByteCommand, dim destUart as byte)
‘ etc, and down while you’re sending just use iPodCommand.instruction as the array you’re iterating through.
Well, crap, that doesn’t let you have variable length commands, but I think you get the idea, you can work from there.
Re: I’ve never used basic much, so going with your style as an example…
Stuff like this where I really appreciate object-oriented programming…
Re: I’ve never used basic much, so going with your style as an example…
Stuff like this where I really appreciate object-oriented programming…
I’ve never used basic much, so going with your style as an example…
‘ define your user data type structure
structure fourByteCommand
dim instruction as byte[4]
end structure
‘ create an instance variable of that structure
dim powerOn as fourByteCommand
powerOn.instruction[0]=0x00
powerOn.instruction[1]=0x00
powerOn.instruction[2]=0x00
powerOn.instruction[3]=0x80
‘ down in the subroutine pass it in
sub procedure sendCommand(dim iPodCommand as fourByteCommand, dim destUart as byte)
‘ etc, and down while you’re sending just use iPodCommand.instruction as the array you’re iterating through.
Well, crap, that doesn’t let you have variable length commands, but I think you get the idea, you can work from there.
Gur. I’m still not getting this, and I can’t quite find an example of it. heh.
Hell if I knew any of that before this came up, thank google, not me ;0)
<sigh> Thank you. :)
Now I feel dumb. For some reason I thought all the docs were in the help file…
i would also lke to point out that page 72 probably has your answer.
i’d try changing your prototype to:
sendCommand(dim byref iPodCommand as ^char, dim destUart as byte).
then pass as normal and make the appropriate functional changes since you are sending just an array of char/bytes.
Of course, byref will just cause it to pass by reference… which he currently believes is happening and is a cause of trouble. Maybe it is, maybe it isn’t… trying it explicitly may yield useful information, though.
Though, being on my iBook, I can’t look at his basic source here to get context of what you’re suggesting, so I maintain that I may be talking out of my ass.
For what it’s worth, the source is right here:
http://nuxx.net/files/hml_mikrobasic_help.txt
For what it’s worth, the source is right here:
http://nuxx.net/files/hml_mikrobasic_help.txt
Of course, byref will just cause it to pass by reference… which he currently believes is happening and is a cause of trouble. Maybe it is, maybe it isn’t… trying it explicitly may yield useful information, though.
Though, being on my iBook, I can’t look at his basic source here to get context of what you’re suggesting, so I maintain that I may be talking out of my ass.
For what it’s worth, changing string[10] to ^char doesn’t compile, saying that “Pointer parameter cannot be passed by reference”
For what it’s worth, changing string[10] to ^char doesn’t compile, saying that “Pointer parameter cannot be passed by reference”
i would also lke to point out that page 72 probably has your answer.
i’d try changing your prototype to:
sendCommand(dim byref iPodCommand as ^char, dim destUart as byte).
then pass as normal and make the appropriate functional changes since you are sending just an array of char/bytes.
Page 73 of this mikrobasic manual has the info you need.
Make an array inside your structure and pass the structure to the function, access the array by using structureName.arrayName, should work, I think.
Hmm… I hope I’m not being too ignorant or demanding here, but can you point me to an example of that?
Oh, gotcha, the only file format I recognized in the zip was .asm
Passing each element should apply, then, and I believe most basic variants have some equivalent of a struct, though it’s not always called that or done exactly like C.
I’m not using either — it’s Basic, specifically mikroBasic.
Oh, wait, I take that back, I really know nothing about ASM programming… I assumed you were using a C compiler… carry on.
There are ways around this, you can pass each element seperately and recreate the array (lame, but it works), or wrap the array in a struct, which can be passed by value.
Since it’s an array it has to be sent as a reference. :\
It seems that the first parameter is passed in as a reference. Try sending it as a normally a copy instead of by reference.