May 4, 2009

way to get my silverlight application talking to the COM port on my local computer

There's no access to computer resources, but you can communicate with sockets. So it's possible to have your users install an application which listens on a port and communicates with a COM port. You can do this by opening a socket connection from your Silverlight control to a local socket which intern have the COM port access

Use a XBAP (Browser Deployed WPF) that requests some extra permissions. Would live inside the browser, but only run on Windows machines with .NET 3.0 / 3.5 installed

Write an ActiveX control that talks to the local device and expose an API. Then use JavaScript to consume this API and interact with the JavaScript code from Silverlight. This would give you a in-browser application, but your users would have to install an additional device  ActiveX component.

SL and Javascript two way connectivity sample:
http://blogs.msdn.com/salvapatuel/archive/2008/05/01/build-interaction-between-javascript-and-silverlight-2.aspx

Calling ActiveX from Javascript
http://www.coderanch.com/t/121777/HTML-JavaScript/Editing-Excel-Sheet-using-javascript

Sample code to connect SL, Javascript and ActiveX
SL Mainpage.xaml:
<UserControl x:Class="JavascriptTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300"
             Name="Xaml1">
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel>
        <TextBox Name="NameTxt" Width="500" Height="20"></TextBox>
        <Button Name="NameBtn" Content="Send to JS"></Button>
        </StackPanel>
    </Grid>
</UserControl>

SL Mainpage.xaml.cs:
namespace JavascriptTest
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            NameBtn.Click += new RoutedEventHandler(NameBtn_Click);
            HtmlPage.RegisterScriptableObject("navObj", this);
        }

        void NameBtn_Click(object sender, RoutedEventArgs e)
        {
            HtmlPage.Window.Invoke("ExecuteAnimation", NameTxt.Text);
        }

        [ScriptableMember()]
        public void SendMessage(string state)
        {
            MessageBox.Show("Message reached SL");
        }
    }
}

test.html:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>JavascriptTest</title>
    <script type="text/javascript">
        function ExecuteAnimation(params) {
            loadexcel(params);
            alert("Hello, World");
        }
        function load() {
            var ctrl = $get("Silverlight1");
            ctrl.Content.navObj.SendMessage('Hello');
        }
        function loadexcel(params) {
            var ExcelSheet;
            ExcelSheet = new ActiveXObject("Excel.Application");
            ExcelSheet.Workbooks.Open(params);
            //ExcelSheet.Visible = true;
            // Place some text in the first cell of the sheet.
            ExcelSheet.ActiveSheet.Cells(1, 1).Value = "This is column A, row 1";
            // Save the sheet.
            ExcelSheet.SaveAs("C:\\TEST.XLS");
            // Close Excel with the Quit method on the Application object.
            ExcelSheet.Quit();
            // Release the object variable.
            ExcelSheet = "";
        }
        </script>
    <style type="text/css">
    html, body {
        height: 100%;
        overflow: auto;
    }
    body {
        padding: 0;
        margin: 0;
    }
    </style>
</head>
<body >
    <form id="form1" runat="server" style="height:100%;">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <div  style="height:90%;">
            <asp:Silverlight ID="Silverlight1" runat="server" Source="~/ClientBin/JavascriptTest.xap" MinimumVersion="3.0.40307.0" Width="100%" Height="100%" />
        </div>
        <input type="submit" onclick="load()" />
        <input type="submit" onclick="loadexcel()" />
    </form>
   
</body>
</html>



1 comment:

  1. URL for Socket programming
    http://silverlight.net/learn/learnvideo.aspx?video=66740

    ReplyDelete