I. Overview
Visual Basic is one of the strong series of development software released by Mcrosoft, in addition, it is widely used, convenient, fast, and has a short development cycle. With more and more powerful functions, it is widely used by programmers, it seems that some professional programmers have abandoned Visaul C ++ and switched to RAD programming tools such as Visaul Basic, although Visaul C ++ is incomparable to Visaul Basic in terms of flexibility, compact code, fast running speed, and underlying development, however, writing a C ++ application requires a long debugging and development cycle, which is not suitable for today's application development environment.
Visaul Basic provides a large number of controls (or control and components) for programmers to use, you can easily use the attributes, methods, and languages of these components to develop applications in an event-driven manner. You can also use tools such as the application interfaces in the windows sdk APIs to develop applications. VB also comes with a control development software package CDK, which can be used to develop the Controls you need. In communication problems, we can use the communication control provided by VB or call the windows api communication function.
Ii. VB Serial Communication
There are two main methods to develop communication programs using VB. One is to use the CONTRALS provided by VB, and the other is to use the windows api application interface, WINDOWS APIs mainly provide three dynamic connection libraries (KERNEL. EXE, USER. EXE, GDI.. EXE mainly includes some underlying operation functions to complete some resource management, tasks, memory and other operations, USER. EXE contains some functions related to WINDOWS Management, such as communication, menus, messages, cursors, inserts, timers, and most non-display functions. EXE graphical device interface library, mainly used for functions related to device output. Functions related to serial communication include BuilidCommDCB, ClearCommBreak, SetCommBreak, FlushComm, GetCommError, GetCommState, WriteComm, ReadComm, SetCommState, and CloseComm, all of which are in the \ Windows \ system subdirectory. in the EXE dynamic connection library, you should declare API communication functions and define constants in the global variable definition before calling VB.
In our practice, it is more convenient and convenient to use the VB control to implement communication than to call the sdk api to dynamically connect to the library, and the same function can be implemented with less code, this is the advantage of using the VB Control for communication. The following describes the problem of using the VB Control for wireless communication.
In VB control toolbox, it provides a very convenient serial communication control MSComm, it provides all the detailed rules for the upper-Layer Development of Serial Communication Using RS-232, it supports both query and event-driven serial communication.
The MSCOMM control provides a series of programming elements, including attributes, events, and functions. Using these elements, you can program almost all the serial communication functions. In VB, over 30 attributes, one event, and two functions are provided, among them, the main attributes are comatrix, Settings, PortOpen, InBufferSize, OutBufferSize, InBufferCount, OutBufferCount, Break, InputLen, delimiter, timeout, RTSEnable, CommEvent, ParityReplace, NullDiscard.
The control MSComm provides an event OnComm, which can intercept any messages from the serial port and transfer them to the event handler. In WINDOWS, the operating mechanism is event-driven. In VB programming, event-driven methods are also one of the main ways for software to run, when no event occurs, the program may be in a loop, wait, or task state. when an event occurs, the program is transferred to the event handler. Each control has some events for programmers. The OnComm events in the MSCOMM control are unique, and OnComm can obtain the serial port events and error messages that occur during communication, when a serial port event or error occurs, VB will immediately trigger an OnComm event and the program will be automatically transferred to the OnComm event handler. The CommEvent attribute is an indicator of the OnComm event. This attribute cannot be used during design and is read-only when the program is running. The CommEvent attribute contains the latest event or wrong numeric code, you can read the CommEvent attribute value in the program to understand the communication status. The OnComm event is closely related to the CommEvent attribute and used together. When any OnComm event or error occurs, this will change the CommEvent attribute value. In the OnComm event processing process, you can determine the CommEvent attribute value. For different attribute values to be transferred to different event processing processes, the general method is to select case ....... End select. Because no wired MODEM is used in wireless communication, the property value code of the wired MODEM involved in the CommEvent attribute is irrelevant to this project.
Iii. Wireless Transmission interfaces and protocols
In our project, we use Intel 586/120 PC as the host computer, and the communication program is developed using VB. We use 8031 single-chip microcomputer as the CPU, and AD574 as the data collection tool, the upper computer is used as the data receiving and data processing center station. After the lower computer collects data in real time, it performs simple data Average calculation. After receiving the instructions sent by the upper computer, it starts to send data to the upper computer.
The upper computer wireless communication interface is a wireless MODEM ZX-02 inserted in the ISA expansion slot, wireless MODEM and KENWOOD TK-378 Wireless Walkie Talkie connected, digital signal through wireless MODEM modulation into the audio signal, MIC port sent to TK-378 Wireless Walkie Talkie.
The lower computer has an external wireless MODEM ZX-01, the digital signal of the microcontroller is sent to the wireless MODEM through the serial port, the MODEM is modulated to the signal and sent to the KENWOOD TK-378 Wireless Walkie talkie, the way to receive data is the same as the above, after the signal is received by the TK-378, the audio signal is demodulated into a digital signal by the wireless MODEM into the computer or MCU for processing.
In our communication network, we are not a point-to-point communication, but a one-to-point broadcast communication method. Therefore, we have added a site identification code in the communication protocol, each station has its own specific identification code. The code assigned to the lower computer ensures the orderliness of network communication.
Since wireless communication may have dry noise interference in space, we have adopted a number of anti-interference measures. The first is the Baotou identification code. After sending the transmission command, the lower computer starts to transmit data in the form of a package. Each package has a packet header and a packet tail identifier. If the identifier is incorrect, this transmission will result in abnormal data processing. Another reason for packaging and sending is that the TK-270 walkie talkie can send data continuously for less than one minute, more than one minute will automatically interrupt the sending, therefore, when a large amount of data is not packaged, the continuous sending time will exceed one minute, and the sending data is interrupted.
There is a 32 kb nvram in the lower computer. You can save 32 KB of data. This data can be clearly cleared by the upper computer. When the 32 KB is full, the earliest data to be put will be lost, because our sampling rate is not high, we can only store the data in two days in the case of slow sampling. In this way, no matter the host computer or the lower computer encounters problems such as power failure or crash, the data will not be lost.
During the sending process, data transmission errors occur due to interference. The upper computer does not send clear commands to the lower computer. The data is stored in NVRAM. After the upper computer sends the transmission command, the data will be re-sent to the host computer, which can avoid data loss caused by the line,
The codes sent from the lower computer to the upper computer include ASCII code and BCD code. The BCD code is used in the measurement data to save resources. The commands sent from the upper computer to the lower computer are all ASCII codes.
We also specify the field length and package length, the number of bytes of a field, and the number of fields in a package. If the data is not enough, it is also sent as a half package according to the format of the entire field.
Considering the dispersibility of the lower computer, it may be fixed in a remote area and should not be close to each other. Use the upper computer to send the command at the lower computer's school and the method at the school time to unify the network time.
Due to the limitations of the wireless MODEM we use, the transmission rate can only reach BPS, while in our use, the transfer BPS, n parity, 8 DATA, and 1 STOP are relatively stable.
Commands sent by the upper computer to the lower computer include the command at school and during school, MEMORY command, Data command, fast command, and slow command. There are two sending methods for sending commands: manual and automatic. The automatic method is completed by the timer.
In the wireless communication process, in addition to the provision of reasonable Protocols, in order to ensure the correctness of the communication, it is necessary to increase the latency during data transmission, when a slow computer sends data to a fast computer, the latency should be increased appropriately.
4. Application Instances
Because the software source code of this project is long, we only come up with program Fragments Related to serial communication for your reference. In our work, we have implemented three communication methods: Query, event-driven, and event-driven to query. These three methods are advantageous, and the query methods are convenient and reliable, you can use the Protocol or set the clock to enter and exit the query status, but it is not an effective way to use resources. The event trigger method is very effective for fixed-length communication, but fixed-length communication is not practical in some scenarios, the event-driven query-to-query method has both the event-driven characteristics and the feature of the query-to-query method. It can be said that it is a collection of the two strengths to effectively use resources. The following describes how to convert an event-driven query method.
First, define the ONCOMM-related parameters in the public module:
Global Const MSCOMM_EV_RECEIVE = 2' receives Rthreshold characters. This event will continue to be generated until data is deleted from the receiving buffer using the Input attribute.
Global Const MSCOMM_ER_RXOVER = 1008 'Accept Buffer Overflow. The receiving buffer has no space.
Global Const MSCOMM_ER_TXFULL = 1010 'the transmission buffer is full. The transmission buffer is full when characters are being transferred.
Initialize the serial port and input/output buffer during startup:
Sub Form_Load ()
Comm1.Settings = "1200, n, 8," sets the baud rate of 1200bps, no verification, 8-Bit Data bit, 1-bit stop bit
Comm1.commp ORT = 1 Serial Port 1
Comm1.InputLen = 1 read one character from the input buffer at a time
Comm1.InBufferSize = 512 defines the input buffer as 512 bytes (bytes)
Comm1.InBufferCount = 0 clear the input buffer
Comm1.OutBufferCount = 0 clear the output buffer
Comm1.PortOpen = True start the serial port
End Sub
The following is a sub-process for sending data transmission commands, which is controlled by a Timer:
Sub sample_data ()
Comm1.RTSEnable = True: Set the PTT of the Modem to high and enable the walkie talkie.
Call time_delay appropriate latency
Comm1.Output = "* TRNS" + Chr $ (13) send command
Do this loop is used to check whether all commands have been sent
Loop Until comm1.OutBufferCount = 0
Call time_delay appropriate latency
Comm1.RTSEnable = False: Set PTT of the Modem to low and change the walkie talkie to accept.
Comm1.InBufferCount = 0 clear the receiving buffer
Comm1.Rthreshold = 1 Set Rthreshold = 1 and wait for the OnComm event to start.
End Sub
Write the receiving and processing code in OnComm:
Sub Comm1_OnComm ()
Select Case comm1.CommEvent
Case MSCOMM_ER_RXOVER receives Buffer Overflow. Corresponding code can be inserted.
Case MSCOMM_ER_TXFULL transmission buffer is full. Pluggable processing code
Case MSCOMM_ER_RECEIVE receives 1 character, which can be processed as follows:
Comm1.Rthreshold = 0 no longer responds to the OnComm event. It is received in the query mode.
Ii = 0
Iii = 0: sinn = Chr $ (42): sinn1 = Chr $ (42) initialization variable
Do
Duration = Timer +. 2 sets the time-out exit value.
Iii = iii + 1
Sinn1 = sinn
Do this loop to determine whether the input buffer has data or whether it has timed out
''Dummy % = DoEvents ()
Loop Until comm1.InBufferCount> = 1 Or Timer> = duration
If Timer> = duration Then
Iii = iii-1
Overtime = True: timed out. logout is not normal.
Exit Do
End If
Sinn = comm1.Input Exits normally and the value is paid to sinn.
If header_er = 0 Then
GoTo test_header
End If
If Asc (sinn)> 175 Or Asc (sinn) <160 Then determines whether the Baotou is correct
Iii = 0
The GoTo station_number_er header is incorrect. The data is lost.
End If
Test_header: header_er = 0. The header is correct. The header is accepted. header_er = 0.
Ssin (iii) = sinn ''---- put date into string dimantion
Station_number = Asc (ssin (1 ))
Package_number = Asc (ssin (2 ))
Sentence_number = Asc (ssin (3 ))
Last_character = Asc (ssin (iii ))
Station_number_er: Loop Until sinn1 = Chr $ (10) and sinn = Chr $ (13) exit after receiving the correct package tail 0A and 0D
If overtime = True Then, exit abnormally If timeout occurs.
Overtime = False
Comm1.Rthreshold = 1 restart the OnComm event and wait for receiving the next package
Timer6_wait.Enabled = True
GoTo endsub
End If
The following are data processing programs that receive correct data, including decoding and calculation.
If package_number = 1 Then If the received data is correct and is the last packet
Timer2_sample.Enabled = True start the timer of the control data transmission command
Comm1.RTSEnable = True set PTT to high
Comm1.OutBufferCount = 0 clear the output buffer
Call time_delay appropriate latency
Comm1.Output = "* MACK" + Chr $ (13) Send memory clearing command
Do check whether sending is complete
Loop Until comm1.OutBufferCount = 0
Call time_delay appropriate latency
Comm1.RTSEnable = False set PTT to low
End If
If package_number> 1 Then is not the last package
Comm1.Rthreshold = 1 start OnComm to prepare to receive the next package
Timer6_wait.Enabled = True
End If
Endsub: header_er = 1: comm1.InBufferCount = 0 ''-- empty inbuffer protect from bed data on the inbuffer
End Select
End Sub
V. Conclusion
Based on our practical experience, the main difference between wireless communication and wired communication in this project is that the RTSEnable attribute of the MSCOMM control is used to perform PTT operations on the wireless Modem and the appropriate latency, the delay length is related to the computer used. In the development process, it is vital to grasp the above two items, set a reasonable communication protocol, and select the appropriate hardware.