P
PWR
I am trying to use Ruby's Win32API interface to access the windows file
i/o in order to make a serial port connection. However the code below
gives the error '[BUG] Segmentation fault' when GetOverlappedResult()
is called. Any ideas would be gratefully received.
# Windows constants.
GENERIC_READ = 0x80000000
GENERIC_WRITE = 0x40000000
OPEN_EXISTING = 0x00000003
FILE_FLAG_OVERLAPPED = 0x40000000
NULL = 0x00000000
# Create objects to access Windows file system API.
CreateFile = Win32API.new("Kernel32", "CreateFile", "PLLLLLL", "L")
CloseHandle = Win32API.new("Kernel32","CloseHandle", "L", "N")
ReadFile = Win32API.new("Kernel32","ReadFile","LPLLP","N")
WriteFile = Win32API.new("Kernel32","WriteFile","LPLLP","N")
SetCommState = Win32API.new("Kernel32","SetCommState","LP","N")
SetCommTimeouts = Win32API.new("Kernel32","SetCommTimeouts","LP","N")
GetLastError = Win32API.new("Kernel32","GetLastError", "V", "N")
GetCommState = Win32API.new("Kernel32","GetCommState", "LP", "N")
GetOverlappedResult = Win32API.new("Kernel32","GetOverlappedResult",
"LPLL", "L")
CreateEvent = Win32API.new("Kernel32","CreateEvent", "LLLP", "L")
# --------------------------------
# Setup com1
# --------------------------------
# Get a file handle
hFile = CreateFile.Call("com1", GENERIC_READ|GENERIC_WRITE,
0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL)
report
# Create and populate overlapped structures
hEvent_write = CreateEvent.Call(0,0,0,0); report
overlapped_write = [0,0,0,0,hEvent_write].pack("L*")
hEvent_read = CreateEvent.Call(0,0,0,0); report
overlapped_read = [0,0,0,0,hEvent_read].pack("L*")
# Create DCB and set com state.
dcb = "\0"*80
GetCommState.Call(hFile, dcb); report
dcb[5] = 150 # Set to 38400 baud.
SetCommState.Call(hFile, dcb); report
# Set timeouts
CommTimeouts = [2,1,1,0,0].pack("L*")
SetCommTimeouts.Call(hFile, CommTimeouts); report
# --------------------------------
# Test
# --------------------------------
numwritten = 0
outbuff = "Test"
# Write to com port
WriteFile.Call(hFile,outbuff,outbuff.size,numwritten,overlapped_write);report
# The next call fails!
res = GetOverlappedResult.Call(hFile,overlapped_write,numwritten,1)
CloseHandle.Call(hFile)
i/o in order to make a serial port connection. However the code below
gives the error '[BUG] Segmentation fault' when GetOverlappedResult()
is called. Any ideas would be gratefully received.
# Windows constants.
GENERIC_READ = 0x80000000
GENERIC_WRITE = 0x40000000
OPEN_EXISTING = 0x00000003
FILE_FLAG_OVERLAPPED = 0x40000000
NULL = 0x00000000
# Create objects to access Windows file system API.
CreateFile = Win32API.new("Kernel32", "CreateFile", "PLLLLLL", "L")
CloseHandle = Win32API.new("Kernel32","CloseHandle", "L", "N")
ReadFile = Win32API.new("Kernel32","ReadFile","LPLLP","N")
WriteFile = Win32API.new("Kernel32","WriteFile","LPLLP","N")
SetCommState = Win32API.new("Kernel32","SetCommState","LP","N")
SetCommTimeouts = Win32API.new("Kernel32","SetCommTimeouts","LP","N")
GetLastError = Win32API.new("Kernel32","GetLastError", "V", "N")
GetCommState = Win32API.new("Kernel32","GetCommState", "LP", "N")
GetOverlappedResult = Win32API.new("Kernel32","GetOverlappedResult",
"LPLL", "L")
CreateEvent = Win32API.new("Kernel32","CreateEvent", "LLLP", "L")
# --------------------------------
# Setup com1
# --------------------------------
# Get a file handle
hFile = CreateFile.Call("com1", GENERIC_READ|GENERIC_WRITE,
0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL)
report
# Create and populate overlapped structures
hEvent_write = CreateEvent.Call(0,0,0,0); report
overlapped_write = [0,0,0,0,hEvent_write].pack("L*")
hEvent_read = CreateEvent.Call(0,0,0,0); report
overlapped_read = [0,0,0,0,hEvent_read].pack("L*")
# Create DCB and set com state.
dcb = "\0"*80
GetCommState.Call(hFile, dcb); report
dcb[5] = 150 # Set to 38400 baud.
SetCommState.Call(hFile, dcb); report
# Set timeouts
CommTimeouts = [2,1,1,0,0].pack("L*")
SetCommTimeouts.Call(hFile, CommTimeouts); report
# --------------------------------
# Test
# --------------------------------
numwritten = 0
outbuff = "Test"
# Write to com port
WriteFile.Call(hFile,outbuff,outbuff.size,numwritten,overlapped_write);report
# The next call fails!
res = GetOverlappedResult.Call(hFile,overlapped_write,numwritten,1)
CloseHandle.Call(hFile)