Dec 12, 2009

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++; 
}

2 comments:

  1. Hi Michael,
    Could you please tell me, to your knowing, would SL4 work with an unmanaged dll that allows us to talk to a serial rs232 device from a WinForms app? Dll's functions of that unamaged dll (and I think not COM) are all DllImported when used in WinForms app. Would then a deployment of that dll alongside a Silverlight app be a big problem (sorry I'm a SL noob)?
    TIA

    ReplyDelete

Followers