Everything about my daily life as a programmer/Electrical Engineer!

Silverlight talking to Arduino

This is an initial rough post, but I have managed to get Silverlight 4 beta talking to an ActiveXperts COM+ interface that talks to an Arduino over a serial connection.

Here is my first attempt to do a video of it. Looks like I’m going to need to learn how to make a screencast.

 

 

image

 

The C# code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interop;
using System.Text;
using System.Threading;

namespace TestSerial
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
if (App.Current.InstallState == InstallState.NotInstalled)
{
App.Current.Install();
}
}
dynamic com;
private void button2_Click(object sender, RoutedEventArgs e)
{

com=ComAutomationFactory.CreateObject("ActiveXperts.ComPort");
dynamic count = com.GetDeviceCount();
StringBuilder sb = new StringBuilder();

List<dynamic> devices = new List<dynamic>();
for(int i=1;i<=9;i++)
{
devices.Add("COM" + i);
}

for (int i = 0; i < count; i++)
{
devices.Add(com.GetDevice(i));

}
devicelst.ItemsSource = devices;

}

private void button3_Click(object sender, RoutedEventArgs e)
{
//string device = devicelst.SelectedItem.ToString();
if (devicelst.SelectedItem == null) { MessageBox.Show("Please pick a port"); return; }
com.Device = devicelst.SelectedItem.ToString();
com.Open();
MessageBox.Show(com.GetErrorDescription(com.LastError));
string buffer = "";
System.Threading.Thread t = new Thread(new ThreadStart(delegate()
{

while (1 == 1)
{
com.Sleep(200);
buffer = com.ReadString();
if (buffer == "") { com.Close(); return; }
tb.Dispatcher.BeginInvoke(delegate()
{
tb.Text += "\r\n" + com.ReadString();
});
}
}));
t.Start();
}


}
}
 
The Xaml.
<UserControl x:Class="TestSerial.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<
Grid x:Name="LayoutRoot" Background="White">
<
Grid.ColumnDefinitions>
<
ColumnDefinition></ColumnDefinition>
<
ColumnDefinition></ColumnDefinition>
<
ColumnDefinition></ColumnDefinition>
</
Grid.ColumnDefinitions>
<
Grid.RowDefinitions>
<
RowDefinition Height="Auto"></RowDefinition>
<
RowDefinition Height="Auto"></RowDefinition>
<
RowDefinition></RowDefinition>
</
Grid.RowDefinitions>
<
Button Content="Install" Height="23" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" Grid.Row="0"/>
<
Button Content="Get Devices" Height="23" HorizontalAlignment="Left" Name="button2" VerticalAlignment="Top" Width="75" Grid.Row="1" Click="button2_Click"/>
<
ComboBox Name="devicelst" Grid.Row="1" Margin="1,0,0,0" Grid.Column="1"></ComboBox>
<
Button Content="Connect" Grid.Column="3" Grid.Row="1" Height="23" HorizontalAlignment="Left" Name="button3" VerticalAlignment="Top" Click="button3_Click"/>
<
TextBox Name="tb" Grid.ColumnSpan="3" Grid.Row="2" VerticalScrollBarVisibility="Visible"></TextBox>
</
Grid>
</
UserControl>



/*
  ASCII table
Prints out byte values in all possible formats: 
* as raw binary values
* as ASCII-encoded decimal, hex, octal, and binary values
For more on ASCII, see http://www.asciitable.com and http://en.wikipedia.org/wiki/ASCII
The circuit:  No external hardware needed.
created 2006
by Nicholas Zambetti
modified 18 Jan 2009
by Tom Igoe
<http://www.zambetti.com>
*/
void setup()
{
  Serial.begin(9600);

  // prints title with ending line break
  Serial.println("ASCII Table ~ Character Map");
}

// first visible ASCIIcharacter '!' is number 33:
int thisByte = 33;
// you can also write ASCII characters in single quotes.
// for example. '!' is the same as 33, so you could also use this:
//int thisByte = '!'; 

void loop()
{
  // prints value unaltered, i.e. the raw binary version of the
  // byte. The serial monitor interprets all bytes as
  // ASCII, so 33, the first number,  will show up as '!'
  Serial.print(thisByte, BYTE);   

  Serial.print(", dec: ");
  // prints value as string as an ASCII-encoded decimal (base 10).
  // Decimal is the  default format for Serial.print() and Serial.println(),
  // so no modifier is needed:
  Serial.print(thisByte);     
  // But you can declare the modifier for decimal if you want to.
  //this also works if you uncomment it:

  // Serial.print(thisByte, DEC); 

  Serial.print(", hex: ");
  // prints value as string in hexadecimal (base 16):
  Serial.print(thisByte, HEX);    

  Serial.print(", oct: ");
  // prints value as string in octal (base 8);
  Serial.print(thisByte, OCT);    

  Serial.print(", bin: ");
  // prints value as string in binary (base 2)
  // also prints ending line break:
  Serial.println(thisByte, BIN);  

  // if printed last visible character '~' or 126, stop:
  if(thisByte == 126) {     // you could also use if (thisByte == '~') {
    // This loop loops forever and does nothing
    while(true) {
      continue;
    }
  }
  // go on to the next character
  thisByte++; 
}

18 comments:

Mixmasterxp said...
This comment has been removed by the author.
Michael said...

Silverlight 4.

TheIceMan said...

Do you have an actual Project you can send? I cant get the ActiveX Com object to be reconized in my project!

Unknown said...

Michael, why did you choose the ActiveXperts component over something that requires no installation. Or: I s there a serial port COM-Component that comes with Windows?

sohaib said...

Hi michael, i'm working on a web application(using silverlight 3 and VS08). I need to read data from a serial port to my application on the browser side, can it be done? will i have to upgrade to silverlight 4 and VS10?
thanks.

Michael Stephens said...

SL4 would make it easier but there is a way. You can have the client install a local web service that exposes the Serial Port. Then SL3 connects on localhost:1234\Serial.svc . I refer to this as bridging. Bridging is of course difficult to get a person to install a mini web service on their comp.

windrago said...

awesome work - why SL4 makes it easier?

unthink said...

Is this cross platform? and I am guessing this is all compacted into one file, not like the typical bridge, such as Flash and Merapi for example.

Michael Stephens said...

Unfortunately this is not cross platform as far as I know. I installed a COM library and communicated through COM. I'm not sure if MAC has COM etc. If they have the library (or you can find a windows com library for serial) then no bridge app is required. No web service or socket.

Unknown said...

Hi Michael,
To your knowing, would SL4 work with an unmanaged dll, one that allows us to talk to a serial rs232 device from a WinForms app? Dll's functions are all DllImported. Would then a deployment of that dll be a problem (sorry I'm a SL noob)?
TIA

swetha said...

Hi , I am a silverlight newbie. I tried adding AComport.dll as a reference in my silverlight project which did not work. After reading a few forums I learnt one cant reference dll's straight on silverlight. Can you please help me on how to proceed? I really need to control COM through silverlight. Thank you.

Michael Stephens said...

com=ComAutomationFactory.CreateObject("ActiveXperts.ComPort"); is the line you want. Make sure to Reference Microsoft.CSharp

swetha said...

thank you so much for your prompt reply. I had missed CSharp reference. Plus ComAutomationFactory has been changes to AutomationFactory.

Oscar Bedoya said...

Good day!
I am getting in com.Sleep(200) line,
InvalidOperationException
Acceso entre procesos no vĂ¡lido.

Some aditional info or permissions to Thread?
Best regards, and congratulations and Thanks for your Post

bharathp said...

Am getting this Exception "No object was found registered for the specified progID". Please help me

Dega said...

Hi Oscar, I am also having same issue. How did you resolve this? Could you please let me know? I am stuck here.
Regards
DEGA

Unknown said...

hi
i need the code
please can i get your code
send to me
so my mail is wwkim3@hanmail.net

Eduardo said...

Hi
What is ActiveXperts.ComPort?
Is it a Dll, an exe?

I got an error "No object was found registered for specified ProgID"