Serial Port C#

Pre-requirements: Microsoft Visual Studio C#(Any Version)

To control the serial port in C# you must first open a connection to an available port. Of course before you do this though you must include the proper files located in System.IO.Ports

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System.IO.Ports
 
namespace Test
{
     class SerialPortTest
     {
          public SerialPortTest()
          {
               // com port("COM1") , baud rate, parity bit, number of bits for data , stop bits 
               SerialPort serialPort = new SerialPort(comPort,9600,Parity.None, 8, StopBits.One);
               serialPort.open();
 
               byte[] temp = new byte[1];
               temp[0] = 0;
               //the data in a byte array to write,starting index in the array, the size of the array in bytes
               serialPort.Write(temp,0,1);
          }
     }
}

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

*
What is 7 + 6 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human)

WordPress Themes