C# serial port program


















Ports SerialPort Examples. Ports SerialPort - 30 examples found. These are the top rated real world C CSharp examples of System. SerialPort extracted from open source projects. You can rate examples to help us improve the quality of examples. Programming Language: C CSharp. Frequently Used Methods. Show Hide. Close ReadExisting DiscardOutBuffer Dispose WriteLine Write ReadLine Open Read ReadByte DiscardInBuffer ReadChar Flush ReadTo 2.

GetListener 1. Realine 1. StartsWith 1. ToString 1. GetClient 1. Related in langs. Decode Go. ParseBaseUrl Go. AndroidRemoteDriver Java. Inheritance: System. SerialPort Class Documentation. Example 1.

Show file. None; break; case FlowControl. DsrDtr: case FlowControl. First thing to do is to add the System. Ports namespace to the top of your code so that you can use the SerialPort Class in your program. You can access the Form1. In my Form,. A " Log " TextBox to Display additional information to the user. Below Figure shows the Name associated with each control on my Form. I didn't want to use the default TextBox1,ComboBox1 naming convention as it was quite confusing. After the form is populated with the required controls,We will change the " FormBorder Style " to " Fixed Single " using the " Properties Window " so that user will not be able to change the size of program window.

You can also change the name of your Form from Form1 to Simple Serial [c ] by changing the " Text " property of Form1 as shown below. We will also disable the " Maximize Button " on the Title Bar. Adding Standard Baudrates. A new Window will open, manually type the baudrates line by line and then press OK.

Detecting Available Serial Ports. After you have decided on the Visual style , name of the properties and controls in your program ,Its time to add the functional part to your Form1 code. The method returns the available serial ports in a string array.

Detecting OS version. This was added just for fun. All these information can be easily obtained using the properties of System. Environment object. The Baudrate Selection combobox will become active only after the user have selected the COM port number and the Serial Transmit ,Serial Receive Groupbox's will get enabled after the user have selected the Baudrate.

Selecting the SerialPort. Now the user is free to Receive or Transmit data at his choosen Baudrate and Port. The Event also has code which prints the choosen serial port and baudrate to the Log TextBox. Transmitting Data. To read from the serial port, we need to create and listen to an event continuously for incoming data. When data is received, it raises the event. In the Connect function after we have opened the serial port, create ComPort.

DataReceived event:. Create the SerialPortDataReceived function that will be called when there is data in the receive buffer:. Data recived from the serial port is coming from another thread context than the UI thread. Instead of reading the content directly in the SerialPortDataReceived, we need to use a delegate. SerialPort Class. Full project code:. Where to? Start building the interface by dragging controls from the ToolBox onto the form as shown below: Drag and drop two GroupBox controls from the ToolBox under the Containers sub-Section.

Change their Text properties from the Properties pane. We use a GroupBox control to group controls, in our case, configuration settings controls. We will use this button to connect or disconnect to selected serial ports. We will use this button to send data typed on the screen to the Serial Port. We set its Enable Properties to false to prevent a user from clicking it before we connect to a port this would generate an error.

We will use this button to clear the screen. This is where we will receive data from the Serial port and any data sent to serial port will be displayed here as well. This is where we are going to type our data and click on Send button to send it to the serial port. None, 8, StopBits. One ;. None , 8 , StopBits. Open ;. Close ;. Write "Your Text To get this functionality, the following lines should be added below the SerialPort declaration:. This will create the objects needed.

The line below should be added to the form declaration below InitializeComponents ; :. I have found it easiest to run this program with a female socket of the end of a 9 way cable. A piece of wire can then be stroked over the pin solder buckets casing the labels to change colour from Green to Red and back , this will give some feeling that you achieving something.

It should be noted at this point the PC will try to open the default serial port which is default is Baud, No Parity, One Stop bit and No flow control, the number is decided by the lowest number serial available Com1 if fitted. All this does is connect Pin 2 of one RS port to Pin 3 of another, and the Pin 2 to Pin 3 of the other end, while this is all that is needed, a connection between the shield grounds is recommended to prevent errors.

Pins 2 and 3 are directly connected Tip : The pin spacing on a standard 9-way D type is just right to all a jumper from the back of an old CD-Rom to be used! To get the demo codes to do this, add a button to the form called PortState and add the code below:. A very good practice to get into is to use the IsOpen property to check to see if the port can be opened. The main use for this I have found is with USB com ports which can be added and removed at will and not always placed back in the same socket an example of this is below:.

This if will check the COM port to see if it is open, the not operator is to check if it is closed. Another useful trick is to use a try … catch as below:. This will cause the software to produce a message when a fault is found and not blow up in the user's face! Also another thing the Serial Port Class provides is the Timeout Property, to prevent the software sitting waiting in a loop, you can do the below:.

The above sets a time limit on actions to the serial port. By default, these are set to infinity but can be set. I have used the Read time out with some radio boards to check they are still in range if the command to get the serial number of board X times out X is not in range. To receive data there are two methods, interrupt using the DataRecieved Event and polling with a timer not recommend but using a stop watch timer from the tool box with a interval property of or 1 second see below for code ….

Generally, this method while it works for a single serial port that does not change quickly it works, however I have been bitten by this with a test rig someone I think I know who!! These are both valid methods, however the DataReceived event is acted upon an interrupt basis using a delegate. To get this function to work, add the below:. This is done with use of the. Write function. It can write a byte array, a character array or subarray or a string. The reading method is ReadExisting method.

This will read text out of the receive buffer until it is empty. This method does not block the port until it gets some data or a timeout is expired.



0コメント

  • 1000 / 1000