大神们好,我要写一个上位机和单片机通信的小程序,目前主要是看能不能从单片机的485口读回数据来,是用C++builder 6.0写的,请帮我看看我写的数据帧那一块是哪里出错了;现在是我把MSComm1->CommPort写死了(主要是我不会用Listbox),调试时那个Com口通过485工装和单片机的485口已经连接好了)。
以下是Unit1.cpp文件内容:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "MSCommLib_OCX"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
if(MSComm1->PortOpen!=true)
{
MSComm1->CommPort=9; //设置通信端口号为COM1
MSComm1->InputMode=1; //设置串口传入数据为二进制
MSComm1->Settings = "4800,n,8,1"; //设置串口参数
MSComm1->InBufferSize=512;
MSComm1->OutBufferSize=1024;
MSComm1->RThreshold=1;
MSComm1->SThreshold=1;
MSComm1->PortOpen=true; //打开通信端口1
Memo1->Text="";
}
else
{
ShowMessage("串口已经打开");
return;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
MSComm1->PortOpen=false;
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Memo1->Text="";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int i;
String temp="FE FE FE FE 68 AA AA AA AA AA AA 68 13 00 DF 16";
byte buff[15];
buff[0]=StrToInt(0xFE);
buff[1]=StrToInt(0xFE);
buff[2]=StrToInt(0xFE);
buff[3]=StrToInt(0xFE);
buff[4]=StrToInt(0x68);
buff[5]=StrToInt(0xAA);
buff[6]=StrToInt(0xAA);
buff[7]=StrToInt(0xAA);
buff[8]=StrToInt(0xAA);
buff[9]=StrToInt(0xAA);
buff[10]=StrToInt(0xAA);
buff[11]=StrToInt(0x68);
buff[12]=StrToInt(0x13);
buff[13]=StrToInt(0x00);
buff[14]=StrToInt(0xDF);
buff[15]=StrToInt(0x16);
OleVariant Txbuff;
Txbuff=VarArrayCreate(OPENARRAY(int,(0,15)),varByte);
for(i=0;i<=15;i++)
{
Txbuff.PutElement(buff[i],i);
}
MSComm1->Output=Txbuff;
Memo1->Text=temp+'\n';
/* String temp="FEFEFEFE68AAAAAAAAAAAA681300DF16",temp1;
Txbuff
data=Edit1->Text;
temp="0x"+Edit1->Text; //转换成十六进制字符串
buff=StrToInt(temp); //字符串转换成整数
Txbuff=VarArrayCreate(OPENARRAY(int,(0,0)),varByte);
Txbuff.PutElement(buff,0);
MSComm1->Output=Txbuff; */
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
//MSComm1->PortOpen=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
Memo1->Text="hei hei! 嘿嘿!";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MSComm1Comm(TObject *Sender)
{
String temp;
byte buff[100];//声明一个存储接收数据的缓冲区
int ByteNum;//收到的字节数
OleVariant RxBuff;
if(MSComm1->CommEvent==comEvReceive)//表示接收缓冲区内有字符
{
if(MSComm1->InBufferCount>0)
{
RxBuff=MSComm1->Input;//如果缓冲区中有多于一个字节的数据
ByteNum=RxBuff.ArrayHighBound();
for(int i=0;i<=ByteNum;i++)
{
buff[i]=RxBuff.GetElement(i);
}
for(int i=0;i<=ByteNum;i++)
{
temp=IntToHex(buff[i],2)+" ";
}
Memo1->Text=temp;
}
}
else
{
Memo1->Text="数据未收到?";
Memo1->Font->Color=clRed;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
以下是Unit1.h内容:
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "MSCommLib_OCX.h"
#include <OleCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TButton *Button3;
TMSComm *MSComm1;
TButton *Button4;
TButton *Button5;
TMemo *Memo1;
void __fastcall FormCreate(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button4Click(TObject *Sender);
void __fastcall Button5Click(TObject *Sender);
void __fastcall MSComm1Comm(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
/////////////////////////////////////////////////////////////////////////////////////
以下是Form1截图: