Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2341467
  • 博文数量: 816
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-17 17:57
文章分类

全部博文(816)

文章存档

2011年(1)

2008年(815)

分类:

2008-12-17 18:07:15

//---------------------------------------------------------------------------

#include
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
HANDLE hComm;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::openPORTClick(TObject *Sender)
{
char *ComNo;
DCB  dcb;
String Temp;
//取得要打开的通信端口
Temp = "COM"+IntToStr(rdCOM->ItemIndex+1);
ComNo = Temp.c_str();
hComm =CreateFile(ComNo,GENERIC_READ | GENERIC_WRITE,
       0, NULL, OPEN_EXISTING, 0, 0);
if (hComm == INVALID_HANDLE_VALUE) // 如果COM 未打开
  {
    MessageBox(0, "打开通信端口错误!!","Comm Error",MB_OK);
    return;
   }
//将dcb地址传入,以取得通信参数
  GetCommState(hComm,&dcb);                         // 得知目前COM 的状态
  dcb.BaudRate = CBR_9600;                          // 设置波特率为9600
  dcb.ByteSize = 8;                                 // 字节为 8 bit
  dcb.Parity = NOPARITY;                            // Parity 为 None
  dcb.StopBits = ONESTOPBIT;                        // 1 个Stop bit
  //通信端口设置
  if (!SetCommState(hComm, &dcb)) {       // 设置COM 的状态
    MessageBox (0, "通信端口设置错误!!!","Set Error",MB_OK);
    CloseHandle(hComm);
    return;
   }
  Timer1->Enabled = True;
}
//---------------------------------------------------------------------------
//定时从串口接收接收数据,并且显示在mReceive上
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
  String Temp;
  char inbuff[1024];
  DWORD nBytesRead, dwEvent, dwError;
  COMSTAT cs;
  unsigned long lStatus;
  if (hComm == INVALID_HANDLE_VALUE) return;
  if (GetCommModemStatus(hComm,&lStatus))
   {
     //检查CTS状态
     if (lStatus & MS_CTS_ON)
      spCTS->Brush->Color =clRed;
     else spCTS->Brush->Color =clWhite;
     //检查DSR状态
     if (lStatus & MS_DSR_ON )
       spDSR->Brush->Color =clRed;
     else spDSR->Brush->Color =clWhite;
     //检查RI状态
     if ( lStatus & MS_RING_ON )
       spRI->Brush->Color =clRed;
     else spRI->Brush->Color =clWhite;
     //检查CD状态
     if ( lStatus & MS_RLSD_ON )
       spCD->Brush->Color =clRed ;
     else spCD->Brush->Color =clWhite;
     }

    //取得状态
   ClearCommError(hComm,&dwError,&cs);
   // 数据是否大于我们所准备的Buffer
   if (cs.cbInQue > sizeof(inbuff))
   {
     PurgeComm(hComm, PURGE_RXCLEAR);  // 清除COM 数据
     return;
    }

   ReadFile(hComm, inbuff,cs.cbInQue,&nBytesRead,NULL);//接收COM 的数据
   //转移数据到变量中
   inbuff[cs.cbInQue]= '\0';
   // 将数据显示于Memo1 上

   mReceive->Text = mReceive->Text + inbuff;



}
//---------------------------------------------------------------------------
void __fastcall TForm1::closePORTClick(TObject *Sender)
{
  if (hComm!=INVALID_HANDLE_VALUE) CloseHandle(hComm);
  exit(EXIT_SUCCESS);
}
//---------------------------------------------------------------------------
//在mSend中检测到键盘"ENTER"键按下后,将mSend中的内容发送到串口
void __fastcall TForm1::mSendKeyPress(TObject *Sender, char &Key)
{
  String Temp;
  char *SendData;
  int  ln;
  unsigned long lrc,BS;
  if (Key!=13) return;
  if (hComm==0) return;     //检查Handle值
  Temp = mSend->Text;       //取得发送的字符串
  SendData = Temp.c_str(); //字符串转换
  //取得发送的字符串数
  BS = Temp.Length();
  //BS = StrLen(SendData);  //也可以使用此种方式取得字符串长度
  //实际的发送动作
  WriteFile(hComm,SendData,BS, &lrc,NULL); // 送出数据

  mSend->Text="";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 if(  spDTR->Brush->Color == clWhite)
 {
  EscapeCommFunction(hComm,SETDTR);
  spDTR->Brush->Color=clRed;
 }
 else
 {
 EscapeCommFunction(hComm,CLRDTR);
 spDTR->Brush->Color=clWhite;
 }
   if(EscapeCommFunction(hComm,SETRTS)==0)
      MessageBox (0, "设置RTS错误!!!","Set Error",MB_OK);


--------------------next---------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 if(  spDTR->Brush->Color == clWhite)
 {
  EscapeCommFunction(hComm,SETDTR);
  spDTR->Brush->Color=clRed;
 }
 else
 {
 EscapeCommFunction(hComm,CLRDTR);
 spDTR->Brush->Color=clWhite;
 }
   if(EscapeCommFunction(hComm,SETRTS)==0)
      MessageBox (0, "设置RTS错误!!!","Set Error",MB_OK);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
 if(  spRTS->Brush->Color == clWhite)
 {
  EscapeCommFunction(hComm,SETRTS);
  spRTS->Brush->Color=clRed;
 }
 else
 {
 EscapeCommFunction(hComm,CLRRTS);
 spRTS->Brush->Color=clWhite;
 }
}

//----------------------------------------------

--------------------next---------------------
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
        TRadioGroup *rdCOM;
        TButton *openPORT;
        TButton *closePORT;
        TMemo *mSend;
        TMemo *mReceive;
        TTimer *Timer1;
        TShape *spCD;
        TShape *spDSR;
        TShape *spCTS;
        TShape *spRI;
        TLabel *Label1;
        TLabel *Label5;
        TLabel *Label7;
        TLabel *Label8;
        TLabel *Label9;
        TLabel *Label10;
        TLabel *Label11;
        TLabel *Label12;
        TLabel *Label13;
        TLabel *Label14;
        TLabel *Label15;
        TShape *spDTR;
        TLabel *Label2;
        TShape *spRTS;
        TLabel *Label3;
        TLabel *Label4;
        TLabel *Label6;
        TLabel *Label16;
        TLabel *Label17;
        TLabel *Label18;
        TLabel *Label19;
        TLabel *Label20;
        TLabel *Label21;
        TLabel *Label22;
        TLabel *Label23;
        TLabel *Label24;
        TButton *Button1;
        TButton *Button2;
        void __fastcall openPORTClick(TObject *Sender);
        void __fastcall Timer1Timer(TObject *Sender);
        void __fastcall closePORTClick(TObject *Sender);
        void __fastcall mSendKeyPress(TObject *Sender, char &Key);
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall Button2Click(TObject *Sender);
       
private: // User declarations
public: // User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

--------------------next---------------------

阅读(1206) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~