Bare Minimum Rhyduino Example

This topic explains the minimum steps that are necessary for creating a program that uses the Rhyduino library to establish communications with an Arduino device.

Assumptions

I am assuming that you have already done the following (or equivalent):

  1. Created a project in Visual Studio and referenced the Rhyduino.dll assembly.
  2. Connected an Arduino Duemilanove (or Uno) to your computer via a USB cable, and that the computer recognizes the connection.

Writing Code

The central object in the Rhyduino library is the Arduino class. Everything starts there.

As simple as it gets

For the first example I will show you the simplest possible way to create an instance of this class and associate it with your physical Arduino.

Code Snippet
  1. using Rhyduino;
  2.  
  3. class Program
  4. {
  5.     static void Main()
  6.     {
  7.         using (var arduino = new Arduino("COM3"))
  8.         {
  9.             // Do work
  10.         }
  11.     }
  12. }

As you can see on line 1, I have chosen to import the Rhyduino namespace with a using statement. It's a matter of personal preference. You choose not to, you'll just have to use fully-qualified object names (i.e. “Rhyduino.Arduino” instead of “Arduino”).

The next thing you'll notice is that I've placed the Arduino constructor call inside a using block. The Dispose() method should always be called when you're done using an instance of the Arduino object; putting it in a using block makes sure that it's called automatically when the object goes out of scope. I often forget to dispose of objects when I'm done with them, so this saves me (and you) the headache.

There is no default constructor for the Arduino class. At a minimum, you need to specify the COM port that the Arduino is physically connected to. If you are unsure which COM port the Arduino is using, you can use Windows Device Manager to identify the connected device.

DeviceManager

The code shown above doesn't actually open a connection to the device immediately. Instead it opens the connection the first time you attempt to use it to perform an operation that requires a connection. Additionally, it assumes that you will be communicating with the device at the default baud rate of 57,600 baud. This is the value that is used in the StandardFirmata.pde sketch. The same value needs to be used in both places, so if you would like to communicate at a different speed you will need to modify the Arduino sketch directly to reflect the change.

Being more direct

The following example builds on the previous example by showing you how to specify an alternate baud rate and override the delayed opening of the connection.

Code Snippet
  1. using Rhyduino;
  2.  
  3. class Program
  4. {
  5.     static void Main()
  6.     {
  7.         using (var arduino = new Arduino("COM3", 57600, true))
  8.         {
  9.             // Do work
  10.         }
  11.     }
  12. }

The advantage of opening the connection as part of the constructor is that you will be notified immediately (via an exception) if Rhyduino is unable to connect to the Arduino.

That's all there is to it. If you encounter problems, post them on one of our discussion lists or in the issue tracker. I want to hear about it so I can help you and/or fix the problem.

Last edited Nov 10, 2010 at 7:42 PM by RhyMednick, version 1

Comments

Kamaloo Jan 1, 2014 at 3:23 AM 
System.IO.FileNotFoundException was unhandled
Message=Could not load file or assembly 'TracerX-Logger, Version=4.0.1005.1163, Culture=neutral, PublicKeyToken=134ad74f7a35afb2' or one of its dependencies. The system cannot find the file specified.
Source=Rhyduino
FileName=TracerX-Logger, Version=4.0.1005.1163, Culture=neutral, PublicKeyToken=134ad74f7a35afb2
FusionLog==== Pre-bind state information ===
LOG: User = Kamalesh-Monkey\Kamalesh
LOG: DisplayName = TracerX-Logger, Version=4.0.1005.1163, Culture=neutral, PublicKeyToken=134ad74f7a35afb2
(Fully-specified)
LOG: Appbase = file:///C:/Users/Kamalesh/documents/visual studio 2010/Projects/arduinofirmata/arduinofirmata/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : Rhyduino, Version=1.0.0.4416, Culture=neutral, PublicKeyToken=134ad74f7a35afb2.
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: TracerX-Logger, Version=4.0.1005.1163, Culture=neutral, PublicKeyToken=134ad74f7a35afb2
LOG: Attempting download of new URL file:///C:/Users/Kamalesh/documents/visual studio 2010/Projects/arduinofirmata/arduinofirmata/bin/Debug/TracerX-Logger.DLL.
LOG: Attempting download of new URL file:///C:/Users/Kamalesh/documents/visual studio 2010/Projects/arduinofirmata/arduinofirmata/bin/Debug/TracerX-Logger/TracerX-Logger.DLL.
LOG: Attempting download of new URL file:///C:/Users/Kamalesh/documents/visual studio 2010/Projects/arduinofirmata/arduinofirmata/bin/Debug/TracerX-Logger.EXE.
LOG: Attempting download of new URL file:///C:/Users/Kamalesh/documents/visual studio 2010/Projects/arduinofirmata/arduinofirmata/bin/Debug/TracerX-Logger/TracerX-Logger.EXE.

StackTrace:
at Rhyduino.Arduino..ctor(String portName, Int32 baudRate, Boolean autoOpen)
at ConsoleApplication1.Program.Main(String[] args) in C:\Users\Kamalesh\documents\visual studio 2010\Projects\arduinofirmata\arduinofirmata\Program.cs:line 15
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: