本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有账号?注册会员
×
我编了一个AT89C4051与C++ Builder 5的串口通信(COM1)程序,P1.2接LED灯的负极,初始时是亮的,希望通过通信使其熄灭,但通信总是不成功,请高手指点!
C++ Builder 5的上位机程序如下所示:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit7.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
HANDLE hComm;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char *ComNo;
DCB dcb;
String Temp;
Temp="COM" +IntToStr(RadioGroup1->ItemIndex+1);
ComNo=Temp.c_str();
hComm=CreateFile(ComNo,
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
if(hComm==INVALID_HANDLE_VALUE)
{
MessageBox(0,"打开端口失败","Comm Error",MB_OK);
return;
}
GetCommState(hComm,&dcb);
dcb.BaudRate=CBR_9600;
dcb.ByteSize=8;
dcb.Parity=NOPARITY;
dcb.StopBits=ONESTOPBIT;
if(!SetCommState(hComm,&dcb))
{
MessageBox(0,"通信设置错误","Set Error",MB_OK);
CloseHandle(hComm);
return;
}
Shape1->Brush->Color=clRed;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(hComm!=INVALID_HANDLE_VALUE)
CloseHandle(hComm);
exit(EXIT_SUCCESS);
Shape1->Brush->Color=clBlue;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
char SendData[1];
unsigned long lrc,BS;
BS=1;
SendData[0]=0xff ;
WriteFile(hComm,SendData,BS,&lrc,NULL);
}
//---------------------------------------------------------------------------
单片机程序如下:
#include<reg52.h>
#include <stdio.h>
#define uchar unsigned char
void main()
{
int i;
SCON=0x50;
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
PCON&=0x7f;
EA=1;
TR1=1;
P1=0x00;
RI=0;
while(!RI);
RI=0;
for(i=0;i<20000;i++)
P1=0xff;
}
新建_Microsoft_Word_文档_(2).doc
(31 KB, 下载次数: 0)
|
|