hspextのソースが公開されてるので、これをもとに書いてみたらどうでしょうか。
http://dev.onionsoft.net/trac/openhsp/browser/trunk/plugins/win32/Hspext/comport.cpp
ポートのハンドルをもつ変数が1つしかないようですので、これさえ直せば複数できそうですが。
勉強がてらソースをそのままHSPに落としてみました。参考までに。
使ってみてないけれど案外動くんじゃないかなあ
#module ModSerial comHandle
// シリアルポート通信モジュール
// 使い方
// 通常のcom*系命令の第一引数にモジュール型変数を指定、残りの引数には同じものを指定
#uselib "kernel32.dll"
#cfunc CreateFileA "CreateFileA" sptr, int, int, int, int, int, int
#func WriteFile "WriteFile" int, sptr, int, int, int
#func ReadFile "ReadFile" int, int, int, int, int
#func CloseHandle "CloseHandle" int
#func GetCommState "GetCommState" int, int
#func SetCommState "SetCommState" int, int
#func BuildCommDCB "BuildCommDCBA" sptr, int
#func SetCommTimeouts "SetCommTimeouts" int, int
#func EscapeCommFunction "EscapeCommFunction" int, int
#func ClearCommError "ClearCommError" int, int, int
#define NULL 0
#define OPEN_EXISTING 3
#define GENERIC_READ 0x80000000
#define GENERIC_WRITE 0x40000000
#define FILE_ATTRIBUTE_NORMAL 0x00000080
#define INVALID_HANDLE_VALUE 0xffffffff
#define MAXDWORD 0xffffffff
#define SETDTR 5
#define CLRDTR 6
#define global comopen(%1, %2, %3="") comopen_ %1, %2, %3
#modfunc comopen_ int _port, str _proto
if _port > 0 {
path = "\\\\.\\COM" + _port
} else {
path = "LPT1"
}
comHandle = CreateFileA(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)
if comHandle == INVALID_HANDLE_VALUE : return -1
dim dcb, 7
GetCommState comHandle, varptr(dcb)
if stat == 0 : return -1
if strlen(_proto) > 0 {
BuildCommDCB _proto, varptr(dcb)
if stat == 0 : return 3
}
SetCommState comHandle, varptr(dcb)
if stat == 0 : return -1
dim timeouts, 5
timeouts(0) = MAXDWORD
timeouts(1) = 0
timeouts(2) = 0
timeouts(3) = 0
timeouts(4) = 0
SetCommTimeouts comHandle, varptr(timeouts)
EscapeCommFunction comHandle, SETDTR
return 0
#modfunc comclose
EscapeCommFunction comHandle, CLRDTR
CloseHandle comHandle
comHandle = NULL
return 0
#modfunc comput str _s
numWritten = 0
WriteFile comHandle, _s, strlen(_s), varptr(numWritten), 0
if stat == 0 : return 0
return -numWritten
#modfunc computc int _byte
sdim sd, 4
numWritten = 0
poke sd, 0, _byte
WriteFile comHandle, sd, 1, varptr(numWritten), 0
if stat == 0 : return 0
return -numWritten
#modfunc computb var v, int _size
numWritten = 0
WriteFile comHandle, varptr(v), _size, varptr(numWritten), 0
if stat == 0 : return 0
return -numWritten
#modfunc comget var v, int _size
sdim v, _size + 1
numRead = 0
if _size == 0 : size = 64 : else : size = _size
ReadFile comHandle, varptr(v), size, varptr(numRead), 0
if stat == 0 {
v = ""
return 0
}
poke v, numRead, 0
return -numRead
#modfunc comgetc var v
numRead = 0
sdim buf, 10
ReadFile comHandle, varptr(buf), 1, varptr(numRead), 0
v = peek(buf, 0)
return -numRead
#modfunc comcontrol int _fn
EscapeCommFunction comHandle, _fn
if stat == 0 : return -1
return 0
#modfunc comstat var v
dim v, 4
ClearCommError comHandle, varptr(v), varptr(v) + 4
if stat == 0 : return -1
return 0
#global
/*
newmod com1, ModSerial
comopen com1, 1
*/