标题:求助!vb上位机与单片机通信
取消只看楼主
wxl9000306
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-10-27
结帖率:0
已结贴  问题点数:10 回复次数:0 
求助!vb上位机与单片机通信
    目的:上位机发送一个数据给单片机,单片机将这个数据+1后返回给上位机
    现象:发送正常,返回时不论发送是几返回总是49
    谁能帮忙看看,那里的问题???

    下面是我vb代码:
Private Sub Command1_Click()
Dim outbyte(0) As Byte
Dim number As Integer
number = Val(Text1.Text)
outbyte(0) = CByte(number)
MSComm1.Output = outbyte
End Sub

Private Sub Command2_Click()
MSComm1.PortOpen = False
Unload Me
End Sub

Private Sub Form_Load()
Form1.AutoRedraw = True
= 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.InputLen = 0              ' 设置或返回一次从接收缓冲区中读取字节数,0表示一次读取所有数据
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.RThreshold = 1            ' 每个字符到接收缓冲区都触发接收事件
MSComm1.InputMode = comInputModehexadecimal      '采用二进制传输
MSComm1.PortOpen = True
Text1 = ""
Text2 = ""
End Sub

Private Sub MSComm1_OnComm()
    Dim BytReceived(0) As Byte
    Dim strBuff As Variant
    Select Case
        Case comEvReceive
         strBuff = MSComm1.InBufferCount
         BytReceived(0) = Asc(strBuff)
         Text2 = BytReceived(0)
    End Select
End Sub


 下面是我c的代码:
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
uchar a,flag;
void main()
{
    TMOD=0X20;
    TL1=0XFD;
    TH1=0XFD;
    SCON=0X50;
    TR1=1;
    EA=1;
    ES=1;
    while(1)
    {
        if(flag)
        {
            ES=0;
            flag=0;
            SBUF=a+1;
            while(!TI);
                TI=0;
            ES=1;
        }
    }
}
void xx() interrupt 4
{
    RI=0;
    a=SBUF;
    P0=0XF0|a;
    flag=1;
}
搜索更多相关主题的帖子: 单片机 number 
2012-10-27 09:48



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-384621-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.995121 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved