none
keil c 與vb6.0溝通的問題 RRS feed

  • 一般討論

  • 我已做了一個8052的控制, 目的是P1.0~P1.3 收SENSOR OUTPUT +5V, 而P2.0~2.3能自動相應而著燈及經過RS232去電腦, 現在最重要怎樣改寫的KEIL C CODE 可以將SIGNAL傅送到電腦的VB6.0顯示燈上??  請各位幫忙!! 

    程式如下:
    #include<REGX52.H>
    #include<STDIO.H>
    main()
    {
      TMOD=0x20;
      TH1=230;
      TL1=230;
      TR1=1;

      SCON=0x70;
      while(1)
        {

          RI=0;
              TI=0;
              SBUF=P1 | 0xf0;
              while(TI==0);

              while(RI==0);

              if(SBUF==0xfe)
                 P1=0x3f;
              else if(SBUF==0xfd)
             P1=0xcf;
              else if(SBUF==0xfb)
             P1=0x0f;
              else if(SBUF==0xf7)
             P1=0xff;
        }


    另外我的VB6.0 程式如下, 相對來說, 我只寫了一個PORT做測試, 但執行有問題, 不知那裡出錯!  


    Private Sub Command6_Click()
    On Error Resume Next
       msg = "您確定要結束本程式嗎 ?"
       code = 65
       Title = "關閉程式"
       return_value = MsgBox(msg, code, Title)
    If return_value = 1 Then
       Command5_Click
       MSComm1.PortOpen = False
       End
    End If
    End Sub

    Private Sub Command7_Click()

    On Error GoTo OUTPUTERR
    Dim i As Integer, iFile As String

    iFile = "d:\test.txt"
    Do While Dir(iFile) <> ""
        i = i + 1
        iFile = "d:\test-" & i & ".txt"
    Loop
    Open iFile For Output As #1
    Print #1, Text0(0).Text
    Directory.Text = iFile

    Close #1
    Exit Sub 
    OUTPUTERR:
    MsgBox Err.Description, vbCritical, "存檔失敗"

    End Sub

    Private Sub Command8_Click()
    Text0(0).Text = Empty
    End Sub
    Private Sub Form_Load()
    On Error Resume Next
    LOAD1.FillColor = RGB(0, 255, 0)
    LOAD2.FillColor = RGB(0, 255, 0)
    LOAD3.FillColor = RGB(0, 255, 0)
    LOAD4.FillColor = RGB(0, 255, 0)
    MSComm1.PortOpen = False
    MSComm1.CommPort = 1
    MSComm1.PortOpen = True
    If MSComm1.PortOpen = True Then
       MSComm1.PortOpen = False
       MSComm1.CommPort = 1
       MSComm1.Settings = "1200,n,8,1"
       MSComm1.PortOpen = True
       tex1.Text = "已自動選用COM1"
    Else
        MSComm1.PortOpen = False
        MSComm1.CommPort = 2
        MSComm1.PortOpen = True
      If MSComm1.PortOpen = True Then
        MSComm1.PortOpen = False
        MSComm1.CommPort = 2
        MSComm1.Settings = "1200,n,8,1"
        MSComm1.PortOpen = True
        tex1.Text = "已自動選用COM2"
      Else
        MSComm1.PortOpen = False
        tex1.Text = "通訊埠無法給您用"
      End If
    End If
    End Sub

    Private Sub MSComm1_OnComm(Index As Integer)
    tex1.Text = "連線中"
    MSComm1.Output = "1"
    delayms 100
    buf = MSComm1.Input
    If buf = "0" Then
       LOAD1.FillColor = RGB(0, 255, 0)
      
    ElseIf buf = "1" Then
       LOAD1.FillColor = RGB(255, 0, 0)
       Text0(0).Text = Text0(0).Text + "GasSensor-" + Format(Now, "DD.MMM.YYYY.HH:MM:SS") + vbNewLine
      
    Else
       tex1.Text = "連線失敗"
    End If
    End Sub 

    2010年5月5日 上午 09:09

所有回覆

  • Hi,
    "我只寫了一個PORT做測試, 但執行有問題, 不知那裡出錯",這實在是看不出你的問題是甚麼?是不是在說明清楚一點,預期的結果是甚麼,目前的狀況又是甚麼樣子的呢?這樣別人也才比較好理解你的問題。


    請注意討論區相關規則,良好的討論環境需要大家共同努力、遵守
    2010年5月5日 下午 02:13
    版主
  • MSComm1.Output = "1"
    buf = MSComm1.Input

    錯誤是在執行哪列發生?注意Output和Input分別為唯寫和唯讀屬性...

    2010年5月7日 上午 02:06
  • 昨晚我用示波器測試控制板, MCU的P1.0~1.3有SIGNAL去RS232 PORT, KEIL C程式碼應該無問題如下:
    #include<REGX52.H>
    #include<STDIO.H>

    main()

    {
    TMOD = 0X20;
    TH1 = 230;
    TL1 = 230;
    TR1 = 1;

    SCON = 0X52;                //0X70;

    while(1)
    {

    RI = 0;
    TI = 0;
    SBUF = P1 | 0Xf0;
    while(TI==0);
    if(SBUF==0xfe)
    P2=0x3e;
    else if(SBUF==0xfd)
    P2=0x3d;
    else if(SBUF==0xfb)
    P2= 0xfb;
    else if(SBUF==0xf7)
    P2= 0xf7;
    }    }

    但VB6.0, 當接駁好MCU板及按"START" 仍無法收到顯示有開SENSOR燈及時間內容, 有錯誤字幕出現編譯錯誤:找不到方法或成員, 在"Private Sub MSComm1_OnComm(Index As Integer)
    Dim InByte As Byte


    Select Case MSComm1(0).CommEvent
    Case comEvReceive

    If MSComm1(0).InBufferCourt > 2 Then
    InByte = MSComm1.Output


    End If
    End Sub"  通訊埠的設定是否有出錯如下, 請指教!!

     

    Private Sub Form_Load()
    On Error Resume Next
    LOAD1.FillColor = RGB(0, 255, 0)
    LOAD2.FillColor = RGB(0, 255, 0)
    LOAD3.FillColor = RGB(0, 255, 0)
    LOAD4.FillColor = RGB(0, 255, 0)
    MSComm1(0).PortOpen = False
    MSComm1(0).CommPort = 1
    MSComm1(0).PortOpen = True
    If MSComm1(0).PortOpen = True Then
       MSComm1(0).PortOpen = False
       MSComm1(0).CommPort = 1
       MSComm1(0).Settings = "1200,n,8,1"
       MSComm1(0).PortOpen = True
       tex1.Text = "已自動選用COM1"
    Else
        MSComm1(0).PortOpen = False
        MSComm1(0).CommPort = 2
        MSComm1(0).PortOpen = True
      If MSComm1(0).PortOpen = True Then
        MSComm1(0).PortOpen = False
        MSComm1(0).CommPort = 2
        MSComm1(0).Settings = "1200,n,8,1"
        MSComm1(0).PortOpen = True
        tex1.Text = "已自動選用COM2"
      Else
        MSComm1(0).PortOpen = False
        tex1.Text = "通訊埠無法給您用"
      End If
    End If
    End Sub

    Private Sub MSComm1_OnComm1()
    Dim InByte As Byte


    Select Case MSComm1(0).CommEvent
    Case comEvReceive

    If MSComm1(0).InBufferCourt > 0 Then
    InByte = MSComm1.Output


    End If
    End Sub


    Private Sub START_Click()

    MSComm1(0).Output = "11111110"
    delayms 100
    If InByte = "0" Then
    LOAD1.FillColor = RGB(0, 255, 0)
    ElseIf InByte = "1" Then
    LOAD1.FillColor = RGB(255, 0, 0)
    Text0(0).Text = Text0(0).Text + "GasSensor-" + Format(Now, "DD.MMM.YYYY.HH:MM:SS") + vbNewLine
    End If



    MSComm1(0).Output = "11111101"
    delayms 100
    If InByte = "0" Then
    LOAD2.FillColor = RGB(0, 255, 0)
    ElseIf InByte = "1" Then
    LOAD2.FillColor = RGB(255, 0, 0)
    Text0(0).Text = Text0(0).Text + "FireSensor-" + Format(Now, "DD.MMM.YYYY.HH:MM:SS") + vbNewLine
    End If


    MSComm1(0).Output = "11111011"
    delayms 100
    If InByte = "0" Then
    LOAD3.FillColor = RGB(0, 255, 0)
    ElseIf InByte = "1" Then
    LOAD3.FillColor = RGB(255, 0, 0)
    Text0(0).Text = Text0(0).Text + "BreakGlass-" + Format(Now, "DD.MMM.YYYY.HH:MM:SS") + vbNewLine
    End If


    MSComm1(0).Output = "11110111"
    delayms 100
    If InByte = "0" Then
    LOAD4.FillColor = RGB(0, 255, 0)
    ElseIf InByte = "1" Then
    LOAD4.FillColor = RGB(255, 0, 0)
    Text0(0).Text = Text0(0).Text + "Test-" + Format(Now, "DD.MMM.YYYY.HH:MM:SS") + vbNewLine
    End If
    End Sub

    有關圖,請到以下網站:

    http://www.hkepc.com/forum/viewthread.php?tid=1445370&pid=22955410&page=7&extra=#pid22955410

     

    2010年5月7日 上午 02:06