Visual Basic serial communication program design

Source: Internet
Author: User

1mscomm. vbx communication control description

The mscomm. vbx communication control can be directly added to the form from the toolbox of vb for communication. If the toolbox does not contain this control, use customcontrols of tools to add mscomm. vbx to the toolbox of vb from the windows system subdirectory.

1.1 Communication Mode

Mscomm. vbx has two different methods to process and solve the development and design problems of various communication software.

1. event-driven. It is similar to the window callback function when c/c writes windows software. It is a powerful method to solve problems. In practice, it is often necessary to handle many events related to communication. For example, when the status of line data arrives at the local end or the status of the cd line and the rts signal line changes, it is required that we use corresponding events for tracking and processing. This control is implemented using oncomm events, and it also involves detecting and processing communication errors, the value of commevent returns the latest communication event or wrong numeric code. Communication Control detailed error and event examples include:

Mscomm-er-break receives one breaksignal

Mscomm-er-cdtocd signal timeout

Mscomm-ev-cdcd signal change

2. query method. The program designer is responsible for reading the value of commevent and processing the errors or events. This approach is usually applicable to simple application design.

1.2 communication control attributes

The key to compiling a communication program using communication controls is to accurately understand the setting of communication control attributes. Mscomm. vbx provides 27 communication control attributes, for example:

Comport: set or return the communication port number.

Settings: sets or returns the data communication format in the form of a string: baud rate, verification, data bit, and stop bit.

Portopen: set or return the status of a communication port (including opening or closing one)

3. Instance

The application background of this program is the automatic monitoring system software of dcc95 electrostatic precipitator, which solves the communication problem between one pc Industrial Computer (master station) and 32 single chip microcomputer (slave station. The bus network structure between the main station and the sub-station uses RS-485 communication standards to communicate with each other in question and answer mode. Because 32 sub-stations send communication commands (downstream commands) to the master station, the master station continues to send downstream commands after receiving the corresponding response commands (upstream commands) sent back from the slave station. According to the requirements of the system functions, the main site needs to send two types of commands: (1) the same period command, which is triggered by a timer and sent once every ls cycle; (2) non-periodic commands, which are triggered by the operator clicking the corresponding command button and are not sent cyclically. The automatic monitoring system software is installed on the main station, and the communication program is installed on the main station as part of the automatic monitoring system software.

This document only lists the basic demo programs used for testing when debugging communication programs. During the test, one PC is used as the main station, and the other PC is used to simulate 32 sub-stations. rs232c serial communication is used between the two PCs. Add one communication control, two timer controls, and one command button control to the communication demo form (form) of the main site. The communication control (mscomm1) is used to access the serial port, send and receive data; periodic Timer control (periodic) is used to control periodic commands sent from the master station to each sub-station per second; command button control (nonperiodic-command) and nonperiodic Timer control (nonperiodic) used to send non-periodic commands. Data transmission uses an event-driven communication mode. You can set the rtreshlods Attribute Based on Different sending commands, causing an oncomm event to receive data.

2.1 Form Control initialization program

Set the parameters for serial communication, set the duration of the periodic timer to ls, and the duration of the nonperiodic timer to 0.5 s.

Subform-load ()

Mscomm1.commp ORT = 2' use com2 serial port

Mscomm1.settings = "9600, n8, 1" 'baud rate 9600, no parity bit, 8-Bit Data bit, 1-bit stop bit

Mscomm1.inputlen = 0' input reads all the content in the receiving buffer.

Mscomm1.inbuffersize = 1024 'sets the length of the byte of the receiving buffer.

Mscomm1.portopen = true' open the communication port

Mscomm1.inbuffercount = 0' clear sending buffer data

Mscomm1.outbuffercount = 0' clear Received Buffer data

Periodic. inteval = 100 'sets the ls scheduled interval so that the telemetry Command sends once every ls

Nonperiodic. inteval = 500 ': Set the scheduled interval of seconds. Check whether the command button is active to determine whether to send periodic commands.

Command-pressed = false' command button is inactive

During-periodic = false' periodic command data transmission has not started

During-nonperiodic = false' non-periodic command data transmission has not started

Endsub

2.2 Non-periodic command sending program

Send non-periodic commands in the interrupt program of the nonperiodic timer based on the command button status and the periodic command data transmission status.

Subnonperiodic-command-click ()

Command-pressed = true' command button activated

Endsub

Subnonperiodic-timer ()

Ifduring-periodic = trueorcommand-pressed = false

Thenexitsub: If the periodic command data transmission is not completed or the command button is activated, exit the non-periodic command program.

Command-pressed = false' command button restored to inactive status

Callsenddata (nonperiodic-command) 'sends non-periodic commands

Mscomm1.rthreshold = r-nonperiodic-byte after sending an unperiodic command, set the rthreshold attribute so that the main site receives the set number of bytes and triggers an oncomm event

Endsub

2.3periodic Timer Program

Send periodic commands in the interrupt program of the periodic Timer:

Subperiodic-timer ()

Ifduring-nonperiodic = truethenexitsub exit the non-periodic command program if the non-periodic command data transmission has not ended.

During-periodic = true' set periodic command data transmission status to in progress

Callsenddata (periodic-command) 'sends periodic commands

Mscomm1.rthreshold = r-periodic-byte after sending a periodic command, the master station receives the r-remot-edata-byte bytes, which can cause an oncomm event

Endsub

2.4oncomm event program

Set the value based on the rthreshold attribute. When a byte character is received in the receiving cache, The oncomm event is triggered and data is received in the interrupt program.

Submscomm1-oncomm ()

Selectcasemscomm1.commevent where code for handling various errors or events can be inserted

Casemscomm-ev-receive

Receivestring $ = mscomm1.input

Selectcasemscomm1.rthreshold

Response Data of caser-periodic-byte periodic commands

Calldisposedata (periodic-command) 'processes received data

Duringperiodic = false' set the periodic command data transmission status to end

Response Data of caser-nonperiodic-byte non-periodic commands

Calldisposedata (nonperiodic-command) 'processes received data

During-nonperiodic = false'

Endselect

Endselect

Endsub

With the continuous upgrade of the vb version, vb will become the fastest, easy to use, and powerful application development tool. It is one of the first tools for enterprise-level customer/server application software development.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.