'Option Explicit
Dim package(1000) As Byte
Private Sub Check1_Click()
If Check1.Value = 1 Then
Text5.Enabled = True
Else
Text5.Enabled = False
End If
End SubPrivate Sub Close_Click()
Unload Form1
Unload Form2
End SubPrivate Sub Command1_Click()
Call rd
End Sub
Private Sub rd()
Dim i As Integer
Dim ad, length As Integer
Dim result As Booleanlength = Val(Text3.Text) 'indicate  bytes of the data
Erase package
If Check1.Value = 1 Then
Timer1.Enabled = True
Timer1.Interval = Val(Text5.Text)
Else
Timer1.Enabled = False
End If
ad = Val(Text1.Text) + 1Call i2c_start              'initia the i2c start
result = i2c_wr(ad)
If (result) Then
Text4.Text = "The MCU don't acknowledge for reading"
Exit SubEnd If
For i = 0 To (length - 2)
package(i) = i2c_rd(True)
Next i
 package(length - 1) = i2c_rd(False)Call i2c_stop
If Check2.Value = 1 ThenText4.Text = Hex$(package(0))
For i = 1 To (length - 1)
If (i Mod 20) = 0 Then
Text4.Text = Text4.Text & vbCrLf & Hex$(package(i))
Else
Text4.Text = Text4.Text + "," + Hex$(package(i))
End If
Next i
Else     ' here is  the  dec 's  codeText4.Text = package(0)
For i = 1 To (length - 1)
If (i Mod 20) = 0 Then
Text4.Text = Text4.Text & vbCrLf & package(i)
Else
Text4.Text = Text4.Text & "," & package(i)
End If
Next i
End If''''''''''''''''''''''''''''''''''''lw''''''''''''''
Text6.Text = package(12) * 256 * 256 + package(11) * 256 + package(10)
Text7.Text = ((package(9) * 256 + package(8)) * 14.79) / 1023
Text8.Text = ((package(7) * 256 + package(6)) * 55) / 1023
Text9.Text = ((package(5) * 256 + package(4)) * 100.4) / 1023
End SubPrivate Sub Command2_Click()Dim result As Boolean
Dim i, j, m, length, addr As Integer
Dim ss As String
ReDim t(1000) As Integer
Text1.Locked = True
Text2.Locked = Trueaddr = Val(Text1.Text)
m = 0
ss = "0"
length = Len(Text2.Text)
For i = 1 To length
If (Mid(Text2.Text, i, 1) >= 0) And (Mid(Text2.Text, i, 1) <= 9) Then
ss = ss + Mid(Text2.Text, i, 1)
j = 1
ElseIf j = 1 Then
t(m) = Val(ss)
m = m + 1
ss = "0"
j = 0
End If
Next i
If j = 1 Then
t(m) = Val(ss)
m = m + 1
End IfCall i2c_start
result = i2c_wr(addr)
If (result) Then
Text4.Text = "The MCU don't acknowledge for writing"
Exit Sub
End If
i = 0
For i = 0 To (m - 1)
result = i2c_wr(t(i))
If (result) Then
Text1.Locked = False
Text2.Locked = False
Text4.Text = "The MCU don't acknowledge for writing"
Exit Sub
End If
Next i
Call i2c_stopText4.Text = "I2C writing completed"
Text1.Locked = False
Text2.Locked = False
End SubPrivate Sub Command4_Click() ' 请教大家这块为什么会出错呀???
Comm.PortOpen = True
    Comm.Output = "da"
Command4.Enabled = False 'disenable   open   the bottom
Command5.Enabled = True  'enable  the write bottom
Command1.Enabled = True 'enable the read bottom
Command2.Enabled = True 'enable the close bottom
Text4.Text = "the port is opened"
End SubPrivate Sub Command5_Click()
Comm.PortOpen = False '
Command4.Enabled = True 'enable   open   the bottom
Command5.Enabled = False 'disenable  the write bottom
Command2.Enabled = False 'disenable the close bottom
Command1.Enabled = False 'disenable the read bottom
Text4.Text = "The port isn't opened"
End SubPrivate Sub EEPROM_Click()Form2.ShowEnd SubPrivate Sub Form_Load()
'Unload Form2Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text5.Enabled = False
Text4.Locked = True     'disenable the recieve  data
Command1.Enabled = False
Command2.Enabled = False
Command5.Enabled = False
Timer1.Enabled = False
End SubPublic Sub SDA_H()
    Comm.DTREnable = False
End Sub
Public Sub SDA_L()
    Comm.DTREnable = True
End SubPublic Function SDA_IN() As Boolean
    Dim result As Integer
    result = Comm.DSRHolding
    SDA_IN = Not result
End Function
Public Sub SCL_H()
    Comm.RTSEnable = False
End Sub
Public Sub SCL_L()
    Comm.RTSEnable = True
End Sub
Public Sub i2c_start()
    Dim temp As Integer    Call SDA_H
    Call SCL_H
    Call SDA_L
    Call SCL_L
    
End SubPublic Sub i2c_stop()
    Call SDA_L
    Call SCL_H
    Call SDA_H
End Sub
Public Function i2c_wr(ByVal DATA As Integer) As Boolean
    Dim i As Integer
    Dim result As Boolean
    Call SCL_L
    i = 0
    While i < 8
        If ((DATA And &H80) = &H80) Then
            SDA_H
        Else
            SDA_L
        End If
        DATA = DATA And &HFF
        DATA = DATA * 2
        SCL_H
        SCL_L
        i = i + 1
    Wend
    SDA_H
    SCL_H
    i2c_wr = SDA_IN
    SCL_L
    SDA_H
    
End Function
Public Function i2c_rd(k As Boolean) As Byte
    Dim i As Byte
    Dim DATA As Integer
    Dim result As Byte
    DATA = 0
    i = 0
    While i < 8
        DATA = DATA * 2
        Call SCL_H
        If (SDA_IN) Then
            DATA = DATA + 1
        End If
        Call SCL_L
        i = i + 1
    Wend
    If (k) Then
        SDA_L
    Else
        SDA_H
    End If
    Call SCL_H
    Call SCL_L
    Call SDA_H
    result = DATA
    i2c_rd = result
    
End FunctionPrivate Sub MCU_Click()Load Form1
MCU.Checked = True   'open the mcu windows
EEPROM.Checked = FalseEnd SubPrivate Sub Timer1_Timer()
Call rd
Timer1.Interval = Val(Text5.Text)
End Sub

解决方案 »

  1.   

    请教是否是下面的红的地方的定义有问题呢?
    Private   Sub   rd() 
    Dim   i   As   Integer 
    Dim   ad,   length   As   Integer 
    Dim   result   As   Boolean 
    length   =   Val(Text3.Text)   'indicate     bytes   of   the   data 
    Erase   package 
    If   Check1.Value   =   1   Then 
    Timer1.Enabled   =   True 
    Timer1.Interval   =   Val(Text5.Text) 
    Else 
    Timer1.Enabled   =   False 
    End   If 
    ad   =   Val(Text1.Text)   +   1 Call   i2c_start                             'initia   the   i2c   start 
    result   =   i2c_wr(ad) 
    If   (result)   Then 
    Text4.Text   =   "The   MCU   don 't   acknowledge   for   reading " 
    Exit   Sub 
    Private   Sub   Command4_Click()   '   请教大家这块为什么会出错呀??? 
    Comm.PortOpen   =   True 
            Comm.Output   =   "da "