Skip to content

Instantly share code, notes, and snippets.

@jrsconfitto
Created February 8, 2013 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrsconfitto/4738994 to your computer and use it in GitHub Desktop.
Save jrsconfitto/4738994 to your computer and use it in GitHub Desktop.
<Window x:Class="SerialCommunication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBox Name="Message"
Grid.Column="0">
Type here
</TextBox>
<Button Grid.Column="1"
Click="Button_Click">
Send to Arduino
</Button>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace SerialCommunication
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private SerialPort _serialPort;
public MainWindow()
{
InitializeComponent();
_serialPort = new SerialPort("COM3", 9600);
_serialPort.Open();
this.Closed += new EventHandler(MainWindow_Closed);
}
void MainWindow_Closed(object sender, EventArgs e)
{
// And close the serial port when we're finished
_serialPort.Close();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// Send the text to the Arduino, byte by byte
if (_serialPort.IsOpen)
{
_serialPort.Write(Message.Text);
// Show anything out there!
var foundMessage = _serialPort.ReadExisting();
MessageBox.Show(foundMessage);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment