Created
February 8, 2013 13:17
-
-
Save jrsconfitto/4738994 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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