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 |