Skip to content

Latest commit

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

#SerialCom

Windowsでシリアル通信を行うプログラムです. 言語はC++.
バグあるかもね.

##開発環境 VisualStudio2013

##使い方 SerialCom.hとSerialCom.cppを適当なところにおいてください.

##ドキュメント ただし適当です.


Robot::SerialCom( constchar *_portName, constchar *_boundRate)

####内容 SerialComオブジェクトの初期設定を行います.

####引数

  • _portName...使用するポートの名前(例: "COM3")
  • _boundRate...バウンドレート(例: 9600)

voidRobot::SerialCom::send(char *transString)

####内容 SerialComコンストラクタで設定したCOMポートに文字列を送信します. ####引数

  • transString...送信する文字列(終端はNULLでないと死にます)

####戻り値
なし


intRobot::SerialCom::read(char *buf, int bufSize)

####内容 SerialComコンストラクタで設定したCOMポートから送信されたデータを取得します.

####引数

  • buf...受信したデータを格納するメモリのアドレス
  • bufSize...bufのサイズ

####戻り値 bufに格納したデータのバイト数


voidSerialCom::clearReadBuffer()

####内容 受信用バッファをクリアします.

####引数 なし

####戻り値 なし


intRobot::getSerialPortNumbers(int *comPortTable, int num_max)

####内容 使用できるCOMポートナンバーを取得します.

####引数

  • comPortTable...使用できるポートナンバーを格納する配列のポインタ
  • num_max...comPortTableに格納するデータ数の上限

####戻り値 使用可能なCOMポートの数

##使用例 使用可能なポートを調べる例.

#include<cstdio>
#include"SerialCom.h"intmain()
{
int comPortTable[255];
int numOfComPorts = Robot::getSerialPortNumbers(comPortTable, 255);
for (int i = 0; i < numOfComPorts; i++)
{
printf("COM%d\n", comPortTable[i]);
}
printf("キーを押してください\n");
while (getchar());
return0;
}

受信プログラムの例.

#include<cstdio>
#include<Windows.h>
#include"SerialCom.h"intmain()
{
Robot::SerialCom com("COM3", 9600);
char buf[255];
int count = 0;
while(count < 10)
{
int len = com.read(buf, 255);
if(len != 0)
{
printf("%s", buf);
}
count++;
Sleep(100);
}
com.exit();
return0;
}

送信プログラムの例.

#include<cstdio>
#include<Windows.h>
#include"SerialCom.h"intmain()
{
Robot::Serial com("COM3", 9600);
char buf[255];
int count = 0;
while(count < 10)
{
sprintf(buf, "%d", count);
com.send(buf);
}
com.exit();
return0;
}

About

シリアル通信

Resources

Stars

2 stars

Watchers

26 watching

Forks

Releases

Packages

Contributors

Languages