![Forenex FES91W Series Скачать руководство пользователя страница 53](http://html1.mh-extra.com/html/forenex/fes91w-series/fes91w-series_user-manual_2318966053.webp)
53
-
4-5.3> Example code
Below is a simple example code to use serial port (
Rs232
) functions in Visual studio 2005.
'The following program is simple code for setting Serial port
Private
Sub
InitSerialPort()
SerialPort1.PortName =
"COM2"
'Set the port for communications
SerialPort1.BaudRate = 115200
'Baudrate
SerialPort1.DataBits = 8
'Data Bits
SerialPort1.Parity = 0
'Parity
SerialPort1.StopBits = 1
'Stop bit
SerialPort1.Open()
'Set Serialport open
End
Sub
'The following program is how to execute serial port send data
SerialPort1.Write(TextBox1.Text)
'Send textbox.text by serialport
Timer1.enable = true
'Setup timer to receive data
'The following program is how to execute serial port receive data
Private
Sub
Timer1_Tick(
ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
Timer1.Tick
Dim
TmpStr
As
String
=
""
Dim
Bytearray()
As
Byte
' Declare a Byte array to receive data
Dim
rcvBytes
As
Integer
' Record the number of bytes received
Do
'Get the number of Byte from serial port buffer
rcvBytes = SerialPort1.BytesToRead
'Set size of Byte array
ReDim
Bytearray (rcvBytes - 1)
'Write Byte to Bytearray() array
Me
.SerialPort1.Read(Bytearray, 0, rcvBytes)
'Convert Byte array to a string
TmpStr = Encoding.Default.GetString(Bytearray, 0, rcvBytes)
'Display on textbox
TxtTerminal.Text &= TmpStr
Loop
While
rcvBytes <> 0
End
Sub
● More serial port information in website MSDN.
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.portname%28v=VS.80%29.aspx