Last active
April 20, 2018 02:22
-
-
Save Freundschaft/b7c3312a384b2465afd8bab249fd614b to your computer and use it in GitHub Desktop.
serial
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.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