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

📄 unit1.cpp

📁 一个串口通信的源码
💻 CPP
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <stdio.h>

#include "Pcomm.h"
#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
     CloseComEnabled();
     ComboBox1->Enabled = true;
     ComboBox2->Enabled = true;
BOOL spcf;
HANDLE hThread;
    hThread = GetCurrentProcess();
    spcf = SetPriorityClass(hThread,HIGH_PRIORITY_CLASS);
//    spcf = SetPriorityClass(hThread,REALTIME_PRIORITY_CLASS);
    if (spcf) Label5->Caption = "进程为HIGH_PRIORITY!";
    else     Label5->Caption = "设置进程HIGH_PRIORITY失败!";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ExitClick(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CloseComClick(TObject *Sender)
{
    sio_close(port);	/* Port Control, disable the port */
    CloseComEnabled();
    ComboBox1->Enabled = true;
    ComboBox2->Enabled = true;
}
//---------------------------------------------------------------------------
int icount = 0;
unsigned char bufr[300000];
int  bufri;
void  CALLBACK comm_interrupt(int port)
{
long n;
unsigned char ch;
    Form1->Timer1->Enabled = true;
    icount = 0;
    n = sio_iqueue(port);
    while(n)
    {
      ch=sio_getch(port);
      Form1->StaticText4->Caption = bufri;
      bufr[bufri++] = ch;
      n--;
    }
}
//---------------------------------------------------------------------------
int __fastcall TForm1::Get_baud()
{
int baud;
    if(ComboBox2->Text == "50")
      baud = B50;
    if(ComboBox2->Text == "75")
      baud = B75;
    if(ComboBox2->Text == "110")
      baud = B110;
    if(ComboBox2->Text == "134")
      baud = B134;
    if(ComboBox2->Text == "150")
      baud = B150;
    if(ComboBox2->Text == "300")
      baud = B300;
    if(ComboBox2->Text == "600")
      baud = B600;
    if(ComboBox2->Text == "1200")
      baud = B1200;
    if(ComboBox2->Text == "1200")
      baud = B1200;
    if(ComboBox2->Text == "1800")
      baud = B1800;
    if(ComboBox2->Text == "2400")
      baud = B2400;
    if(ComboBox2->Text == "4800")
      baud = B4800;
    if(ComboBox2->Text == "7200")
      baud = B7200;
    if(ComboBox2->Text == "9600")
      baud = B9600;
    if(ComboBox2->Text == "19200")
      baud = B19200;
    if(ComboBox2->Text == "38400")
      baud = B38400;
    if(ComboBox2->Text == "57600")
      baud = B57600;
    if(ComboBox2->Text == "115200")
      baud = B115200;
    return baud;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OpenComClick(TObject *Sender)
{
int ret, baud = B1200;
    port = StrToInt(ComboBox1->Text);
    baud = Get_baud();
    ret = sio_open (port);	/* Port Control, enable the port */
    if(ret == 0)
    {
     sio_ioctl (port, baud, P_NONE | BIT_8 | STOP_1 );
     sio_cnt_irq(port,comm_interrupt,1);
     OpenComEnabled();
     ComboBox1->Enabled = false;
     ComboBox2->Enabled = false;
    }else
    {
     ComboBox1->Enabled = true;
     ComboBox2->Enabled = true;
     CloseComEnabled();
    }
    Label0->Caption = ret;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CloseComEnabled()
{
    OpenCom->Enabled = true;
    CloseCom->Enabled = false;
    Button2->Enabled = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::OpenComEnabled()
{
      OpenCom->Enabled = false;
      CloseCom->Enabled = true;
      Button2->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  OpenDialog1->Options.Clear();
  OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist;
  OpenDialog1->Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
  OpenDialog1->FilterIndex = 2; // start the dialog showing all files
  if (OpenDialog1->Execute())
  {
	StaticText1->Caption = OpenDialog1->FileName;
    Thread1 *hread1;
    hread1 = new Thread1(true);       // create but do not run
//    hread1 -> Priority = tpNormal;    // set the priority
    hread1 -> Priority = tpIdle;    // set the priority
    hread1 -> Resume();               // now start the thread running
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
int iFileHandle;
    icount++;
    if(icount>9)
    {
      icount = 0;
      Timer1->Enabled = false;
      StaticText4->Caption = bufri;
      try
      {
        if(StaticText3->Caption=="")
          StaticText3->Caption = "t.tmp";
        iFileHandle = FileCreate(StaticText3->Caption);
        FileWrite(iFileHandle, bufr, bufri);
        FileClose(iFileHandle);
      }
      catch(...)
      {
        Application->MessageBox("Can't perform one of the following file operations: Open,Write, Close.", "File Error", IDOK);
      }
    bufri=0;
   }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  SaveDialog1->Options.Clear();
  SaveDialog1->Options << ofAllowMultiSelect << ofFileMustExist;
  SaveDialog1->Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
  SaveDialog1->FilterIndex = 2; // start the dialog showing all files
  if (SaveDialog1->Execute())
  {
   StaticText3->Caption = SaveDialog1->FileName;
  }
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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