Skip to content

Instantly share code, notes, and snippets.

@Freundschaft
Last active April 20, 2018 02:22
Show Gist options
  • Save Freundschaft/b7c3312a384b2465afd8bab249fd614b to your computer and use it in GitHub Desktop.
Save Freundschaft/b7c3312a384b2465afd8bab249fd614b to your computer and use it in GitHub Desktop.
serial
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
namespace ConsoleApplication2
{
class Program
{
private static SerialPort port = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
private static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
byte[] bytes = Encoding.ASCII.GetBytes(indata);
Console.WriteLine(BitConverter.ToString(bytes));
}
static void Main(string[] args)
{
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment