Use VB to debug serial communication

zhaozj2021-02-16  52

One existing electronic scale, using serial ports to communicate with the computer. Write a VB program to access the serial port to reach the data displayed on the electronic scale. The electronic scale is a BE01 type meter, which is output as an RS-232C standard interface. The baud rate is 300-9600, even check, 7 data bits, 2 stop bits. All characters are sent 11 ASCII codes, one start bit. In VB, you need to introduce control MSCOMM serial communication controls (in Microsoft Comm Control 6.0). The specific procedures are as follows: Control abbreviation: 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.inputbuffers DIM I as INTEGER, J AS INTEGER' with a variable, calculating cycle

*********************************************************** *********************************** Private Sub Form_Load () Cleartext with msc .commport = 1 'Sets COM1 is the communication port .Settings = "9600, E, 7, 2 "Set the communication port parameter 9600 Hz, EtOAc, 7 data bits, 1 stop bit. (Here you need further: .SETTING =" BBBB, P, D, S ".

Meaning is: B: Baud Rate; P: Parity (); d: data bit; s: stop bit) .INBUFFERSIZE = 40 'Set buffer reception data is 40 bytes

. INPUTLEN = 1 'Setting INPUT once from the reception buffer reading by 1 .rthreshold = 1' Settings to receive one byte to generate an 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 with Msc .InputMode = CominputModeBinary 'Setting Data Receive Mode is binary

.INBUFFERCOUNT = 0 'Clear Receive Buffer IF Not .Portopen Then .Portopen = True' Open Communication Port End If End With End Sub Private Sub MSC_ONCOMM () DelayTime 'Used to Continue Cleartext With MSC SELECT CASE .commevent' Judgment Communication Event Case Comevreceive: 'Received the Receive Events Generated by Rthreshold Swichvar 1 if Out (1) = 2 TEN' Judging whether the start flag of the data.Rthreshold = 0 'Close the ONCOMM event Receive end if do do do doects loop unsinbuffercount> = 3 'cycle waiting to 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 SPREVIOS = Timer (Timer can calculate the number of seconds passing from the child to now, in Microsoft Windows, the Timer function can return a second decister Part) Do WHILE BDT IF TIMER - SPREVIOUS> = 0.3 True End Sub (Communication Transmission Rate is 9600bps, the fastest speed 1.04ms sends a byte, the meter sends 50 frames per second, each frame The data has 4 bytes, which is transmitted per second. Average 5.0ms sends a byte, and adds a loop wait program in the program to add a cyclic wait program when serial port data. Private Sub Swichvar DelayTime Var = Null var = msc.input OUT (NNUM) = var (0) End Sub (Setting the received data mode is used in binary form, "iNPutMode = CominputModeBinary, but when you read data with the input property, you cannot directly assign the value to the BYTE type variable, you can only assign a VARIANT first Type variables, return an array of binary data, and then save to the Byte type 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 (50 frame data per second, the microcomputer receives a frame The full data requires at least 20 ms of time, and then performs data processing. If the microcomputer can complete the data calculation processing in 20ms before the next frame data is received, only one frame data will only be saved within the receiving buffer, and there is no two frames. The above data, the size of the receiving buffer does not affect the real-time monitoring effect (receive buffer> 4 bytes), it can fully realize real-time monitoring or real-time control; if the microcomputer does not have data calculation processing within 20ms, receive buffers It is set to be large. Before the data calculation processing is completed, there will be two frames of data in the receiving buffer, and the longer the working time is, the more data frames in the buffer, the more data acquisition and data processing. Produce an additional amount of additional time difference, when the reception buffer is full, the time difference is no longer increased, fixed in a certain value, some data cannot be collected in time, data is lost, and the data is lost, and the real work will be The microcomputer processing result produces a large time difference, is very disadvantageous to real-time monitoring and real-time control, and in this case, the size of the receiving buffer will affect the real-time monitoring effect, so the receive buffer setting cannot be too large to ensure the real-time performance of data processing. Summary: The instrument used herein is the BE01 electronic scale produced by METTLER, and each encoding is a standard ASCII code. Other instruments include BCD compression code in the encoding, and are divided into high low positions, and need to be decoded after receiving, and then add high and low digits, ie, can be converted into real numbers. There is also a possible error: Judgment the maximum, the instrument is interference when it is started, it will transmit some garbled, the displacement sensor has the parameter deviation, and the maximum value is generally greater than 50 mm, so take 51 is the limit maximum, take - 51 is the minimum limit. Temporarily write these, of course, other cases can be pushed!

转载请注明原文地址:https://www.9cbs.com/read-26513.html

New Post(0)