vb.net 转 VC++.net(搞定另加50分-100分)
Declare Sub SetPortDir Lib "SX_GPIO.dll" (ByVal iPort As Integer, ByVal dbDir As Byte)
Declare Function GetPortDir Lib "SX_GPIO.dll" (ByVal iPort As Integer) As Byte
Declare Function ReadPort Lib "SX_GPIO.dll" (ByVal iPort As Integer) As Byte
Declare Sub WritePort Lib "SX_GPIO.dll" (ByVal iPort As Integer, ByVal dbValue As Byte)
Public Const MAX_ASCII_LENGTH = 2
Public data_Mark As Byte = 0
Public Function Ascii2Hex(ByVal p As Char) As Integer
If (p >= "0"c) AndAlso (p <= "9"c) Then
Return CType(AscW(p), Integer) - &H30
End If
If (p >= "A"c) AndAlso (p <= "F"c) Then
Return CType(AscW(p), Integer) - CType(AscW("A"), Integer) + &HA
End If
If p >= "a"c AndAlso p <= "f"c Then
Return CType(AscW(p), Integer) - CType(AscW("a"), Integer) + &HA
End If
Return -1
End Function
Private Sub fmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyCode
Case Keys.Escape
Application.Exit()
End Select
End Sub
Private Sub fmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cbPort.SelectedIndex = 0
End Sub
Private Sub txtSetDir_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSetDir.TextChanged
If txtSetDir.Text.Length = 0 OrElse txtSetDir.Text.Length > MAX_ASCII_LENGTH Then
MsgBox("Please input 00~FF.")
txtSetDir.Text = "0"
End If
End Sub
Private Sub txtWrite_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtWrite.TextChanged
If txtWrite.Text.Length = 0 OrElse txtWrite.Text.Length > MAX_ASCII_LENGTH Then
MsgBox("Please input 00~FF.")
txtWrite.Text = "0"
End If
End Sub
Private Sub btnSetDir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSetDir.Click
Dim dbDir As Byte
Dim nPort As Integer = Integer.Parse(cbPort.Text)
If txtSetDir.Text <> "" Then
If 2 = txtSetDir.Text.Length Then
Dim first_Ch As Char = Convert.ToChar(txtSetDir.Text.Substring(0, 1))
Dim second_Ch As Char = Convert.ToChar(txtSetDir.Text.Substring(1, 1))
Dim first_Num As Integer = Ascii2Hex(first_Ch)
Dim second_Num As Integer = Ascii2Hex(second_Ch)
If first_Num >= 0 AndAlso second_Num >= 0 Then
dbDir = Byte.Parse(Convert.ToString(first_Num * 16 + second_Num))
data_Mark = dbDir
SetPortDir(nPort, dbDir)
Else
MsgBox("Please input 00~FF.")
txtSetDir.Text = "0"
End If
End If
If 1 = txtSetDir.Text.Length Then
Dim last_Num As Integer = Ascii2Hex(Convert.ToChar(txtSetDir.Text.Substring(0, 1)))
If last_Num >= 0 Then
dbDir = Byte.Parse(Convert.ToString(last_Num))
data_Mark = dbDir
SetPortDir(nPort, dbDir)
Else
MsgBox("Please input 00~FF.")
txtSetDir.Text = "0"
End If
End If
Else
txtSetDir.Text = "0"
MsgBox("Please input 00~FF.")
End If
End Sub
Private Sub btnGetDir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetDir.Click
Dim get_Data As Byte
Dim nPort As Integer = Integer.Parse(cbPort.Text)
get_Data = GetPortDir(nPort)
If get_Data >= 0 AndAlso get_Data <= 255 Then
txtGetDir.Text = get_Data.ToString("X")
End If
End Sub
Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
Dim wData As Byte
Dim nPort As Integer = Integer.Parse(cbPort.Text)
If txtWrite.Text <> "" Then
If 2 = txtWrite.Text.Length Then
Dim first_Ch As Char = Convert.ToChar(txtWrite.Text.Substring(0, 1))
Dim second_Ch As Char = Convert.ToChar(txtWrite.Text.Substring(1, 1))
Dim first_Num As Integer = Ascii2Hex(first_Ch)
Dim second_Num As Integer = Ascii2Hex(second_Ch)
If first_Num >= 0 AndAlso second_Num >= 0 Then
wData = Byte.Parse(Convert.ToString(first_Num * 16 + second_Num))
wData = wData And data_Mark
WritePort(nPort, wData)
Else
MsgBox("Please input 00~FF.")
txtWrite.Text = "0"
End If
End If
If 1 = txtWrite.Text.Length Then
Dim last_num As Integer = Ascii2Hex(Convert.ToChar(txtWrite.Text.Substring(0, 1)))
If last_num >= 0 Then
wData = Byte.Parse(Convert.ToString(last_num))
wData = wData And data_Mark
WritePort(nPort, wData)
Else
MsgBox("Please input 00~FF.")
txtWrite.Text = "0"
End If
End If
Else
txtWrite.Text = "0"
MsgBox("Please input 00~FF.")
End If
End Sub
Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
Dim rData As Byte
Dim nPort As Integer = Integer.Parse(cbPort.Text)
rData = ReadPort(nPort)
rData = rData And CByte(Not data_Mark) 'Not data_mark 按位取反, C#中为~data_mark
If rData >= 0 AndAlso rData <= 255 Then
txtRead.Text &= rData.ToString("X")
End If
End Sub