function dxsample if exist('dxlib2.h') ~= 2; disp('header undefined'); return; end if ~libisloaded('dxlib2') loadlibrary('dxlib2.dll','dxlib2.h');end pcom = '\\.\COM3'; br = 1000000; tout = 100; devid = calllib('dxlib2','DX_OpenPort',pcom,br); if devid==0; disp('Open failed!'); unloadlibrary('dxlib2'); return; end Err = libpointer('uint16Ptr', 0); for i = 0:252 Ret = calllib('dxlib2', 'DX_Ping', devid, i+1, tout, Err); if Ret; fprintf('Found ID=%d\n', i+1); else fprintf('Not found ID=%d [$%04x]\n', i+1, Err.Value); end end calllib('dxlib2','DX_ClosePort',devid); unloadlibrary('dxlib2'); disp('Normal end');
#include <signal.h>COMポートやボーレート等は適宜変更して、GCC Developer Liteのコンパイラオプションを開き、設定リストから「x86 (Console)」を選んでコンパイルすればWindowsのコマンドプロンプトで動くexeファイルの完成。終了するにはCtrl+Cを押してね。
#include <windows.h>
#include <conio.h>
int term = 1;
void signal_handler (int signo) {
(void)signo;
signal (SIGINT, signal_handler);
term = 0;
}
void main (void) {
int i;
char c = 0;
HANDLE h = CreateFile ("\\\\.\\COM3", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
DCB dcb = { BaudRate:115200, ByteSize: 8, Parity: NOPARITY, StopBits: ONESTOPBIT, };
COMMTIMEOUTS timeouts = {0,0,0,0,0};
COMSTAT stat;
DWORD l;
UCHAR buf[500];
if (h != INVALID_HANDLE_VALUE) {
signal (SIGINT, signal_handler);
if (SetCommState (h, &dcb)) {
if (SetCommTimeouts (h, &timeouts)) {
while (term) {
if (kbhit ()) {
c = getch ();
WriteFile (h, &c, 1, &l, NULL);
}
if (ClearCommError (h, NULL, &stat)) {
if (stat.cbInQue > sizeof (buf)) stat.cbInQue = sizeof (buf);
if (ReadFile (h, &buf, stat.cbInQue, &l, 0) != 0) for (i = 0; i < l; i++) putchar (buf[i]);
}
Sleep (5);
}
}
}
CloseHandle (h);
}
}