BrainStem.NET Library
The BrainStem.NET library is a C# implementation of the Acroname BrainStem command set. It is not a managed wrapper to the existing BrainStem C library, but is a native .NET library that writes to the BrainStem using its serial protocol. The library was built with Visual Studio 2008 against the .NET 2.0 Framework.
I’ve released this library under the BSD License. Pre-compiled binaries and source code are provided in the distribution below.
Command Set
I started this library with the specific goal of moving servos for a robotics project. As such, I only intended to implement some of the commands available through the serial protocol. However, once I got started, I included support for digital and analog-to-digital IO, system parameter gets and sets, and a number of other features. At the time of this writing the following commands are supported:
Test Harness
I’ve included a Windows application to test the library. This application is akin to the GP Application from Acroname. Here are a few screenshots:
Documentation
I used Sandcastle and the Sandcastle Help File Builder to generate the documentation. It’s included in the downloadable package in compiled help format (.chm). An online version is also available.
Getting Started
A look at the test harness is a great way to familiarize yourself with the library. I’ve also included Command-Line and Windows sample projects, but here’s a few notes on getting started.
There are three main classes, all in the Bespoke.Robotics.BrainStem namespace: Packet, Stream, and CommandManager. Packets represent communication units within the system (Host-to-BrainStem, BrainStem-to-Host, and BrainStem-to-Router). You won’t normally need to create packets on your own, nor process received packets. The CommandManager class will handle all of this for you. The Stream class represents a connection between the host and the BrainStem. This class handles the reading and writing of Packets over the serial port. The Stream class includes two event hooks, HeartbeatReceived and PacketReceived, that allow you to process received packets if you’re so inclined. Again, the CommandManager will handle this for you, but this can be helpful at times. The CommandManager class contains a series of static methods that represent the supported command set.
You can setup the API with as little as 3 lines of code. Instantiate and open a Stream object and initialize the CommandManager with the newly opened Stream.
Stream stream = new Stream();
stream.Open();
int commandReplyTimeout = 1000;
CommandManager.Init(stream, commandReplyTimeout);
After that, begin making calls to the CommandManager methods.
CommandManager.SaveServoParameters(mModuleAddress);
CommandManager.SoftwareReset(mModuleAddress);
More Information
If you have any questions or comments I’d like to hear from you. You can reach me at pvarchol@bespokesoftware.org or leave a comment below.
Downloads (Current Version: 2.0 – Release Date: 1/17/2010)
- Source + Binaries (.zip)
- API Documentation (.chm)
- API Documentation (.html)
- License – BSD
Previous Revisions
- 1.00.002 (8/14/2006)






Great work on this. Thanks for contributing to the community.
On a side note, might I ask what software you have powering your blog here? I like it alot better than the solution I have on my site.
Hi Keir,
Sorry it’s taken me some time to get back to you. I’m using WordPress for this site.
Paul
I am taking a stab at building my first robot from scratch instead of a kit. I saw that Microsoft just released their next CTP of Robotics Studio and was wondering if you had used it yet with the brainstem. Also, after compiling the code in .NET how do I put the program onto the brainstem? (Sorry if this was explained above)
Hi Justin,
I haven’t used the Microsoft Robotics Studio as of yet. Though I’m interested to see what it’s all about.
As for using the BrainStem.NET library; you don’t install the library on the BrainStem module itself. Instead, the BrainStem.NET library runs on a PC and communicates to the BrainStem GP module through a serial port. You can use any PC that’ll run the .NET framework, and I’m certain that the library would even run on the .NET Compact Framework, though I haven’t spent the time to test this. With the .NET Compact Framework you could run the library from a PDA.
I’ve used the library from a mini-ITX motherboard, from a laptop, and a regular desktop. But if you’re looking for portablilty, the smaller the better. Another alternative is an RF-serial module that can further disconnect your PC running the BrainStem.NET library from the BrainStem module.
Paul
Thx Paul, however I guess I wasn’t clear when I said “compile code in .NET”, sry my fault. What I meant was after I write a program for the robot in .Net how do I compile it to the .tea format and then load it to the memory of the brainstem. Can I still just use the Console App from Acroname and the commands “steep”, “load”, and “launch”. Thanks for your help and patience explaining this to me. I’m still kind of a noob when it come to .NET.
If I understand your question correctly, what you’re asking isn’t possible. Programs written against the .NET environment can’t be compiled into TEA format, loaded into and run on the BrainStem. You could potentially port your .NET applications to TEA, but this is essentially rewriting your application.
There is no way to execute a .NET application directly on the BrainStem module. This is the primary reason why I wrote the BrainStem.NET library. I wanted the comprehensive feature set the .NET Framework Class Library offered, wanted to code in C#, and wanted those applications to communicate with a BrainStem. Since direct execution isn’t possible, serial communication was the solution.
Paul
Oh, I think I misunderstood how this all worked. So you are saying that you write the programs in .NET and run the code on a computer that then sends the commands to the brainstem to be executed via a serial cable or some other connection.
That’s correct. And don’t worry about working at too low a level (you don’t need to know the serial protocol). The BrainStem.NET library abstracts most of the complexity. All you need to do from your .NET applications is make calls into the BrainStem.NET API.
Paul
Hi Paul,
Thanks for the excellent work. I have extended your API and Test harness to work with the branstem moto – either networked to a brainstem or stand alone. I have a few more command to implement then I’ll send it to you for review before I publish it.
I’m planning on writing drivers for the Microsoft Robotics Studio, have you looked at this yet? Would be great to have someone to collaborate with on this project.
Andy.
Oh I forget to mention that I looked at another MS Robotics Studio driver written for the devantech devices – it gives you an idea of whats needed for brainstem drivers. This is available from http://www.robot-electronics.co.uk/shop/Examples.htm (near bottom of page) and discussed at http://channel9.msdn.com/ShowPost.aspx?PostID=232998
Andy.
Hi Andy,
I released a new set of Devantech Drivers today. They can be downloaded from:
http://www.diversity.co.uk/DevantechDrivers.html
I have never had the chance to play with a brainstem, but would be happy to share my experience in doing the Devantech drivers and helping RoboticsConnection with their Serializer drivers.
It does take quite a lot of effort to get a clean result – not something I have achieved quite yet, but am gettting there, if slowly. I have a codesmith template which can take away a little of the typing and could be adapted, and extended to be right for the BrainStem.NET library.
Best wishes,
Chris
[...] BrainStem.NET Library [...]
[...] BrainStem.NET Library [...]
First of all, great work.
I’m trying to implement the API on .NET Compact Framework running on a PocketPC.
I’ve tried using the dll’s in the Brainstem.Net-Binaries folder but it came up with runtime errors of having mismatched desktop and CF reference.
Is there a way to get around this? Or do I have to rebuild everything using the source files?
I am neither an expert in .NET nor C# so some extra help would be greatly appreciated!
Max
Hi Max,
It should be quite easy to use the BrainStem.NET library on a PocketPC — but you do have to modify and rebuild the source. I’ve had a number of people comment on this — and at least one person has already done the modification (though I don’t have the updates he made). I intend to provide support for the Compact Framework in a future release. However, I’m not expecting to do the next release until mid-December.
Paul
Hey Paul,
Can’t wait for your next release! But in the mean time, do you have any hints on where to start modifying? I am thinking just the reference part, as code-wise they should be compatible, right?
Thanks,
Max
Max,
That’s correct. I doubt there will be many code compatibility issues when porting the library to the Compact Framework. It’s likely just references.
Paul
I’m interested in two things mentioned by other users. (1) A C# implementation for the Brainstem Moto, including Moto only functions. (2) A port for the C# Compact Framework. I’m currently working on both of these, but if someone already has a working version already it would be nice to have it on the site.
[...] BrainStem.NET Library [...]
[...] BrainStem.NET Library [...]
[...] BrainStem.NET Library [...]
Nice Work !
Hello.
I’m new there
Nice forum!
Hi
I am trying to connect two brain stem module using one application. Is it possible to connect two brain stem module simultaneously from same application using your library. I am new to C#, some kind of example can be very helpful to understand the implementation
Thanks
Abhinav
HI Abhinav,
Yes, this is possible. All you need to do is specify the module address (which is the first parameter of most of the functions within the library). Two BrainStem modules daisy chain together, so physically you only connect the first BrainStem module to the serial port of your PC. When the first module receives a command to an address, other than its own, it forwards that command through its IIC port — which is where your second module is connected.
Paul
Hi Paul
Thanks for your reply. I have another question for you. From my understanding, the given sample source code in your website works similar to Brainstem Console. When i send the command “2 3 0″ in brainstem console, my bot do response but when i send the same command with your source code. I receive the error “Expected delay message response”.
I am not sure what is happening. Please let me know what do you think
Hi Abhinav,
Assuming your 2 3 0 indicates, 2 as the module address, 3 as the command code, and 0 as the data for the command — I’m unsure what the command 3 is. According to the BrainStem Documentation (http://www.acroname.com/brainstem/ref/ref.html) the command value 3 is a reserved command called cmdTMR_EXP (http://www.acroname.com/brainstem/ref/h/Commands/cmdTMR_EXP.html). What command are you intending? Moreover, my API doesn’t permit free-form commands, so what method are you calling to process this message? Lastly, I don’t have an error message that reads “Expected delay message response”. I have an “Expected debug message response”, is that the error you’re receiving?
Paul
very intresting
I was able to get my prototype built very fast using your API. Thanks! But I am having trouble communicating with the devantech thermal array sensor. It is an i2c device with the address of 0xd0. With the brainstem console I am able to grab the bits using the following:
0xd0 1
2 37 128 0xd1 1
I am not sure how to use the CommandManager to do this. Any hints would be greatly appreciated.
Hi Paul,
I can successfully send the commands but i can not read anything back from the brainstem. I get the error message “Expected debug message response.”
Can you suggest me what should i be doing to fix this problem.
Thanks a lot, I appreciate your help.
Abhinav
Hi Abhinav,
What command are you calling from the BrainStem.NET API? Recall from my previous post, that the API doesn’t behave like the BrainStem console application — it doesn’t allow free-form commands. Instead, the API exposes the BrainStem command set through a set of function calls.
Paul
Hi Paul,
I am getting the errors
result = CommandManager.GetServoPosition(mModuleAddress, mServoId);
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
result= CommandManager.Send(mModuleAddress,data);
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Packet replyPacket = Instance.GetPacketFromQueue(Packet.CommandCodeType.cmdPAD_IO);
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
replyPacket is empty although my robot execute the command succesfully (i.e it does what it suppose to do) but no i am not getting any feedback.
Thanks
Abhinav
Abhinav,
I had the same problem. It happens when you call GetServoPosition from the Form_Load event, and the CommandManager.Init has not finished initializing. I confirmed this by adding a 500 millisecond delay to the Form_Load event, using Thread.Sleep(500), before I call GetServoPositionThread.
Hello,
I am trying to use the ReadDigitalIoTimer CommandCodeType, but I just can’t seem to get the syntax right. I have an accelerometer that outputs digital high low pulses. The pulse length can be read to simulate the variable output of an analog signal. I tried the code below, and set a break point in the loop to inspect the packet, but currently the byte array is populated with the module address, the pin number, and the 29 code representing the ReadDigitalIoTimer. Any help would be greatly appreciated.
Mike
Code
———————
Packet packet = new Packet(mModuleAddress, Packet.CommandCodeType.ReadDigitalIoTimer, Packet.DirectionType.StemToHost);
while (true)
{
byte[] data = packet.ToByteArray();
Console.WriteLine(data[0] ” – ” data[1] ” – ” data[2]);
Thread.Sleep(100);
}
———————
[...] BrainStem.NET Library [...]
Hi Mike,
The ReadDigitalIoTimer command is fairly complicated. You can see details about the command in the Acroname BrainStem reference at http://acroname.com/brainstem/ref/h/Commands/cmdPTIME_RD.html.
Your question prompted me to look at the last time I updated the BrainStem.NET library — which was in August 2006. So, rather than relay the details of how to implement this command, I went ahead and did it myself, and released the 2.0 version of the BrainStem.NET library.
Please note, I have not fully tested the functionality of the ReadDigitalIoTimer implementation. I’m confident in the outbound packet structure, but my parsing of the cmdDEV_VAL reply packet is questionable.
Hope this helps.
Paul
Paul,
Thank you very much for the update, and all your work that went into this library.
Mike
Hello Paul and everyone on the blog,
Looking to build a list of developers who I can send simple programming jobs to.
Each project has a real-world application and before letting them loose on the students, each must be prototyped and bench tested for obvious reasons.
Aimed at college level, with the idea of sparking interest in mechatronics that the students themselves can follow. Robots are the perfect entry to this discipline.
What the students work on must be robust in hard and soft wares, thus robotics fits the needs and Paul’s libraries for Brainstorm make so much sense.
Yes, there is a small budget to help defray the costs with each build on a fixed price / fixed time basis.
Looking to build longer term relationships, so things will be simple and cheap to start with.
Best regards,
Harry K
harry1233 at excite dot com
.
A very interesting .Net development. Anticipating further progress updates in this field. Will look out for next post.