The SerialPort class supports multiple encodings other than the default ASCII through the SerialPort.Encoding property; there is plenty of information about that in the link in the intro.They are relatively simple to use and are very common among peripherals and especially DIY projects.
Lua Read Write Serial Port Serial Communication SoMany platforms such as Arduino have built in serial communication so they are really easy to set up and use.
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 Microsofts. 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, HERE is a link to the full documentation by MSDN if you want to check out the rest of the class. I also found a great article explaining how to fix several common bugs relating to serial ports. Lua Read Write Serial Port Free To PostFeel free to post questions or feedback I am always happy to hear constructive comments so I can make improvements. Add Tip Ask Question Comment Download Step 1: Set-up and Open the Serial Port We need to include two namespaces in order to use the SerialPort class: using System.IO.Ports; using System.IO; We now need to instantiate a SerialPort object. There are several constructors to choose from to specify different frame formats but in general the easiest to use is the following: SerialPort(string portName, int baudRate) An example of this would be: SerialPort mySerialPort new SerialPort( COM3, 9600); Here I am using COM3 at 9600 baud. You can find the full list of constructors in the link in the intro. Now that we have created our SerialPort object, we need to open the port using the Open() method. When we are done we will want to close it using the Close() method: mySerialPort.Open(); mySerialPort.Close(); Several notes: when you use any operation that uses a serial port there is a good chance that an error will occur. Lua Read Write Serial Port Code For TheFor this reason we want to write our code for the serial port within a try catch block. This will prevent our program from crashing if, for example we tried to open a port that didnt exist. It is not necessary to instantiate our object within the try catch block, but we want to open, close, read, and write within it. Add Tip Ask Question Comment Download Step 2: Reading From the Port Now that we have created our serial port object and opened the port, we now want to read from the serial port. 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 Im 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |