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 |
require "dl/import" module DXLIB2 extend DL::Importer dlload "./dxlib2.dll" extern "int DX_OpenPort( char *, long )" extern "int DX_Active( int )" extern "int DX_Ping( int, unsigned char, int, unsigned short * )" extern "int DX_ClosePort( int )" end comport = "\\\\.\\COM3" br = 1000000 timeout = 50 terr = ' ' * 2 mark = "\n" devid = DXLIB2.DX_OpenPort( comport, br ) if ( devid != 0 ) then ret = DXLIB2.DX_Active( devid ) if ( ret == 1 ) then sleep 1 i = 0 while ( i <= 253 ) ret = DXLIB2.DX_Ping( devid, i, timeout, terr ) if ( ret == 1 ) then printf( "\n%d alive", i ) mark = "\n" else printf( "%s%d dead(%x)", mark, i, terr.unpack('S')[0] ) mark = "\r" end i += 1 end end ret = DXLIB2.DX_ClosePort( devid ) if ( ret == 0 ) then print "CLOSE ERROR\n" end end print("\nnormal end") |
SMPL1.rb dxlib2.dll |
> ruby SMPL1.rb |
public class SMPL1 { private native int DxOpenPort( String com, int br ); private native boolean DxClosePort( int devid ); private native boolean DxPing( int devid, int uid, int tout, char[] terr ); static { System.loadLibrary( "dxtest" ); // 今回作成するラッピング用DLL(dxtest.dll) } public static void main( String args[] ) { String COMPORT = "\\\\.\\COM3"; // COMポート名 int BUADRATE = 1000000; // ボーレート int TIMEOUT = 50; // タイムアウト char terr[] = new char[1]; int devid, i; String msg = "Ping to ID:", mrk = "\n"; SMPL1 me = new SMPL1(); devid = me.DxOpenPort( COMPORT, BUADRATE ); if ( devid != 0 ) { for ( i = 0; i < 253; i++ ) { if ( me.DxPing( devid, i, TIMEOUT, terr ) ) { System.out.printf( "\nPing to ID:%d...OK", i ); mrk = "\n"; } else { System.out.printf( "%sPing to ID:%d...NG(%x)", mrk, i, (int)terr[0] ); mrk = "\r"; } } if ( !me.DxClosePort( devid ) ) System.out.printf( "Close error\n" ); } else System.out.printf( "Port not open\n" ); } } |
>javac SMPL1.java |
>javah -classpath . -d . SMPL1 |
JNIEXPORT jint JNICALL Java_SMPL1_DxOpenPort (JNIEnv *, jobject, jstring, jint); JNIEXPORT jboolean JNICALL Java_SMPL1_DxClosePort (JNIEnv *, jobject, jint); JNIEXPORT jboolean JNICALL Java_SMPL1_DxPing (JNIEnv *, jobject, jint, jint, jint, jcharArray) |
#define __MAKE_LIB__ #include <stdio.h> #include <stdlib.h> #include <jni.h> #include "dxlib2.h" JNIEXPORT jint JNICALL Java_SMPL1_DxOpenPort (JNIEnv *env, jobject jobj, jstring pCom, jint br) { DXDEVICEID dev; char *port; int sz = (*env)->GetStringLength( env, pCom ); // java文字列より取得 const char *comPort = (*env)->GetStringUTFChars( env, pCom, 0 ); port = (char *)malloc( sz + 1 ); strcpy( port, comPort ); port[strlen(port)] = 0x0; dev = DX_OpenPort( port, br ); // dxlib2.dllのDX_OpenPort()の呼出し // memory解放 free( port ); (*env)->ReleaseStringUTFChars( env, pCom, comPort ); return (jint)dev; } JNIEXPORT jboolean JNICALL Java_SMPL1_DxClosePort (JNIEnv *env, jobject jobj, jint id) { return DX_ClosePort( (DXDEVICEID)id ); } JNIEXPORT jboolean JNICALL Java_SMPL1_DxPing (JNIEnv *env, jobject jobj, jint devid, jint id, jint tout, jcharArray terr) { TDxErrorCode err; BOOL bResult; UCHAR uID; jchar *arrays; // local領域 int sz; // 配列size jboolean jbl; // isCopy uID = (UCHAR)(id & 0x00ff); bResult = DX_Ping( (DXDEVICEID)devid, uID, tout, &err ); // 配列の取得 arrays = (*env)->GetCharArrayElements( env, terr, &jbl ); if ( (*env)->GetArrayLength( env, terr ) > 0 ) arrays[0] = err; // 配列の解放(Javaへ適用) (*env)->ReleaseCharArrayElements( env, terr, arrays, 0 ); return bResult; } |
>gcc -shared -o dxtest.dll dxtest.c -l dxlib2 -Wl,-kill-at -I Javaのincludeパス -I Javaのincludeパス\win32 -L . |
SMPL1.class dxtest.dll dxlib2.dll |
>java SMPL1 |