NOTE3 : The entire project can be downloaded here. ZIP 98K. The Chr 13 is required by the ADR to tell it a command has been received. The entire program code is shown in Figure 3 and Figure 4. The code is heavily commented and a few explanations are provided below each figure. This value can be increased if desired. When the button is pressed, the value in the combobox is used to open the selected COM port and give it a label of ADRport. This is required by the ADR and depending on your hardware, initialization may or may not be required.
Finally block allows the application to close the serial port even if it generates an exception. All code that manipulates the serial port should appear within this block. Use the ReadLine method to read the next available line of text from the serial port. Use an If statement to determine if the ReadLine method returns Nothing which means no more text is available. If it does return Nothing , exit the Do loop.
Add an Else block to the If statement to handle the case if the string is actually read. The block appends the string from the serial port to the return string. This code example is also available as an IntelliSense code snippet. In the code snippet picker, it is located in Connectivity and Networking.
Configuring the SerialPort. Reading and Writing into SerialPort. Open method. Close method. Writing into Serial Port. Here is a short snippet of code for writing into serial port.
The full Source file " SerialCommWrite. Here is a screenshot of " SerialCommWrite. Reading from Serial Port. Here is a short snippet of code for Reading from SerialPort. Here we define a string variable called DataReceived for storing the data send from the microcontroller to the PC. The received data is then stored in DataReceived.
Please remember to add a new line character to the text string transmitted by embedded system side. The full Source file " SerialCommRead. Close ' Close port. Here is a screenshot of " SerialCommRead. Data received by the PC. Hardware Connections. I have interfaced a microcontroller board MSPG on Launch Pad to the serial port using a null modem cable like this. The Using block allows the application to close the serial port even if it generates an exception.
All code that manipulates the serial port should appear within this block or within a Try Finally block. The WriteLine method sends the data to the serial port. This example assumes the computer is using COM1 ; for more flexibility, the code should allow the user to select the desired serial port from a list of available ports. This example uses a Using block to make sure that the application closes the port even if it throws an exception.
0コメント