28.12.2019

Read Serial Port From Rs232 Continuously

  1. Rs232 Port On Computer
  2. Read Serial Port From Rs232 Continuously Monitor

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. 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. 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.

SerialFrom

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’. If you wanted to read the actual numeric value you should use readByte or readChar since they return integer values which are not decoded. 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. Writing to a port is incredibly easy! Here are the write methods that you can use:Write(String data)WriteLine(String data)Write(byte data, int offset, int length)Write(char data, int offset, int length)The first two methods are almost identical, except that WriteLine writes a newline character(‘n’) after writing the data.

The other two write methods are also similar; the only difference is the data type of the data to send. To use them, you provide an array of bytes or characters which will be written to the serial port.

The offset parameter just specifies what element of the array to start at i.e. If you pass it 0, it will start at the very beginning of the array; if you pass 1 it will start at the second element. The length parameter is simply the length of the array. Remember to perform these write operations within a try-catch block because they readily throw errors.Technical Note.Remember encoding from step 2? The same concept is applied to Write and WriteLine. The call WriteLine(“Hi”) writes 0x41, 0x61, 0x0A (0x0A is the added ‘n’ since we used WriteLine). If we want to recognize them as characters on the hardware side you must have your own decoding logic present there.

Read

Rs232 Port On Computer

Thanks very much for responding, I wasn't sure how old this post was. But yes, the meat & potatoes of the program relies on listening to these com ports but it has to listen to all of them at the same time which I guess would be using the event handler. Since the ports most likely would be different on other systems using this program, I have a separate program that reads and writes configuration data to an SQL database. Once the main program is launched, it reads in the configuration info of the com ports from the sql database. These com ports are connected to several 3rd party databases, so I need to send heartbeats to each one at time intervals and expect to receive an acknowledgement back to make sure the far end is listening. I have a server connected to the port that would send a telephone number to this program, the program would look at the number, compare it against a list to determine which 3rd party database it belongs to and then send it out that comm port. The 3rd party database at the far end would then send that customers address information to the program, the data would be rearranged, and then sent back to the server requesting it.

Read Serial Port From Rs232 Continuously Monitor

(in a nutshell) We've used software like this in the past but now with the XP & server 2003 going away we need to write our own, I've designed stuff in VB6 before but never used the MSComm so I'm sort of a newbie to all of this. I'll be doing it in.net though so I've got my hands full learning the intricacies. Sorry to talk your ear off, have a nice night -Steve (I tried to upload a diagram don't know if it took).