I'm trying to make a AHK simple script that connects to my named pipe python script to retrieve real time data. Here is a sample. The named pipe works. When I create a python script to connect to it and print out the sample data, it works, so I know the named pipe is functioning properly. But when I make an AHK script to connect to it, it always says that it failed to connect to it. Any help would be greatly appreciated. Thanks!
Code: Select all
[code]
import win32pipe
import win32file
import struct
import time
PIPE_NAME = r'\\.\pipe\SamplePipe'
def main():
# Create named pipe
pipe = win32pipe.CreateNamedPipe(
PIPE_NAME,
win32pipe.PIPE_ACCESS_OUTBOUND,
win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT,
1, 65536, 65536,
300,
None
)
print("Waiting for connection...")
win32pipe.ConnectNamedPipe(pipe, None)
print("Connected to pipe.")
try:
while True:
# Sample x, y coordinate data
x = 10
y = 20
# Convert data to bytes
data = struct.pack('ii', x, y)
# Write data to named pipe
win32file.WriteFile(pipe, data)
print(f"Sent data: x={x}, y={y}")
time.sleep(1)
finally:
win32file.CloseHandle(pipe)
if __name__ == "__main__":
main()
[/code]
Code: Select all
#Persistent
PIPE_NAME := "\\.\pipe\SamplePipe"
pipe := DllCall("CreateFile", "Str", PIPE_NAME, "UInt", 0xC0000000, "UInt", 3, "Ptr", 0, "UInt", 3, "UInt", 0, "Ptr", 0)
if (pipe = -1) {
MsgBox, Failed to connect to named pipe.
ExitApp
} else {
MsgBox, Connected to named pipe successfully.
}
Loop {
VarSetCapacity(data, 8)
bytesRead := DllCall("ReadFile", "Ptr", pipe, "Ptr", &data, "UInt", 8, "Ptr", 0, "UInt", 0)
if (bytesRead > 0) {
x := NumGet(data, 0, "Int")
y := NumGet(data, 4, "Int")
MsgBox, Data received:`nX: %x%`nY: %y%
} else if (bytesRead < 0) {
MsgBox, Error reading from named pipe.
}
Sleep, 1000
}