function ret = dx_open()MATLABで宣言したdxopenを呼出しているだけ。 当然dxopenはmファイルだ。
eml.extrinsic ('dxopen');
ret = 0;
ret = dxopen();
function ret = dxopen()mファイルは前回使用したものを分割して戻り値を返すようにしただけなので、他のmファイルは割愛。
if exists('dxlib2.h','file') ~= 2;
disp('Not found');
devid = 0;
else
if ~libisloaded('dxlib2'); loadlibrary('dxlib2.dll','dxlib2.h'); end
devid = calllib('dxlib2','DX_OpenPort','\\.\COM4',1000000);
if devid ==0; unloadlibrary('dxlib2'); end
disp('call DX_OpenPort');
end
ret = devid;
disp('start end');
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);
}
}
Declare Function DX_OpenPort Lib "dxlib2.dll" (ByVal pcom As String, ByVal br As Long) As Long本文抜粋
Declare Function DX_ClosePort Lib "dxlib2.dll" (ByVal id As Long) As Boolean
Declare Function DX_Ping Lib "dxlib2.dll" (ByVal devid As Long, ByVal uid As Byte, ByVal tout As Integer, ByRef terr As Integer) As Boolean
Dim sMsg As String
Dim devid As Long
Dim i As Integer, terr As Integer
Dim vRet As Variant
ChDrive ActiveWorkbook.Path
ChDir ActiveWorkbook.Path
devid = DX_OpenPort("\\.\COM3", 1000000)
If devid = 0 Then
MsgBox "Port open failed!", vbCritical, "COMポートオープン失敗"
Else
For i = 0 To 10
vRet = DX_Ping(devid, i, 50, terr)
If vRet Then
sMsg = sMsg & i & " Alive" & Chr(13) & Chr(10)
Else
sMsg = sMsg & i & " Dead : " & Hex(terr) & Chr(13) & Chr(10)
End If
Next
DX_ClosePort devid
End If
MsgBox sMsg, , "DXLIB2 TEST"
from ctypes import *
import sys
# load library
dxlib2 = cdll.LoadLibrary("dxlib2.dll")
dxopen = dxlib2.DX_OpenPort
dxclose = dxlib2.DX_ClosePort
dxping = dxlib2.DX_Ping
# set types for return and parameter of API
dxopen.argtypes = [c_char_p, c_long]
dxopen.restype = c_void_p
dxclose.argtypes = [c_void_p]
dxping.argtypes = [c_void_p, c_ubyte, c_int, c_void_p]
# initialize
terr = c_ushort()
comport = "\\.\COM3"
baudrate = 1000000
timeout = 100
procsw = 0
OK = 1
NG = 0
cr = "\n"
lf = "\r"
# call API
devid = dxopen( comport, baudrate )
for idx in range(254):
result = dxping( devid, idx, timeout, byref(terr) )
if result == OK:
sys.stdout.write( "\n%d alive" % idx )
procsw = 0
else:
if procsw == 0:
sys.stdout.write( cr )
else:
sys.stdout.write( lf )
sys.stdout.write( "%d dead(%x)" % (idx,terr.value) )
procsw = 1
sys.stdout.flush()
dxclose( devid )
SMPL1.py dxlib2.dll |
> python SMPL1.py |