⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commmainunit.cpp

📁 串口调试程序源程序。 非常好的串口调试助手
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*---------------------------------------------------------------------------
  串口通讯测试主程序            V 1.0
  注意事项:
            调入文件只允许调入文本文件

                                珠海经济特区晋电自动化设备公司  张尚文
                                                1998 年
---------------------------------------------------------------------------*/
#include <vcl.h>
#pragma hdrstop
#include <stdio.h>

#include "AboutBox.h"
#include "Functions.h"
#include "Settings2.h"
#include "ParityTypeSele.h"
#include "CommMainUnit.h"

//#include "crc32.h"
#include "CRCClass.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cspin"
#pragma link "CSPIN"
#pragma resource "*.dfm"
TCommMain *CommMain;

//---------------------------------------------------------------------------
enum hexType {htValid,htInvalid,htSeperator};

hexType ConvertByte( Byte &b);
Byte Combine( Byte b1,Byte b2);

hexType ConvertByte( Byte &b)
{
  if ( b >='0' && b <= '9' )  {
    b -= '0';
    return htValid;
  }
  if ( b >='a' && b <= 'f' )  {
    b -= 'a';
    b += '\xa';
    return htValid;
  }
  if ( b >='A' && b <= 'F' )  {
    b -= 'A';
    b += '\xa';
    return htValid;
  }
  if ( b == ' ' || b == '\t' || b == '\r' || b == '\n' )
    return htSeperator;
  else
    return htInvalid;
}

Byte Combine( Byte b1,Byte b2)
{
  Byte b = b1;
  b <<= 4;
  b += b2;
  return b;
}
//---------------------------------------------------------------------------
__fastcall TCommMain::TCommMain(TComponent* Owner)
    : TForm(Owner)
{
  RepeatCount = 0;
  BytesReceived = 0;
  DataReceived  = "";
  DataModified  = true;
  SBytesInQue   = -1;
  SBytesOutQue  = -1;
  tempStore     = NULL;
  CurrHistory   = LastHistory = -1;
  HistoryFull   = false;
  IsSendingData = false;
  Comm = new TCommControl;
  Comm->OnOverRThreshold = DataArrived;
  Comm->OnSendOut = PackageFinished;
  Application->OnIdle = IdleHandler;
}
//---------------------------------------------------------------------------
void __fastcall TCommMain::SetRepeatCount(int RCount)
{
  FRepeatCount = (RCount==MaxInt)?0:RCount;
  RepeatCountLabel->Caption = FRepeatCount;
}
//---------------------------------------------------------------------------
void __fastcall TCommMain::SetPortClick(TObject *Sender)
{
  FormSettings->ReadSetting(Comm);
  FormSettings->ShowModal();
  if ( FormSettings->ModalResult == mrOk )  {
    FormSettings->WriteSetting(Comm);
    UpdateStatus();
  } // if ( FormSetting->ModalResult == mrOk )
}
//---------------------------------------------------------------------------
void __fastcall TCommMain::ClosePortClick(TObject *Sender)
{
  Comm->ClosePort();
  UpdateStatus();
}
//---------------------------------------------------------------------------
void __fastcall TCommMain::OpenPortClick(TObject *Sender)
{
  Comm->OpenPort();
  if ( !Comm->PortOpen )
    ShowMessage("无法打开指定的通讯口!");
  else
    RepeatTimer->Enabled = RepeatSend->Checked;
  UpdateStatus();
}
//---------------------------------------------------------------------------
void __fastcall TCommMain::FormActivate(TObject *Sender)
{
  Comm->RThreshold = intChars->Value;
  FormSettings->ReadSetting(Comm);
  UpdateStatus();
  maxStoreChange(this);
}
//---------------------------------------------------------------------------
void __fastcall TCommMain::UpdateStatus(void)
{
  StatusBar1->Panels->Items[0]->Text = AnsiString("端口: ") +
                                       AnsiString(Comm->Port);
  if ( Comm->PortOpen )  {
    OpenPort->Enabled  = false;
    ClosePort->Enabled = true;
    StatusBar1->Panels->Items[1]->Text = "状态: 打开";
    if ( RD->Brush->Color == clWhite )  {
      RD->Brush->Color = clGreen;
      WD->Brush->Color = clGreen;
    }
  }
  else  {
    OpenPort->Enabled  = true;
    ClosePort->Enabled = false;
    StatusBar1->Panels->Items[1]->Text = "状态: 断开";
    RD->Brush->Color = clWhite;
    WD->Brush->Color = clWhite;
  }
}
//---------------------------------------------------------------------------
void __fastcall TCommMain::IdleHandler(TObject *Sender, bool &Done)
{
  if ( DataModified )  {
    RMemo->Text = DataReceived;
    StatusBar1->Panels->Items[2]->Text = AnsiString("已接收字符数: ") +
                                           AnsiString(BytesReceived);
    DataModified = false;
  }
  if ( (int)Comm->BytesInQue != SBytesInQue )  {
    SBytesInQue = Comm->BytesInQue;
    StatusBar1->Panels->Items[3]->Text = AnsiString("输入缓冲区字符数: ") +
                                         AnsiString(SBytesInQue);
  }
  if ( (int)Comm->BytesOutQue != SBytesOutQue )  {
    SBytesOutQue = Comm->BytesOutQue;
    StatusBar1->Panels->Items[4]->Text = AnsiString("输出缓冲区字符数: ") +
                                         AnsiString(SBytesOutQue);
  }
  if ( Comm->PortOpen && SMemo->Text.Length() > 0 )  {
    if ( !RepeatSend->Checked )
      Send->Enabled = true;
    RepeatSend->Enabled = true;
  }
  else  {
    Send->Enabled = false;
    RepeatSend->Enabled = false;
  }

  EnableAddParity();
//  if ( SMemo->Text.Length() > 0 && !AddParity->Enabled )
//    AddParity->Enabled = true;
//  if ( SMemo->Text.Length() <= 0 && AddParity->Enabled )
//    AddParity->Enabled = false;

  if ( RMemo->Text.Length() <= 0 && ClearRMemo->Enabled )
    ClearRMemo->Enabled = false;
  if ( RMemo->Text.Length() > 0 && !ClearRMemo->Enabled )
    ClearRMemo->Enabled = true;

}
//---------------------------------------------------------------------------
void __fastcall TCommMain::DataArrived(void)
{
  RDFlashTimer->Enabled = true;
  BytesRead = Comm->RThreshold; //Comm->BytesInQue;
  Comm->RThreshold = 0;             // 禁止产生新的中断
  BytesReceived += BytesRead;
  if ( BytesRead > 0 )  {
    Comm->Receive((Byte*)InBuff, BytesRead);
    AddBytes();
  }
  Comm->RThreshold = intChars->Value;             // 开启中断
}
//---------------------------------------------------------------------------
void __fastcall TCommMain::PackageFinished(void)
{
  WDFlashTimer->Enabled = true;
  if ( !( IsSendingData && (StartingAt < SMemo->Text.Length()) &&
          SendPackage() ) )  {
    IsSendingData = false;
    Send->Enabled = true;
    if ( RepeatSend->Checked )
      RepeatTimer->Enabled = true;
  }
}
//---------------------------------------------------------------------------
void __fastcall TCommMain::AddBytes(void)
{
  if ( BytesReceived > maxStore->Value )  {
    DataReceived = "";
    BytesReceived = BytesRead;
  }
  if ( RASCII->Checked )  {
    InBuff[BytesRead] = '\0';
    DataReceived = DataReceived + (char*)InBuff;
  }
  else  {
    int i;
    for ( i = 0; i < BytesRead; i++ )
      sprintf(&tempStore[i*3],"%02X ", InBuff[i]);
    tempStore[BytesRead*3] = '\0';
    DataReceived = DataReceived + tempStore;
  }
  DataModified = true;
}
//---------------------------------------------------------------------------
void __fastcall TCommMain::SendClick(TObject *Sender)
{
  if ( SMemo->Text.Length() > 0 )  {
    StartingAt = 0;
    if ( CurrHistory == -1 )  {
      History[1] = SMemo->Text;
      UseHex[1]  = SHEX->Checked;
      LastHistory  = 0;
      CurrHistory = 1;
    }
    else  {
      if ( !( SMemo->Text==History[CurrHistory] &&
              SHEX->Checked==UseHex[CurrHistory]) )  {
        CurrHistory = CurrHistory%HISTNUM + 1;
        History[CurrHistory] = SMemo->Text;
        UseHex[CurrHistory]  = SHEX->Checked;
        if ( CurrHistory == HISTNUM  )
          HistoryFull = true;
      }
      LastHistory = CurrHistory -1;
      if ( LastHistory == 0 && HistoryFull )
        LastHistory = HISTNUM;
      PrevBtn->Enabled = LastHistory>=1;
    }
    IsSendingData = true;   // 设置正在发送数据标志
    //while ( StartingAt < SMemo->Text.Length() )
    SendPackage();          // 开始发送数据数据包
    //IsSendingData = false;   // 设置正在发送数据标志
    //WDFlashTimer->Enabled = true;
    //Send->Enabled = true;
    //if ( RepeatSend->Checked )
    //  RepeatTimer->Enabled = true;
    RepeatCount++;
  }
}
//---------------------------------------------------------------------------
bool __fastcall TCommMain::SendPackage(void)
//发送一数据包(定长),如果发送了数据返回真值,否则返回假值
{
#define PackageLen 86
  Byte temp[PackageLen+2];
  int  i;
  int  datalen = SMemo->Text.Length();
  if ( StartingAt < datalen )  {
    if ( SHEX->Checked )   {  // 编辑窗内的是十六进制字符
      if ( (i = SMemoHexToChar(temp,StartingAt,PackageLen)) > 0 )
        Comm->Send(temp,i);
      else
        return false;
    }
    else  {                   // 编辑窗内的是ASCII字符
      for ( i=0; i < PackageLen && StartingAt + i < datalen; i++ )   {
        temp[i] = SMemo->Text.c_str()[StartingAt + i];
      }
      StartingAt += i;
      Comm->Send(temp,i);
    }
    return true;
  }
  else   //  ( StartingAt < datalen )
    return false;
}
//---------------------------------------------------------------------------



void __fastcall TCommMain::ClearRMemoClick(TObject *Sender)
{
  DataReceived  = "";
  BytesReceived = 0;
  DataModified  = true;
  //RepeatCount = 0;
}
//---------------------------------------------------------------------------

void __fastcall TCommMain::RASCIIClick(TObject *Sender)
{
  int i = 0;
  DataReceived = "";
  Byte Byte1,Byte2;
  while ( i < RMemo->Text.Length() )  {
    Byte1 = RMemo->Text.c_str()[i++];
    switch ( ConvertByte(Byte1) )  {
    case htInvalid:
         ShowMessage("有无法转换的字符!");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -