This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to communicate between RS232 VB6.0 & 8051 keil C

I want to have some leds in P2.0~2.3 and show off the "ON" lamp in VB6.0 program though RS232 during the sensors are +5V output to P1.0~1.3 89S52 MCU. But it appears many errors and doesn't work in PC. Please advise me the comments for the following code. Thanks a lot!!

Parents
  • #include<REGX52.H>
    #include<STDIO.H>
    void load1(void);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)
    
       P2=0xfe;
    
      load1();
       else if(SBUF==0xfd)
             P2=0xfd;
    void load1(void)
    {
      P1_0 = !P1_0;
      if(P1_0==0)  send('0');
      else   send('1");
    }
    

Reply
  • #include<REGX52.H>
    #include<STDIO.H>
    void load1(void);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)
    
       P2=0xfe;
    
      load1();
       else if(SBUF==0xfd)
             P2=0xfd;
    void load1(void)
    {
      P1_0 = !P1_0;
      if(P1_0==0)  send('0');
      else   send('1");
    }
    

Children
  • By the VB6.0 for the following codes:
    
    Private Sub Command5_Click()
    tex1.Text = "connectiong"
    MSComm1.Output = "0"
    delayms 100
    buf = MSComm1.Input
    If buf = "2" Then
       LOAD1.FillColor = RGB(0, 255, 0)
       LOAD2.FillColor = RGB(0, 255, 0)
       LOAD3.FillColor = RGB(0, 255, 0)
       LOAD4.FillColor = RGB(0, 255, 0)
    Else
       tex1.Text = "fail"
    End If
    End Sub
    Private Sub Command6_Click()
    On Error Resume Next
       msg = "are u close?"
       code = 65
       Title = "closed"
       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, "file failed"
    
    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 = "set 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 = "set COM2"
      Else
        MSComm1.PortOpen = False
        tex1.Text = "no COM"
      End If
    End If
    End Sub
    
    Private Sub MSComm1_OnComm(Index As Integer)
    tex1.Text = "connecting"
    MSComm1.Output = "P1.0"
    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 = "fail"
    End If
    End Sub
    

  • before you do anything else, sort-out the layout of that code so that you can at least see some idea of its structure!

    That should make several errors immediately obvious; eg, unmatched brackets...

  • Sorry!

    void delayms(unsigned int time);
    
    main()
    {
      TMOD=0x20;
      TH1=230;
      TL1=230;
      TR1=1;
    
      SCON=0x70;
      RI=0;
      TI=0;
      ES=1;
      EA=1;
      while(1)
        {
         while(P3==0xff);
             switch(P3)
               {
                case 0xfb:
                   {
                         P1_0=0;
                 SBUF=P1;
                             break;
                       }
    
                case 0xf7:
                   {
                         P1_0=1;
                 SBUF=P1;
                             break;
                       }
    
                case 0xef:
                   {
                         P1_1=0;
                 SBUF=P1;
                             break;
                       }
    
                case 0xdf:
                   {
                         P1_1=1;
                 SBUF=P1;
                             break;
                       }
           }
    
              delayms(100);
              while(P3!=0xff);
            }
    }
    
    void scon_int (void) interrupt 4
    {
     if(RI==1)
        {
         RI=0;
         switch(SBUF)
              {
               case 0xfb:
                  {
                        P1_0=0;
                SBUF=P1;
                            break;
                      }
    
               case 0xf7:
                  {
                        P1_0=1;
                SBUF=P1;
                            break;
                      }
    
               case 0xef:
                  {
                        P1_1=0;
                SBUF=P1;
                            break;
                      }
    
               case 0xdf:
                  {
                        P1_1=1;
                SBUF=P1;
                            break;
                      }
          }
    
        }
      else TI=0 ;
    }
    
    void delayms(unsigned int time)
    {
      unsigned int n;
      while(time>0)
       {
         n=120;
         while(n>0)  n--;
         time--;
       }
    }
    

  • #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;
        }
    }
    

  • I want to output the signal through RS232 to PC(VB6.0) (just show ON/Off lamp on VB6.0)during the switch on/off. Please advise me how to write Keil C in addition base on the above code or provde some information for my reference. Thanks a lot!!

  • Thanks! But I want to have more related examples for my reference!!

  • But what you need first is to get a firm, foundational understanding of the principles.

    If you just want examples, there are tons available all over the internet - serial comms must be one of the commonest tasks on an 8051!

    There are even plenty of examples on this very site: http://www.keil.com/download/list/c51.htm

    The most basic example of serial comms is, of course, the classic "Hello, world" program - see: http://www.keil.com/support/man/docs/uv4/uv4_ex_hello.htm

    See also: http://www.keil.com/support/man/docs/uv4/uv4_ex_measureovw.htm

  • Thanks! If i have the following codes, how to write the code through RS232 to PC?? Please advise it!!

    #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;
        }
    }
    

  • Please re-read the entire thread from the top. Nobody here is going to do your homework.

  • Do you actually understand what it's doing?

    If you do, then put in comments to show your understanding.

    If not, then clearly that is your basic problem!

    Why don't you just start with "Hello World", get to understand it, and work from there?

    PS

    Your layout is still messy; don't use TABs - they don't work on (most) internet forums - use only spaces for indenting.

    Note that any decent editor (including uVision) can be set to insert spaces when you press the TAB button:

    Edit > Configuration > Editor > Insert spaces for tabs

  • I just want to ask the related questions, hope someone can help me. Why you say that!! how come the rules to do it!! If I can solve it, i dont need to post my questions!!

  • I know it's messy for you!! But now I m very urgent to know how to do it!! In fact, if i can't do it, I ll be worst!! I

  • If I can solve it, i dont need to post my questions!!

    No. You should have posted:

    "If I cannot solve it, I need to read the documentation and to debug".

  • "I'm very urgent to know how to do it!!"

    There are no shortcuts!
    If you want to know how to do it, then you need to spend time learning the principles involved!

    IF this is a homework assignment, then it is assumed that a capable student should be able to do it. If you really can't do it, then you need to speak to your tutors about either getting some additional tuition, or switching to another course...