把VB语言转化成C#~哪个大神帮帮小妹吧! 200C

Private Sub Timer1_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Timer1.Tick
Dim j As Object
Dim i As Object
Dim temp As String
Dim arr() As Byte
Dim n As Short
Dim temp1 As String
Dim arr1() As Byte
Dim n1 As Short
Dim buffer As String
Dim buffer1 As String
Dim kg(100) As Short '空格位置
Dim data1(10) As String '电压值16进制各字节
Dim data2(10) As String '电压值16进制各位
Dim datan As Integer '电压值的数字量形式
Dim dataV As Single '电压实际值
Dim byteToRead As Int16 = SerialPort1.BytesToRead '(读取缓冲区的字节长度)
Dim Inbyte(byteToRead) As Byte
Dim bytesRead As Int16 = 0
bz = bz + 1
buffer = "" : buffer1 = ""
If bz = 1 Then
temp = "681B1B6802006C320100000000000E00000401120A100400010001840003208D16"
n = Len(temp) \ 2 - 1
ReDim arr(n)
For i = 0 To n
arr(i) = Val("&H" & Mid(temp, i * 2 + 1, 2))
Next i
SerialPort1.Write(arr, 0, 33) '发送读指令
End If
If bz = 2 Then
'读取返回数据串"E5"
bytesRead = SerialPort1.Read(Inbyte, 0, byteToRead)
For i = LBound(Inbyte) To UBound(Inbyte)
buffer = buffer & Hex(Inbyte(i)) & Chr(32) '获得16进制数
Next i
temp1 = "1002005C5E16"
n1 = Len(temp1) \ 2 - 1
ReDim arr1(n1)
For i = 0 To n1
arr1(i) = Val("&H" & Mid(temp1, i * 2 + 1, 2))
Next i
'PLC返回数据 E5 后,确认写入命令,发送以下数据:10 02 00 5C 5E 16
If Mid(Trim(buffer), 1, 2) = "E5" Then
SerialPort1.Write(arr1, 0, 6) '发送确认指令
End If
End If
Dim byteToRead1 As Int16 = SerialPort1.BytesToRead '(读取缓冲区的字节长度)
Dim Inbyte1(byteToRead1) As Byte
Dim bytesRead1 As Int16 = 0
If bz = 3 Then
'读取返回数据串
bytesRead1 = SerialPort1.Read(Inbyte1, 0, byteToRead1)
For i = LBound(Inbyte1) To UBound(Inbyte1)
buffer1 = buffer1 & Hex(Inbyte1(i)) & Chr(32) '获得16进制数
Next i
If buffer1 <> "" Then
j = 1
For i = 1 To Len(Trim(buffer1))
If Mid(buffer1, i, 1) = " " Then
kg(j) = i '计算空格位置
j = j + 1
End If
Next i
'电压值是第25和26个字节
T16.Text = Mid(buffer1, kg(25) + 1, kg(27) - kg(25) - 1) '显示电压值16进制2字节
data1(1) = Mid(buffer1, kg(25) + 1, kg(26) - kg(25) - 1) '取电压值第1字节
data1(2) = Mid(buffer1, kg(26) + 1, kg(27) - kg(26) - 1) '取电压值第2字节
'取电压值各位
If Len(data1(1)) = 1 Then
data2(1) = "0" : data2(2) = Mid(data1(1), 1, 1)
Else
data2(1) = Mid(data1(1), 1, 1) : data2(2) = Mid(data1(1), 2, 1)
End If
If Len(data1(2)) = 1 Then
data2(3) = "0" : data2(4) = Mid(data1(1), 1, 1)
Else
data2(3) = Mid(data1(2), 1, 1) : data2(4) = Mid(data1(2), 2, 1)
End If
datan = Val("&H" & data2(1)) * (16 ^ 3) + Val("&H" & data2(2)) * (16 ^ 2) + Val("&H" & data2(3)) * (16 ^ 1) + Val("&H" & data2(4)) * (16 ^ 0) '计算电压10进制值
Tdata.Text = Str(datan)
dataV = datan / 6400
Tv.Text = VB6.Format(dataV, "0.00")
End If
bz = 0
End If
End Sub

'当退出程序时,关闭串行口
Private Sub Cmdquit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Cmdquit.Click
    SerialPort1.Close() '关闭串口
    Me.Close()
End Sub

End Class

1个回答

好好看看书,c#不很难得,特别好上手、、、

qq_16253637
qq_16253637 我知道但是没有学过VB啊。。。
大约 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问