请教大家一个问题,在vb中能不能定义一个长度位一千的数组,然后按照公式取其中的元素啊?
本帖最后由 bcrun 于 2013-01-19 11:02:18 编辑 请教大家一个问题,在vb中能不能定义一个长度位一千的数组,然后按照公式取其中的元素啊?比如定义:dim ch(1000) as byte
dim a as string
dim b as string
dim c asstring
dim d as string
for i as integer to 249
a=ch(4*i)
b=ch(4*i+1)
c=ch(4*i+2)
d=ch(4*i+3)next
我写的具体代码:
Public Classvb?数据采集?数组定义
Public Delegate Sub myDelegate()
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For i As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1
cmbSerialPort.Items.Add(My.Computer.Ports.SerialPortNames(i))
Next
btnCloseSerialPort.Enabled = False
SerialPort.Close()
Timer.Enabled = False
picState.BackColor = Color.Red
cmbBoadrate.Items.Add("4800")
cmbBoadrate.Items.Add("9600")
cmbBoadrate.Items.Add("57600")
cmbChanel.Items.Add("通道一")
cmbChanel.Items.Add("通道二")
cmbChanel.Items.Add("通道三")
cmbChanel.Items.Add("通道四")
End Sub
Private Sub btnOpenSerialPort_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpenSerialPort.Click
If SerialPort.IsOpen = True Then
SerialPort.Close()
End If
If cmbSerialPort.Text = "" Then
MessageBox.Show("请选择一个串口", "串口选择警告", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
SerialPort.PortName = cmbSerialPort.Text
SerialPort.ReadBufferSize = 4096
SerialPort.BaudRate = cmbBoadrate.Text
SerialPort.Parity = IO.Ports.Parity.None
SerialPort.DataBits = 8
SerialPort.StopBits = IO.Ports.StopBits.One
SerialPort.DiscardNull = True
SerialPort.DtrEnable = False
SerialPort.Encoding = System.Text.Encoding.ASCII
SerialPort.RtsEnable = False
SerialPort.Open()
SerialPort.DiscardOutBuffer()
SerialPort.DiscardInBuffer()
picState.BackColor = Color.Green
btnCloseSerialPort.Enabled = True
End If
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
If MessageBox.Show("清空文本框显示请选择Yes,清零计数器请选择No", "选择一个删除对象", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
txtChanelOne.Text = String.Empty
Else
txtNumOfReceive.Text = "0"
txtNumOfSendChar.Text = "0"
End If
End Sub
Public Sub SendtoSbuf()
Dim TestArray() As String = Split("fe")
'Dim str(0)="fe" as string
Dim hexBytes() As Byte
ReDim hexBytes(TestArray.Length - 1)
Dim i As Integer
For i = 0 To TestArray.Length - 1
hexBytes(i) = Val("&h" & TestArray(0))
Next
SerialPort.Write(hexBytes, 0, 1)
txtNumOfSendChar.Text = (Val(txtNumOfSendChar.Text) + 1).ToString
End Sub
Public Function DecToHex(ByVal DecNumber As Byte) As String '转换成十六进制字符串
If DecNumber <= 15 Then
DecToHex = " 0" & Hex(DecNumber)
Else : DecToHex = " " & Hex(DecNumber)
End If
End Function
Public Sub GetCharFromAndSendToTextBox()
Dim str_indata1 As String
Dim str_indata2 As String
Dim str_indata3 As String
Dim str_indata4 As String
Dim byteToRead As Int16
byteToRead = SerialPort.BytesToRead '(读取缓冲区的字节长度)
txtNumOfReceive.Text = (Val(txtNumOfReceive.Text) + byteToRead).ToString
Dim ch(byteToRead) As Byte
Dim bytesRead As Int16 = 0
' str_indata = SerialPort1.ReadExisting()
bytesRead = SerialPort.Read(ch, 0, byteToRead)
If bytesRead > 0 Then
For i As Int16 = 0 To bytesRead - 1
str_indata1 = str_indata1 & " " & (Val(ch(4 * i)) / 256) * 1250
str_indata2 = str_indata2 & " " & (Val(ch(4 * i + 1)) / 256) * 1250
str_indata3 = str_indata3 & " " & (Val(ch(4 * i + 2)) / 256) * 1250
str_indata4 = str_indata4 & " " & (Val(ch(4 * i + 3)) / 256) * 1250
'System.Threading.Thread.Sleep(50) ' 这个很重要!
'str_indata = str_indata & DecToHex(ch(i))
'str_indata = Val("&h" & str_indata)
Next
End If
With txtChanelOne
.Font = New Font("Garamond", 12.0!, FontStyle.Bold)
.AppendText(str_indata1)
.ScrollToCaret()
End With
With txtChanelTow
.Font = New Font("Garamond", 12.0!, FontStyle.Bold)
.AppendText(str_indata1)
.ScrollToCaret()
End With
With txtChanelThree
.Font = New Font("Garamond", 12.0!, FontStyle.Bold)
.AppendText(str_indata1)
.ScrollToCaret()
End With
With txtChanelFour
.Font = New Font("Garamond", 12.0!, FontStyle.Bold)
.AppendText(str_indata1)
.ScrollToCaret()
End With
End Sub
Private Sub btnStartCollectData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartCollectData.Click
If SerialPort.IsOpen = True Then
SendtoSbuf()
Else : MessageBox.Show("请打开串口", "警告", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End If
End Sub
Private Sub btnCloseSerialPort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCloseSerialPort.Click
SerialPort.Close()
picState.BackColor = Color.Red
End Sub
Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
txtChanelOne.Invoke(New myDelegate(AddressOf GetCharFromAndSendToTextBox), New Object() {})
End Sub
Private Sub btnCycleCollect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCycleCollect.Click
If SerialPort.IsOpen = True Then
SendtoSbuf()
Timer.Enabled = True
Else : MessageBox.Show("请打开串口", "警告", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End If
End Sub
Private Sub Timer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer.Tick
SendtoSbuf()
End Sub
Private Sub btnStopCollect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopCollect.Click
Timer.Enabled = False
End Sub
End Class