| 受信スレッド(RS-232C制御) | BCB Tips! Last modified : 2002/12/03 |
| 追加コンポート名 | プロパティ名 | 値(設定した値) | ||||||
| MainManu1 | メニューを追加 → |
| ||||||
| Bevel1 | Shape | bsTopLine | ||||||
| Edit1 | Font - Name | Courier | ||||||
| Font - Pitch | fpFixed | |||||||
| Button1 | Caption | Send | ||||||
| Memo1 | ScrollBars | ssVertical | ||||||
| ReadOnly | true | |||||||
| Font - Name | Courier | |||||||
| Font - Pitch | fpFixed | |||||||

| MainUnit.h |
//---------------------------------------------------------------------------
#ifndef MainUnitH
#define MainUnitH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Menus.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TMainMenu *MainMenu1;
TBevel *Bevel1;
TEdit *Edit1;
TButton *Button1;
TMenuItem *File;
TMenuItem *FileExit;
TMenuItem *Comm;
TMenuItem *CommOpen;
TMenuItem *CommClose;
TMenuItem *Help;
TMenuItem *HelpAbout;
TMemo *Memo1;
void __fastcall FileExitClick(TObject *Sender);
void __fastcall CommOpenClick(TObject *Sender);
void __fastcall CommCloseClick(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
HANDLE m_hComm; // COMポートハンドル
COMMTIMEOUTS m_CommTimeOuts; // タイムアウト情報
DCB m_DCB; // シリアル ディバイス コントロール ブロック
TReadCommThread *m_pReadCommThread; // スレッドの生成1/2
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
|
| MainUnit.cpp |
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "ReadCommUnit.h"
#include "MainUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Memo1->Clear();
Edit1->Text = "0123456789";
CommOpen->Enabled = true; // メニューをトグルさせる
CommClose->Enabled = false;
Button1->Enabled = false; // [Send] ボタンを無効にする
}
//---------------------------------------------------------------------------
// 終了
//---------------------------------------------------------------------------
void __fastcall TForm1::FileExitClick(TObject *Sender)
{
exit(0);
}
//---------------------------------------------------------------------------
// ポート・オープン
//---------------------------------------------------------------------------
void __fastcall TForm1::CommOpenClick(TObject *Sender)
{
//+--------------------------------+
//| シリアルポートのオープン |
//+--------------------------------+
m_hComm = CreateFile("COM1", // オープンするポート
GENERIC_READ | GENERIC_WRITE, // アクセスモード
0, // 共有モード
NULL, // セキュリティー属性
OPEN_EXISTING, // 作成方法
FILE_ATTRIBUTE_NORMAL, // ファイル属性
NULL); // コピーする属性付きファイルのハンドル
if(m_hComm == INVALID_HANDLE_VALUE){ // 通信ポート接続失敗
Application->MessageBox("Error opening port.\n\nSelect new settings,try again.","Error",MB_OK);
return;
}
//+--------------------------+
//| DCB構造体の設定 |
//+--------------------------+
GetCommState(m_hComm, &m_DCB); // 通信パラメータを取得
m_DCB.BaudRate = 9600; // ボーレート
m_DCB.fParity = true; // パリティーチェック
m_DCB.Parity = NOPARITY; // パリティ方式
m_DCB.ByteSize = 8; // 受信データのバイトあたりのビット数(4〜8)
m_DCB.StopBits = ONESTOPBIT; // ストップビットの数
// m_DCB.fOutxCtsFlow = false; // CTS(ClearToSend)出力フロー制御
// m_DCB.fOutxDsrFlow = false; // DSR(DataSetReady)出力フロー制御
// m_DCB.fDtrControl = DTR_CONTROL_HANDSHAKE; // DTR(DataTerminalReady)フロー制御の種類
// m_DCB.fRtsControl = RTS_CONTROL_HANDSHAKE; // RTS(RequestToSend)フロー制御の種類
// m_DCB.fInX = true; // XON/XOFF入力フロー制御
// m_DCB.fOutX = true; // XON/XOFF出力フロー制御
// m_DCB.fNull = true; // ヌル ストリッピングを有効
// m_DCB.XonChar = 0x11; // 送受信時のXON文字
// m_DCB.XoffChar = 0x13; // 送受信時のXOFF文字
SetCommState(m_hComm, &m_DCB); // 通信パラメータをセット
//+--------------------------+
//| タイムアウトの設定 |
//+--------------------------+
GetCommTimeouts(m_hComm, &m_CommTimeOuts); // タイムアウト情報を取得
m_CommTimeOuts.ReadIntervalTimeout = 0;
m_CommTimeOuts.ReadTotalTimeoutMultiplier = 0; // 0 0
m_CommTimeOuts.ReadTotalTimeoutConstant = 100; // 100 0
m_CommTimeOuts.WriteTotalTimeoutMultiplier = 0; // 0 200
m_CommTimeOuts.WriteTotalTimeoutConstant = 0; // 0 10000
SetCommTimeouts(m_hComm, &m_CommTimeOuts); // タイムアウト値をセット
// --- 監視するイベントのセットを指定する
// SetCommMask(m_hComm, EV_RXCHAR);
//+----------------------------------------------------+
//| 端末をレディ状態に(RTS、DTR信号をオンにする) |
//+----------------------------------------------------+
if (EscapeCommFunction(m_hComm, SETRTS | SETDTR) == true){ // RTS、DTR信号設定成功
}else{ // RTS、DTR信号設定失敗
Application->MessageBox("Error opening port.\n\nSelect new settings,try again.","Error",MB_OK);
return;
}
//+--------------------+
//| 受信スレッド |
//+--------------------+
m_pReadCommThread = new TReadCommThread(true, m_hComm); // 受信スレッドの生成2/2
// 受信スレッドを作動させる
m_pReadCommThread->Resume();
//+--------------+
//| その他 |
//+--------------+
CommOpen->Enabled = false; // メニューをトグルさせる
CommClose->Enabled = true;
Button1->Enabled = true; // [Send] ボタンを有効にする
}
//---------------------------------------------------------------------------
// ポート・クローズ
//---------------------------------------------------------------------------
void __fastcall TForm1::CommCloseClick(TObject *Sender)
{
//+--------------------------------+
//| シリアルポートのクローズ |
//+--------------------------------+
CloseHandle(m_hComm);
//+--------------+
//| その他 |
//+--------------+
CommOpen->Enabled = true; // メニューをトグルさせる
CommClose->Enabled = false;
Button1->Enabled = false; // [Send] ボタンを無効にする
}
//---------------------------------------------------------------------------
// 送信
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char str[MAX_PATH]; // 書き込みデータバッファ
int writeSize; //
DWORD dwErrors; // エラー情報
COMSTAT ComStat; // デバイスの状態
DWORD dwWritten; // ポートへ書き込んだバイト数
int freeBufSize; //
// 1文字ずつ出力
AnsiString OutChar = Edit1->Text;
for (int i=1; i<OutChar.Length()+1; i++ ){
AnsiString OneChar = OutChar[i];
// 文字列出力
strcpy(str, OneChar.c_str());
writeSize = strlen(str);
// 送信バッファに書き込めるまで待機
do{
ClearCommError(m_hComm, &dwErrors, &ComStat);
Application->ProcessMessages();
freeBufSize = MAX_PATH - ComStat.cbOutQue;
}while(freeBufSize <= writeSize );
//データをバッファへ書き込む
WriteFile(m_hComm, str, writeSize, &dwWritten, NULL);
}
}
|
| ReadCommUnit.h |
//---------------------------------------------------------------------------
#ifndef ReadCommUnitH
#define ReadCommUnitH
//---------------------------------------------------------------------------
#include <Classes.hpp>
//---------------------------------------------------------------------------
class TReadCommThread : public TThread
{
private:
protected:
void __fastcall TReadCommThread::DisplayIt(void);
void __fastcall Execute();
HANDLE m_hComm;
public:
__fastcall TReadCommThread(bool CreateSuspended, HANDLE hComm);
};
//---------------------------------------------------------------------------
#endif
|
| ReadCommUnit.cpp |
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "ReadCommUnit.h"
#include "MainUnit.h"
char InBuff[100]; // 読み出しデータバッファ
#pragma package(smart_init)
//---------------------------------------------------------------------------
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall TReadCommThread::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//---------------------------------------------------------------------------
__fastcall TReadCommThread::TReadCommThread(bool CreateSuspended, HANDLE hComm)
: TThread(CreateSuspended)
{
m_hComm = hComm;
}
//---------------------------------------------------------------------------
void __fastcall TReadCommThread::DisplayIt(void)
{
Form1->Memo1->SetSelTextBuf(InBuff);
}
//---------------------------------------------------------------------------
void __fastcall TReadCommThread::Execute()
{
//---- Place thread code here ----
DWORD dwErrors; // エラー情報
COMSTAT ComStat; // デバイスの状態
DWORD dwCount; // 受信データのバイト数
DWORD dwRead; // ポートから読み出したバイト数
FreeOnTerminate = true;
while(1)
{
ClearCommError(m_hComm, &dwErrors, &ComStat);
dwCount = ComStat.cbInQue;
ReadFile(m_hComm, InBuff, dwCount, &dwRead, NULL);
if(dwRead){
InBuff[dwRead] = 0;
Synchronize(DisplayIt);
}
}
}
//---------------------------------------------------------------------------
|
| Type | Name | Note
| 書籍 | C MAGAZINE 1998年12月号、ソースコード | 著者:荒井 孝一 | 発行:ソフトバンク(株) ネット | WIN Communications | CREATORS BANKさん
| ネット | Serial Communication with Borland C++ Builder | New Campus School | Indoor Climbing Wall ネット | NULL(0x00)を文字として認識 | VC++ 超初心者のホームページ
| |
|---|