Visual C Serial Port Programming

Serial ports provide an easy way to communicate between many types of hardware and your computer. They are relatively simple to use and are very common among peripherals and especially DIY projects. Many platforms such as Arduino have built in serial communication so they are really easy to set up and use. How To Install A Breadboard Arduino. Goldendict For Android. Many times you may want your project to communicate with your computer in order to have a cool interactive output, a neat sensor that passes data to your computer, or anything else you could possibly dream up. In this tutorial, I will walk you through how to interface to a serial port on the computer side of things, using Microsoft's.

Net framework. The code examples in this tutorial are in C#, but can be easily transferred to Visual Basic, or Visual C++. This tutorial assumes that you have a very basic understanding of object oriented programing, and whatever language you choose to program in. Since we are mainly going to be using the System.IO.Ports.SerialPort class, is a link to the full documentation by MSDN if you want to check out the rest of the class. I also found a explaining how to fix several common bugs relating to serial ports. Check it out if you get stuck with any odd errors.

Represents a serial port. Initializes a new instance of the SerialPort class using the specified port. // Use this code inside a project created with the Visual.

Feel free to post questions or feedback! I am always happy to hear constructive comments so I can make improvements. Now that we have created our serial port object and opened the port, we now want to read from the serial port.

Here are the basic read functions: (there are several other, but these are the simplest and will work for most applications) int readChar() - returns the next char from the input buffer int readByte() – returns the next byte from the input buffer string readLine() – returns everything up to the newline character (‘ n’) in the input buffer string readExisting() – returns everything in the input buffer It should be noted that readChar() and readByte() both return integers not chars and bytes respectively. In order to convert them to their corresponding types, you will need to typecast them into their respective types: char nextChar = (char)mySerialPort.readChar(); byte nextByte = (byte)mySerialPort.readByte(); The other two methods are pretty self-explanatory. In the next step I'm going to go a little more in depth about how we would go about reading from a port. *Technical note* It is worth noting that both ReadLine(), ReadExisting() return a string based off of decoded bytes from the input buffer. What does that mean? It means that for example if we received the bytes 0x48, 0x69, and 0x0A those would be decoded based off of the ASCII encoding to ‘H’, ‘I’, and ‘ n’. This is significant because if we wanted our hardware to send the numeric value of 65 (0x41), and we used ReadExisting() and printed the return value to a console window we would get an output of “A” not “65” because it decoded 0x41 and changed it to ‘A’.

See More On MSDNVisual C# Serial Port Tutorial