Vs2005 comes with a serial communication control SerialPort, which is similar to the serial MSComm of VB6.0, but there are some differences
1. enable/disable the serial port
VB6.0 serial port is enabled mscomm1.portopen = true, vb2005 directly calls serialport1.open
When the serial port is disabled in VB6.0, mscomm1.portopen = false. If vb2005 is used, serialport1.close is called directly.
2. Comparison of parameter settings
You can set the VB serial port settings separately or through its setting attribute, for example, mscomm1.settings = "9600, N"
The serial port attribute of vb2005 can only be set separately, for example:
Port: serialport1.portname
Baud Rate: serialport1.baudrate
Data bit length: serialport1.databit
Parity: serialport1.parity
Stop bit: serialport1.stopbits
3. output buffer write data to the serial port
VB6.0 writes data to the serial port using the mscomm1.output method, which transmits the starting address of the string or data.
Vb2005 writes data to the serial port through the serialport1.write method. There are three methods:
(1) directly output the string. This method only has one parameter, String, for example: serialport1.write sendstring
Sendstring is a string.
(2). Output in bytes. This method has three parameters:
The first parameter is the starting address of the byte data to be output.
The second parameter starts from the first few bytes of data.
The third parameter indicates the number of bytes to be sent.
For example, serialport1.write (sendbyte, 1st) refers to sending 10th to bytes of the byte array sendbyte to the output buffer.
(3). Output by character. This method also has three parameters:
The first parameter is the starting address of the character data to be output.
The second parameter starts with the character data.
The third parameter indicates the number of characters to be sent.
For example, serialport1.write (sendchar, 1st) refers to sending 10th to characters of the sendbyte of the character array to the output buffer.
4. read data from the serial port input buffer
VB6.0 reads data from the serial port using the mscomm1.input method. The returned data is a string or one-dimensional data.
Vb2005 reads data from the serial port through the serialport1.read method. There are two methods:
(1). Read in bytes. This method has three parameters:
The first parameter buffer is the byte array in which the input is written.
The second parameter offset refers to the offset read from the buffer array. For data read from the beginning, set it to 1.
The third parameter, Count, is the number of bytes to read. If you want to read all data in the current buffer zone, you can use its attribute serialport1.bytestoread as the parameter to pass
Serialport1.bytestoread indicates the number of received data.
For example, serialport1.read (readbyte, 1st) means to read the 10th to bytes of the buffer to the readbyte byte array.
(2). Read in bytes. This method has three parameters:
5. Load all com
Sub getserialportnames ()
For each SP as string in my. Computer. Ports. serialportnames
Combobox1.items. Add (SP)
Next
If combobox1.items. Count> 0 then
Combobox1.selectedindex = 0
End if
End sub
6. Open the serial port
If serialport1.isopen then
Serialport1.close ()
End if
Try
With serialport1
. Portname = combobox1.text
. Baudrate = 9600
. Parity = Io. Ports. Parity. none' parity
. Databits = 8' data bit
. Stopbits = Io. Ports. stopbits. One
End
'Open
Serialport1.open ()
Label1.text = "connected"
Button1.enabled = false
Button2.enabled = true
Catch ex as exception
Msgbox (ex. tostring)
End try
7. Disable the serial port
Serialport1.close ()
8. Send data
General Data
Dim data as string = textbox1.text
Serialport1.write (data)
Hexadecimal string
When sending a hexadecimal string, we use an array to save the information to be sent.
For example, if the data sent is: FF 01 00 00 01 00 fe
Try
Dim data (8) as byte
Data (0) = & HFF
Data (1) = & H1
Data (2) = & H0
Data (3) = & H0
Data (4) = & H1
Data (5) = & H0
Data (6) = & H0
Data (7) = & hfe
Serialport1.discardoutbuffer ()
Serialport1.write (data, 0, 8)
Catch ex as exception
Msgbox (ex. tostring)
End try
9. receive data
SerialPort has a datareceived event, in which we can write and accept the data code.
Accept string: SerialPort. readexisting
Accept stream data:
Dim bytetoread as int16 = SerialPort. bytestoread (read buffer byte length)
Dim CH (bytetoread) as byte
Dim bytesread as int16 = 0
Bytesread = SerialPort. Read (CH, 0, bytetoread)
For I as int16 = 0 to bytesRead-1
Indata = indata & dectohex (CH (I ))
Next
Indata is the data read.
Custom function: dectohex (returns hexadecimal characters)
Public Function dectohex (byval decnumber as byte) as string 'is converted to a hexadecimal string
If decnumber <= 15 then
Dectohex = "0" & hex (decnumber)
Else: dectohex = "" & hex (decnumber)
End if
End Function