We will introduce the specific steps of C # Serial programming from two aspects, so we should first pay attention to what software is used for serial programming? In addition, we need to pay attention to its installation and environment configuration. Only when these are done in place can we do a good job in the future. The next step is our practical programming, so how can we do every step well? I will share with you the specific content for viewing.. Net/C #Course [1]: intended for beginnersC #BasicTutorial:
C # Serial programming 1. virtual serial port Software
Because there is no serial port hardware on hand, I found a vspm virtual serial port software on the Internet, which is said to be free of charge.
I didn't know how to use it at the beginning. I found out and finally succeeded.
After installation, the working mode should be: vspm runs in server mode, and supports client-mode devices.
Then use the virtual serial port of the Device Detector resume. (If you choose to create the default serial port, the software will automatically help you establish four virtual serial ports) after the virtual serial port is added
Select com2 for the serial port and the IP address of the listener: 192.168.1.111. The client-mode device tries to connect to this port: 8099.
After the device is created, select the serial port, and then select the menu bar: Check the connection of the device. Enter the IP address 192.168.1.111 selected when the device is added. The selected port is 8099.
Click "reconnect". This window does not reflect the following information: But the rightmost column of the serial port in the main window: the last operation: the serial port: com2 is not opened.
C # Serial programming 2.. Net serial port operations
Since the serial port: com2 is not enabled, we need to first open the serial port to connect to it with the virtual software (I did not notice it at first, it took several hours ). Add reference: Microsoft. VisualBasic
Use the following to traverse serialportnames and obtain all serial ports
Microsoft. visualBasic. devices. computer Pc = new Microsoft. visualBasic. devices. computer (); foreach (string s in PC. ports. serialportnames) {This. combobox1.items. add (s );}
Then, the displayed serial port name comname is used to obtain the operation object. You can enable this serial port:
System. Io. Ports. SerialPort COM = new system. Io. Ports. SerialPort (comname); com. open ();
After opening it, you can connect it with the virtual software. Use the following message to send the information, and the virtual software will receive it.
Send data to the serial port:
Com. writeline (this. textbox1.text );
Of course, data can also be accepted:
Com. Readline ()
Here is to receive a line of data, You need to press enter after the virtual software input (that management interface input is, there is no echo, then press Enter ).
Other operations are self-transparent, or refer to the following keywords: The my namespace in VB. NET, how to use the my keyword of VB. NET in C #
Of course, it is disabled:
Com. Close ();
Virtual serial port software and test operations:
The actual steps of C # Serial programming are introduced here. I hope you can understand and be familiar with C # Serial programming. MoreProgramming Language tutorialFor more information, log on to the E-mentor Yiyou network.
Application Instances programmed using C # arrays and serial ports