This repository was archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathProgram.cs
More file actions
40 lines (38 loc) · 1.23 KB
/
Program.cs
File metadata and controls
40 lines (38 loc) · 1.23 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
using System;
using System.IO.Ports;
namespace serialapp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Serial port!");
var ports = SerialDevice.GetPortNames();
bool isTTY = false;
foreach (var prt in ports)
{
Console.WriteLine($"Serial name: {prt}");
if (prt.Contains("ttyS0"))
{
isTTY = true;
}
}
if (!isTTY)
{
Console.WriteLine("No ttyS0 serial port!");
return;
}
Console.WriteLine("Yes, we have the embedded serial port available, opening it");
SerialDevice mySer = new SerialDevice("/dev/serial0", BaudRate.B115200);
mySer.DataReceived += MySer_DataReceived;
mySer.Open();
mySer.Write(System.Text.Encoding.UTF8.GetBytes("Hello Serial port!"));
while (!Console.KeyAvailable);
mySer.Close();
}
private static void MySer_DataReceived(object arg1, byte[] arg2)
{
Console.WriteLine($"Received: {System.Text.Encoding.UTF8.GetString(arg2)}");
}
}
}