Qiita Conference 2025

けんちょん (@drken)

読者層の解像度を高めて、読まれる記事を書こう!

0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

line monitor > テスト用 > COM > echo server

Last updated at Posted at 2015-12-11
C++ Builder XE4 + 独自拡張

line monitorで通信テストをしている中で、ロギングの改行処理がいまいち。

自分が経験したもので行の終端文字列は以下のものがある。

  • CR
  • LF
  • CR + LF
  • LF + CR

これら全部にきちんと対応したいと思いつつ、その場合はエコーサーバーを作ってテストしたほうがいいと思い、作った。

なお、下記のコードは独自拡張を使っているので、通常のXE4では動かない、はず。

Unit1.cpp
void __fastcall TForm1::B_connectClick(TObject *Sender)
{
	if (B_connect->Tag == 0) {
		Com232->PortNo = NE_port->Integer;
		Com232->BitRate = NE_baud->Integer;

		try {
			Com232->PortOpen();
		} catch (...) {
			ShowMessage(L"Com open fail");
			return;
		}
		B_connect->Tag = 1;
		B_connect->Caption = L"Stop";

	} else {
		B_connect->Tag = 0;
		B_connect->Caption = L"Start";
	}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Com232Receive(TObject *Sender, int ReceiveSize)
{
    static char szBuf[100];
    static int pos=0;

    char code;

    for (int i=0; i<ReceiveSize; i++) {
		code = Com232->ReceiveChar();
        switch (code) {
        default:
            if (isprint(code) || code == '\t') {
                szBuf[pos] = code;
                pos++;
            }
            break;
        case '\r':
        case '\n':
			if (pos > 0) {
				szBuf[pos] = '\0';

				Memo1->Lines->Add(String(szBuf));
				procCommand(String(szBuf));
				
				pos = 0;
            }
            break;
        }
	}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::procCommand(String rcvd)
{
	String retStr = rcvd;

	// 1. remove CR / LF / CR+LF
	retStr = StringReplace(retStr, L"\r\n", L"", TReplaceFlags()<<rfReplaceAll);
	retStr = StringReplace(retStr, L"\r", L"", TReplaceFlags()<<rfReplaceAll);
	retStr = StringReplace(retStr, L"\n", L"", TReplaceFlags()<<rfReplaceAll);

	// 2.add CR / LF based on check box
	if (CHK_CR->Checked) {
		retStr = retStr + L"\r";
	}
	if (CHK_LF->Checked) {
		retStr = retStr + L"\n";
	}

	Sleep(NE_delay_msec->Integer);

	Com232->TransString(retStr);
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?