Visual Basic Serial Communication debugging method

Source: Internet
Author: User
Tags integer
Visual existing electronic scale, using a serial port to communicate with the computer. Write VB program to access the serial port, to read the data displayed on the electronic scale. The electronic scale is BE01 type instrument, output is rs-232c standard interface, baud rate is 300-9600, parity, 7 data bits, 2 stop bit. All characters are sent 11-bit ASCII, a starting bit. In VB and serial communication needs to introduce control MSComm serial communication controls (in Microsoft Comm Control 6.0). The specific procedure is as follows: Control abbreviation: MSC

Dim out (a) as Byte receives the value in Var
Dim var as Variant ' receives values in Msc.input
Dim nrece as Integer ' calculates the number of Msc.inputbuffer
Dim I As Integer, j As Integer ' immediately variable, COMPUTE loop

****************************************************************************

Private Sub Form_Load ()
Cleartext
With MSC
. Commport = 1 ' Set Com1 as communication port
. Settings = "9600,e,7,2" Sets the communication port parameters 9600 Hertz, even parity, 7 data bits, and one stop bit. What needs to be further explained here is:. setting= "Bbbb,p,d,s".
Meaning: B:baud Rate (baud rate); p:parity (parity); D:data bit;s:stop Bit)

. Inbuffersize = 40 ' Set buffer receive data to 40 bytes
. Inputlen = 1 ' Set input to read bytes from the receive buffer at a time of 1
. RThreshold = 1 ' Set to receive a byte generates OnComm event

End With

End Sub

****************************************************************************

Private Sub cleartext ()
Text3.text = ""
Text2.text = "5"
Text1.Text = ""
End Sub

Private Sub Command1_Click ()
Cleartext
' nrece = 0 ' counter clear Zero
With MSC
. Inputmode = Cominputmodebinary ' Set data reception mode to binary form
. InBufferCount = 0 ' clears the receive buffer
If not. PortOpen Then
. PortOpen = True ' Open communication port
End If
End With
End Sub

Private Sub Msc_oncomm ()
Delaytime ' used to prolong time
Cleartext
With MSC
Select case. CommEvent ' Judge communication events
Case comevreceive: ' Receive events generated by RThreshold bytes
Swichvar 1
If out (1) = 2 Then ' to determine whether the start of the data marker
. RThreshold = 0 ' off OnComm event receive
End If
Todo
DoEvents
Loop Until. InBufferCount >= 3 ' Loop waiting for receive buffer >=3 bytes
' nrece = nrece + 1
For i = 2 to 12
Swichvar I
Text1.Text = Text1.Text & Chr (Out (i))
Next
Text1.Text = LTrim (Text1.Text)
Text2.text = Text2.text & CStr (nrece)
. RThreshold = 1 ' Open MSComm event receive
Case Else
' . PortOpen = False
End Select
End With

End Sub

****************************************************************************

Private Sub Delaytime ()

Dim BDT as Boolean
Dim sprevious as single, slast as single

BDT = True

sprevious = Timer (timer can calculate the number of seconds elapsed from midnight to now, in Microsoft Windows, the Timer function can return a fraction of a second)

Do While BDT
If timer-sprevious >= 0.3 Then BDT = False
Loop
BDT = True

End Sub

(communication transmission rate of 9600bps, the fastest speed 1.04ms send a byte, the meter sent 50 frames per second, each frame data has 4 bytes, that is, send 200 bytes per second, the average 5.0ms send a byte, continuous reading serial data in the program to add a loop waiting program)

Private Sub Swichvar (ByVal nnum as Integer)

Delaytime
var = Null
var = MSC. Input
Out (Nnum) = var (0)

End Sub

(The set receive data mode is in binary form, that is, inputmode=cominputmodebinary, but when reading data with the input property, it cannot be assigned directly to the Byte type variable, only by first assigning to a VARIANT type variable. Returns an array of binary data, which is then converted to a byte-type number variable. )

Private Sub Text1_change ()

Text3.text = Ctext (text1.text)-Ctext (Text2.text)

End Sub

****************************************************************************

Private Function Ctext (ByVal str as String) as Currency

If str <> "" Then
Ctext = CCur (Val (str))
Else
Ctext = 0
End If

End Function

(The meter sends 50 frames per second, and the microcomputer receives a full frame of data for at least Ms time and then data processing.) If the computer in the next frame of data received before 20ms to calculate the data processing, then only one frame of data will be stored in the receive buffer, no more than two frames of data, the size of the receive buffer will not affect the real-time monitoring effect (receive buffer >4 bytes), then real-time monitoring or real-time control can be realized If the computer can not be completed in 20MS data processing, the receiver buffer set is very large, in the data processing completed, the receiving buffer will be stored within two frames of data, and the longer the work time, the buffer zone in the number of trapped data frames, Data acquisition and data processing has gradually increased the additional time difference, when the receiving buffer is full, the time difference no longer increases, fixed in a certain value, some data due to not be collected in a timely manner in the receiving buffer, data loss phenomenon, real work will be the result of computer processing and a larger time lag, For real-time monitoring and real-time control is very disadvantageous, in this case the size of the receiving buffer will affect the real-time monitoring effect, so receive buffer settings can not be too large to ensure real-time data processing. Summary: The instrument used in this paper is the BE01 type electronic scale produced by Mettler company, and the output of each encoding is standard ASCII code. Other instruments exist in the launch of the Code contains BCD code, and is divided into high and low, need to be received after decoding the conversion, and then add the upper and lower digits, that is, it can convert the BCD code into real numbers. There is also the possibility of error: To determine the maximum value, the instrument in the beginning of the work of the interference, will conduct some garbled, displacement sensors have parameter deviations, the maximum value is generally slightly greater than 50 mm, so take 51 as the limit maximum, take 51 as the limit of the minimum value. Write these for the time being, of course other things can be and so on!



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.