One existing electronic scale can communicate with a computer through a serial port. Write a vbprogram to access the serial port to read the data displayed on the electronic scale. The electronic scale is BE01 type instrument, the output is the RS-232C standard interface, the baud rate is 300-9600, even verification, 7 data bit, 2 stop bit. All characters are sent with an 11-bit ASCII code and a start bit. MSComm serial communication Control (in Microsoft Comm Control 6.0) must be introduced in VB for serial communication ). The specific procedure is as follows: MSC
Dim Out (12) As Byte receives values 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, then variable, calculation cycle
**************************************** ************************************
Private Sub Form_Load ()
ClearText
With MSC
. Commp ORT = 1 Set Com1 as the communication port
. Settings = "9600, E, 9600" sets the communication port parameter Hz, even verification, 7 data bits, and 1 Stop bits. (Here we need to further explain :. setting = "BBBB, P, D, S ".
Meaning: B: Baud Rate (Baud Rate); P: Parity (Parity); D: Data Bit; S: Stop Bit)
. InBufferSize = 40 set the buffer to receive 40 bytes of data
. InputLen = 1 set the number of bytes read from the receiving buffer for one Input operation to 1.
. RThreshold = 1 set to receive a byte to generate an OnComm event
End
End Sub
**************************************** ************************************
Private Sub ClearText ()
Text3.Text = ""
Text2.Text = "5"
Text1.Text = ""
End Sub
Private Sub commandementclick ()
ClearText
NRece = 0 counters cleared
With MSC
. InputMode = comInputModeBinary sets the data receiving mode to binary.
. InBufferCount = 0 clear the receiving buffer
If Not. PortOpen Then
. PortOpen = True open the communication port
End If
End
End Sub
Private Sub MSC_OnComm ()
Delaytime' is used to extend the time
ClearText
With MSC
Select Case. CommEvent determines communication events
Case comEvReceive: receives the receiving event generated by the Rthreshold byte
SwichVar 1
If Out (1) = 2 Then determines whether it is the start mark of the data
. RThreshold = 0 disable OnComm event reception
End If
Do
DoEvents
Loop Until. InBufferCount> = 3 Loop waiting for receiving 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 enable MSComm event reception
Case Else
. PortOpen = False
End Select
End
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 that have elapsed since midnight. 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
(If the communication transmission rate is 200 bps, one byte is sent at the fastest speed of Ms. The meter sends 50 frames of data per second. Each frame has four bytes, that is, bytes are sent per second, one byte is sent in an average of 5.0 ms. When reading serial data continuously, you must add a cyclic waiting program to the program)
Private Sub SwichVar (ByVal nNum As Integer)
DelayTime
Var = Null
Var = MSC. Input
Out (nNum) = var (0)
End Sub
(Set the receiving data mode to binary, that is, InputMode = comInputModeBinary. However, when reading data using the Input attribute, you cannot directly assign values to Byte variables. You can only assign values to a Variant type variable first, returns an array of binary data and then converts and saves it to the Byte type number variable .)
Private Sub text=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 of data per second. It takes at least 20 ms for the microcomputer to receive a complete frame of data before processing the data. If the microcomputer can complete data computing within 20 ms before receiving the next frame of data, only one frame of data will be stored in the receiving buffer and no more than two frames of data will be stored, the size of the receiving buffer does not affect the real-time monitoring effect (the receiving buffer is greater than 4 bytes). In this case, real-time monitoring or real-time control can be implemented. If the microcomputer cannot complete data computing within 20 ms, the receiving buffer is very large. Before the data computing and processing are completed, the receiving buffer stores more than two frames of data, and the longer the processing time, the more data frames stranded in the buffer, the more time difference between data collection and data processing. When the receiving buffer is full, the time difference does not increase and is fixed to a certain value, because some data cannot be collected to the receiving buffer in a timely manner, data loss may result in a large time difference with the processing results of the microcomputer, which is detrimental to real-time monitoring and real-time control, in this case, the size of the receiving buffer will affect the real-time monitoring effect. Therefore, the setting of the receiving buffer cannot be too large to ensure real-time data processing .) Summary: The instrument used in this article is the BE01 Electronic Scale produced by the company. Each output code is a standard ASCII code. Other instruments contain BCD compression codes, which are divided into high and low bits. After receiving the codes, decode them and add the numbers at the high and low positions, that is, the BCD code can be converted into a real number. There is also a possibility of error: to determine the maximum value, the instrument has interference at the beginning of work, will transmit some garbled, the displacement sensor has a Parameter deviation, the maximum value is generally slightly greater than 50mm, therefore, 51 is the maximum value, and-51 is the minimum value. Write this for the time being. Of course, you can push it in other cases!