return codes of a shell command

pmu5757

Member
Licensed User
Hello everybody
On the desktop, with the "shell" command, I can execute a dos command.
How can I do to set a B4PPC variable with the return code of this dos command ?

Thank you.

Pascal.
 

Cableguy

Expert
Licensed User
Longtime User
Not sure WE understand what you mean..
can you give an example of what you want to do and expect from it?
 

pmu5757

Member
Licensed User
Here are the details you ask :
I would like to use B4PPC to command a lego robotic brick, the "Mindstorms RCX 2.0".
I've found on the web, a program called "nqc" that can send commands to the lego brick by typing dos commands.
For exemple the command "c:\nqc.exe -Susb -raw 5103" makes the brick play the internal sound number 3.
Another example, the command "c:\nqc.exe -Susb -raw 2181" makes the brick start the motor number 1.
My problem is that other commands get the value of a sensor that is connected on the brick.
For example, the command "c:\nqc.exe -Susb -raw 120901", when I type it in a dos window returns the value of sensor number 1.
In B4PPC, I would like to put this return value in a variable.

Thank you.
 

Cableguy

Expert
Licensed User
Longtime User
How does the "brick" conect to the desktop...?
perhaps it all can be done with serial com...
 

Cableguy

Expert
Licensed User
Longtime User
yes, but from what I could google about mindstorms, there are a few ocx, and .net based dll, so a wrapper should sufice, thus making the need of shell obsolete....
And far better performance I think...

Here's one:
 
Last edited:

pmu5757

Member
Licensed User
The Mindstorms brick I have is the old one, the RIS 2.0, and not the new one (NXT).
The line between the brick and the PC is infrared : an infrared sender is connected to one usb port of the PC.
I agree with Cableguy, concerning the low performance of the shell commands, but I personnaly don't know how to do a "wrapper", I only know B4PPC, So I'll try with shell commands.

Thank you
 

Cableguy

Expert
Licensed User
Longtime User
since you are using an IR tower, it must have a com port assigned to it, then the rest should be "normal" serial comunication...
I found alot of info about mindstorms and .net, also some sets of command and relations....
Nothing like an hands-on aproach to see if it works...
use one of the serial IR examples to send a simple, ON/OFF command and see if it works...
If so, build on it...

PS.I wish I had one of those to play with....
perhaps if you decide to upgrade to the new version...argh just daydreamming
 

agraham

Expert
Licensed User
Longtime User
since you are using an IR tower, it must have a com port assigned to it
I don't think it does. I believe that it requires its own USB driver to be installed and my guess would be that it is an HID (Human Interface Device) device (like a keyboard or mouse) as this is an easy way to interface a slow(ish) speed device to USB without writing a complicated driver as Windows already has a generic HID driver available. The interface is by File I/O calls and calls to a Windows dll - HID.DLL
 

Cableguy

Expert
Licensed User
Longtime User
continuin googling I found that RIS2.0 should have come with, msot probably, a CD with some software, including .NET runtimes (DLL's)...
If this is to be true, then one of two things are true..
They(the dll's) run from B4PPc or need a wrapper...
The IR tower, as far as I could figure, is standart issue in RIS2.0
 

pmu5757

Member
Licensed User
Agraham,
As I'm not a B4PPC expert, I think I need an example of using your library with its "lastexitcode" property.
Could you do it for me ?
I just need to launch a shell command and put its return code in a variable.
Thank you.

Pascal.
 

pmu5757

Member
Licensed User
Hello Agraham
Thank you for your example, it's very clear for a newbie like me.
But unfortunately, it doesn't work.

In a dos windows I type :
C:\>c:\nqc.exe -Susb -raw 120901
It answers :
54 00
This works

In your example, I use the line :
Process.Start("c:\nqc.exe" ,"-Susb -raw 120901")
Result : the command is launched (I can see the red light on the infrared lego tower), but a 0 is returned.

I suppose that your example only gets error codes and that "54 00" is not an error code. So it answers "0" that means "no error".

I then tried to redirect the result of the command in a text file :
In a dos widow, I type : C:\>c:\nqc.exe -Susb -raw 120901 > sortie.txt
Then the file "sortie.txt" contains "54 00" : it works.

In your code I then use the line : Process.Start("c:\nqc.exe" ,"-Susb -raw 120901 > sortie.txt")
I obtain the number "-13" that indicates an error I presume, because the file "sortie.txt" is neither created, nor modified if it exists...

I think I still need help...

Pascal.
 

agraham

Expert
Licensed User
Longtime User
pmu5757 said:
How can I do to set a B4PPC variable with the return code of this dos command ?
My example does just this. The problem is that it is not the return code that you require :(

In a dos widow, I type : C:\>c:\nqc.exe -Susb -raw 120901 > sortie.txt
Then the file "sortie.txt" contains "54 00" : it works.
It looks like that app writes the number to StdOut. StdOut and redirection of it only exists for a console app running in a DOS window. Executing it directly under Windows using Shell doesn't provide access to StdOut. The following code line starts the command interpreter "cmd" and instructs it "/c" to execute the rest of the argument string then terminate.

B4X:
Process.Start("cmd", "/c c:\nqc.exe -Susb -raw 120901 > " & AppPath & "\sortie.txt")
 

pmu5757

Member
Licensed User
Thank you Agraham.
your "cmd" works well with a line like :
Process.Start("cmd", "/c c:\nqc.exe -Susb -raw 120901 >> c:\sortie.txt")

But not with your line with the AppPath statement...

It doesn't matter, it's enough for me.

Best regards

Pascal.
 

pmu5757

Member
Licensed User
Sorry, but with my poor english, I don't understand what you mean with "make a typo in appending the strings".

Anyway, your other idea is good : using the "cmd" only when I need to return a value, and using the standard shell command if not.

Pascal.
 

agraham

Expert
Licensed User
Longtime User
"make a typo in appending the strings".
Make a mistake when adding the three strings for the arguments parameter
"/c c:\nqc.exe -Susb -raw 120901 > " & AppPath & "\sortie.txt"

using the "cmd" only when I need to return a value, and using the standard shell command if not.
What I meant was that you can use cmd with the standard Shell command instead of using Process as you don't want the exitcode of the app, only what it writes to StdOut.
 
Top