In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "VB.NET how to achieve communication programs", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve communication programs in VB.NET" this article.
1 mscomm.vbx communication control description
The mscomm.vbx communication control can be used to communicate by adding the form form directly from the toolbox of vb. If there is no such control in toolbox, use tools's custom controls to add mscomm.vbx from the system subdirectory of windows to the toolbox of vb.
1.1 Communication mode
Mscomm.vbx has two different ways to deal with and solve all kinds of communication software development and design problems.
1) event-driven. It is similar to the window callback function when writing windows software, and is a powerful way to deal with problems. In practical work, we often have to deal with many related events in communication, for example, when the line data arrives at the local end or the state of the CD line and rts signal line changes, we are required to use the corresponding events to track and handle. The control is implemented by using oncomm events, and it also includes problems such as detecting and dealing with communication errors. The commevent value returns the recent communication event or error digital code. Examples of detailed errors and events for communication controls are:
Mscomm-er-break received a break signal mscomm-er-cdto cd signal timeout. Mscomm-ev-cd cd signal changes.
2) query method. The programmer is responsible for reading the value of commevent and handling errors or events that occur. This approach is usually available for simple application design.
1.2 Properties of the communication control
Using communication control to program VB.NET communication program, the key is to accurately understand the properties of setting communication control. Mscomm.vbx provides 27 properties about communication controls, such as:
Commport: sets or returns the communication port number.
Settings: sets or returns the format of data communication as a string: baud rate, parity, data bits, and stop bits.
Portopen: sets or returns the communication port status (including opening and closing 1 communication port)
……
two。 Example
The application background of this program is the software of automatic monitoring system of dcc95 electrostatic precipitator, which solves the communication problem between 1 pc industrial control computer (master station) and 32 single chip computers (sub-stations). The bus network structure between the master station and the sub-station adopts the rs-485 communication standard and carries on the data communication by the way of question and answer. Since 32 substations send communication commands (downlink commands) with the master station, the master station continues to send downlink commands after receiving the corresponding reply commands (uplink commands) sent back by the substations. According to the functional requirements of the system, the master station needs to send two types of commands: (1) simultaneous commands, which are triggered by timers and sent every ls cycle; (2) aperiodic commands, which are caused by the operator pressing the corresponding command button and are sent aperiodic. The automatic monitoring system software is installed on the master station, and the VB.NET communication program is also installed on the master station as a part of the automatic monitoring system software.
This article only lists the list of basic demonstration programs that are used in debugging VB.NET communication programs. In the experiment, one PC is used as the main station, and the other PC simulates the work of 32 sub-stations. Rs232c serial communication is used between the two PCs. A communication control, two timer controls and a command button control are added to the communication demonstration program form (form) of the master station. The communication control (mscomm1) is used to access the serial port, send and receive data; the periodic timer control (periodic) is used to control the master station to send periodic commands to each child station every second; the command button control (nonperiodic-command) and the nonperiodic timer control (nonperiodic) are used to send non-periodic commands. Data transmission adopts event-driven communication mode, and the rtreshlod property is set according to different sending commands, thus causing oncomm events to receive data.
2.1 initialization program for each control of the form
Set the communication serial port working parameters, set the break interval of periodic timer to ls, and the interrupt interval of nonperiodic timer to 0.5s.
Sub form-load () mscomm1.commport=2 'uses com2 serial port mscomm1.settings= "9600 ~ n8 ~ # 1" baud rate 9600, no parity check bit. 8-bit data bit 1-bit stop bit mscomm1.inputlen=0 'input will read the whole contents of the receive buffer mscomm1.inbuffersize=1024' set the byte length of the receive buffer mscomm1.portopen=true 'open the communication port mscomm1.inbuffercount=0' clear the sending buffer data mscomm1.outbuffercount=0 'clear the reception buffer data periodic.inteval=100' set the ls fixed time interval so that the telemetry command sends nonperiodic.inteval=500 'every other ls to set the fixed time interval Query whether the command button is active to determine whether to send periodic commands command-pressed=false 'command buttons are inactive during- periodic=false' periodic command data transfer has not started during- nonperiodic=false 'aperiodic command data transfer has not started end sub
2.2 aperiodic command sender
According to the command button state and the periodic command data transmission state, non-periodic commands are sent in the interrupt program of the nonperiodic timer.
Sub nonperiodic-command-click () command-pressed=true 'command button activates end sub sub nonperiodic-timer () if during- periodic=true or command-pressed=false then exit sub' if periodic command data transfer has not been completed or the command button is active, exit the program to send aperiodic commands. Command-pressed=false 'command button returns to inactive state call senddata (nonperiodic-command)' send aperiodic command mscomm1.rthreshold=r-nonperiodic-byte' sends aperiodic command, set the rthreshold property to cause the master station to receive the set number of bytes and raise the oncomm event end sub
2.3 periodic timer program
Send periodic commands in the interrupt program of the periodic timer:
Sub periodic-timer () if during- nonperiodic=true then exit sub 'exits the program for sending aperiodic commands if the data transfer of aperiodic commands has not been completed. During-periodic=true 'sets the periodic command data transfer status to call senddata (periodic-command)' send periodic command mscomm1.rthreshold=r-periodic-byte 'after sending periodic commands, the master station receives rRRART-edata-byte bytes, which can raise the oncomm event end sub
2.4 oncomm event program
According to the value set by the rthreshold property, when a character of the corresponding byte is received in the receive cache, the oncomm event is raised to receive data in the interrupt program.
Sub mscomm1-oncomm () select case mscomm1.commevent 'can be inserted here to handle a variety of different errors or events code case mscomm-ev-receive receivestring$=mscomm1.input select case mscomm1.rthreshold case r-periodic-byte' periodic command response data call disposedata (periodic-command) 'process received data during periodic=false' set periodic command data transfer status to end case r-nonperiodic-byte 'non-periodic command respondent According to call disposedata (nonperiodic-command) 'processing received data during-nonperiodic=false', set aperiodic command data transfer status to end end select end select end sub and above are all the contents of the article "how to implement a communication program in VB.NET" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.