Skip to content

Commit f547e51

Browse files
committed
Adding usage
1 parent 27e4906 commit f547e51

File tree

1 file changed

+130
-1
lines changed

1 file changed

+130
-1
lines changed

README.md

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,136 @@
1515

1616
## Usage
1717

18-
TO be added here
18+
You will find detailed examples in the [Tests](./Tests/UnitTestsSerialPort).
19+
20+
### Creating the SerialPort
21+
22+
You can create the `SerialPort` like this:
23+
24+
```csharp
25+
var port = new SerialPort("COM2");
26+
```
27+
28+
Note that the port name **must** be `COMx` where x is a number.
29+
30+
The GetPortNames method will gi you a list of available ports:
31+
32+
```csharp
33+
var ports = SerialPort.GetPortNames();
34+
```
35+
36+
You can as well directly specify the baud rate and other elements in the constructor:
37+
38+
```csharp
39+
var port = new SerialPort("COM2", 115200);
40+
```
41+
42+
Each property can be adjusted, including while the port is open. Be aware that this can generate hazardous behaviors. It is always recommended to change the properties once the port is closed.
43+
44+
**Important**: you should setup a timeout for the read and write operations. If you have none, while operating a read or a write, you will wait indefinitely to read or write that everything is received or sent.
45+
46+
```csharp
47+
port.WriteTimeout = 1000;
48+
port.ReadTimeout = 1000;
49+
```
50+
51+
Note: some MCU do not support Hankshake or specific bit parity even if you can set them up in the constructor.
52+
53+
### Opening and Closing the port
54+
55+
The port can only be in operation once open and will finish his operations when closed. If you dispose the SerialPort, it will close it before.
56+
57+
```csharp
58+
var port = new SerialPort("COM2");
59+
port.Open();
60+
// Do a lot of things here, write, read
61+
port.Close();
62+
```
63+
64+
### Read and Write
65+
66+
You have multiple functions to read and write, some are byte related, others string related. Note that the string one will use the `Enconding` charset that you will define. By default, this is UTF8.
67+
68+
#### Sending and receiving bytes
69+
70+
Example of sending and reading byte arrays:
71+
72+
```csharp
73+
byte[] toSend = new byte[] { 0x42, 0xAA, 0x11, 0x00 };
74+
byte[] toReceive = new byte[50];
75+
// this will send the 4 bytes:
76+
port.Write(toSend, 0, toSend.Length);
77+
// This will only send the bytes AA and 11:
78+
port.Write(toSend, 1, 2);
79+
// This will check then number of available bytes to read
80+
var numBytesToRead = port.BytesToRead;
81+
// This will read 50 characters:
82+
port.Read(toReceive, 0, toReceive.Length);
83+
// this will read 10 characters and place them at the offset position 3:
84+
port.Read(toReceive, 3, 10);
85+
// Note: in case of time out while reading or writing, you will receive a TimeoutException
86+
// And you can as well read a single byte:
87+
byte oneByte = port.ReadByte();
88+
```
89+
90+
#### Sending and receiving string
91+
92+
You can as well write and read strings:
93+
94+
```csharp
95+
string toSend = "I ❤ nanoFramework";
96+
port.WriteLine(toSend);
97+
// this will send the string encoded finishing by a new line, by default \r\n
98+
// You can change the new line by anything:
99+
port.NewLine = "❤❤";
100+
// Now it will send the 2 hearts as the end of line while operating a ReadLine or WriteLine
101+
// You can ad anytime change it back:
102+
port.NewLine = SerialPort.DefaultNewLine; // default is "\r\n"
103+
// This will read the existing buffer:
104+
string existingString = port.ReadExisting();
105+
// Note that if it can't properly convert the bytes to a string, you'll get an exception
106+
// This will read a full line, it has to be terminated by the NewLine string.
107+
// If nothing is found ending by the NewLine in the ReadTimeout time frame, a TimeoutException will be raised.
108+
string aFullLine = port.ReadLine();
109+
```
110+
111+
### Events
112+
113+
SerialPort supports events when characters are received.
114+
115+
```csharp
116+
// Subscribe to the event
117+
port.DataReceived += DataReceivedNormalEvent;
118+
119+
// When you're done, you can as well unsubscribe
120+
port.DataReceived -= DataReceivedNormalEvent;
121+
122+
private void DataReceivedNormalEvent(object sender, SerialDataReceivedEventArgs e)
123+
{
124+
var ser = (SerialPort)sender;
125+
// Now you can check how many characters are available, read a line for example
126+
var numBytesToRead = port.BytesToRead;
127+
string aFullLine = ser.ReadLine();
128+
}
129+
```
130+
131+
#### Case of WatchChar
132+
133+
.NET nanoFramework has a specific API to watch for a specific character if present at the end of the transmission.
134+
135+
```csharp
136+
port.WatchChar = '\r';
137+
// Subscribe to the event
138+
port.DataReceived += DataReceivedNormalEvent;
139+
140+
private void DataReceivedNormalEvent(object sender, SerialDataReceivedEventArgs e)
141+
{
142+
if (e.EventType == SerialData.WatchChar)
143+
{
144+
// We have our special character at the end of the transmission
145+
}
146+
}
147+
```
19148

20149
## Feedback and documentation
21150

0 commit comments

Comments
 (0)