-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew Text Document.cs
More file actions
51 lines (51 loc) · 1.32 KB
/
New Text Document.cs
File metadata and controls
51 lines (51 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.IO.Ports;
using System.Diagnostics;
class program
{
//SerialPort sp = null;
static void Main()
{
SerialPort sp = new SerialPort();
UsrSerialPortOpen(sp);
UsrSerialPortSendData(sp,"Hello World");
Console.ReadKey();
}
private static bool UsrSerialPortOpen(SerialPort sp)
{
try
{
sp.PortName = "COM1";
sp.BaudRate = 9600;
sp.DataBits = 8;
sp.StopBits = StopBits.One;
sp.Parity = Parity.None;
sp.Open();
}
catch (Exception exp)
{
Debug.WriteLine(exp);
}
if(sp.IsOpen)
{
sp.DataReceived += new SerialDataReceivedEventHandler(UsrSerialPortRecvData);
}
return sp.IsOpen;
}
private static void UsrSerialPortClose(SerialPort sp)
{
sp.Close();
}
private static void UsrSerialPortSendData(SerialPort sp,String data)
{
sp.Write(data);
}
private static void UsrSerialPortRecvData(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
Console.Clear();
Console.WriteLine(sp.ReadExisting());
}
}