Skip to content

Instantly share code, notes, and snippets.

@unixunion
Created January 20, 2015 16:04
Show Gist options
  • Save unixunion/45e4d9d3e261d6ef5593 to your computer and use it in GitHub Desktop.
Save unixunion/45e4d9d3e261d6ef5593 to your computer and use it in GitHub Desktop.
Arduino's don't seem to trigger the callback onDataReceived.
using System;
using System.IO.Ports;
using System.Threading;
using System.ComponentModel;
class Program
{
public static void Main()
{
SerialPort mySerialPort = new SerialPort("/dev/tty.usbmodem621", 9600, Parity.None, 8, StopBits.One);
// mySerialPort.BaudRate = 9600;
// mySerialPort.Parity = Parity.None;
// mySerialPort.StopBits = StopBits.One;
// mySerialPort.DataBits = 8;
// mySerialPort.WriteBufferSize = 1;
// mySerialPort.ReadBufferSize = 1;
// mySerialPort.Handshake = Handshake.None;
// mySerialPort.RtsEnable = true;
// mySerialPort.DtrEnable = false;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
// mySerialPort.Disposed += SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.Open();
Console.WriteLine("Press any key to continue...");
Console.WriteLine();
mySerialPort.WriteLine ("a");
System.Threading.Thread.Sleep(500);
mySerialPort.WriteLine ("a");
Console.ReadKey();
mySerialPort.Close();
}
private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine("Data Received:");
Console.Write(indata);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment