def testTransactNamedPipeAsync(self): event = threading.Event() overlapped = pywintypes.OVERLAPPED() overlapped.hEvent = win32event.CreateEvent(None, 0, 0, None) self.startPipeServer(event, 0.5) open_mode = win32con.GENERIC_READ | win32con.GENERIC_WRITE hpipe = win32file.CreateFile( self.pipename, open_mode, 0, # no sharing None, # default security win32con.OPEN_EXISTING, win32con.FILE_FLAG_OVERLAPPED, None, ) # set to message mode. win32pipe.SetNamedPipeHandleState( hpipe, win32pipe.PIPE_READMODE_MESSAGE, None, None ) buffer = win32file.AllocateReadBuffer(1024) hr, got = win32pipe.TransactNamedPipe( hpipe, str2bytes("foo\0bar"), buffer, overlapped ) self.assertEqual(hr, winerror.ERROR_IO_PENDING) nbytes = win32file.GetOverlappedResult(hpipe, overlapped, True) got = buffer[:nbytes] self.assertEqual(got, str2bytes("bar\0foo")) event.wait(5) self.assertTrue(event.isSet(), "Pipe server thread didn't terminate")
def testTransactNamedPipeBlockingBuffer(self): # Like testTransactNamedPipeBlocking, but a pre-allocated buffer is # passed (not really that useful, but it exercises the code path) event = threading.Event() self.startPipeServer(event) open_mode = win32con.GENERIC_READ | win32con.GENERIC_WRITE hpipe = win32file.CreateFile( self.pipename, open_mode, 0, # no sharing None, # default security win32con.OPEN_EXISTING, 0, # win32con.FILE_FLAG_OVERLAPPED, None, ) # set to message mode. win32pipe.SetNamedPipeHandleState( hpipe, win32pipe.PIPE_READMODE_MESSAGE, None, None ) buffer = win32file.AllocateReadBuffer(1024) hr, got = win32pipe.TransactNamedPipe( hpipe, str2bytes("foo\0bar"), buffer, None ) self.assertEqual(got, str2bytes("bar\0foo")) event.wait(5) self.assertTrue(event.isSet(), "Pipe server thread didn't terminate")
def transact(self, name, writeData): buf = win32file.AllocateReadBuffer(length) win32pipe.TransactNamedPipe(name, writeData, buf, None) """ string/buffer = TransactNamedPipe(pipeName, writeData , buffer/bufSize , overlapped ) Combines the functions that write a message to and read a message from the specified named pipe into a single network operation. Parameters pipeName : PyUNICODE The name of the pipe. writeData : string/buffer The data to write to the pipe. buffer/bufSize : PyOVERLAPPEDReadBuffer/int Size of the buffer to create for the result, or a buffer to fill with the result. If a buffer object and overlapped is passed, the result is the buffer itself. If a buffer but no overlapped is passed, the result is a new string object, built from the buffer, but with a length that reflects the data actually read. overlapped=None : PyOVERLAPPED An overlapped structure or None """ pass
def _transact(self, write_data): """Wrapper over TransactNamedPipe""" read_buf = win32file.AllocateReadBuffer(512) err, data = win32pipe.TransactNamedPipe(self.file_handle, write_data, read_buf) while err == ERROR_MORE_DATA: err, d = win32file.ReadFile(self.file_handle, read_buf) data += d return data
def SCTDCStart(self, acqtime): BUFFSIZE = 1024 * 1024 target_buffer = POINTER(c_char * BUFFSIZE)() target_buffer.contents = (c_char * BUFFSIZE)() read_buffer = POINTER(c_char * BUFFSIZE)() read_buffer.contents = (c_char * BUFFSIZE)() mybuffer = win32file.AllocateReadBuffer(1024 * 1024) win32pipe.SetNamedPipeHandleState(self.hpipe, win32pipe.PIPE_READMODE_MESSAGE, None, None) success = win32pipe.TransactNamedPipe( self.hpipe, str2bytes("START %i" % (acqtime)), mybuffer, None) #↨win32pipe.DisconnectNamedPipe(self.hpipe) #win32file.CloseHandle(self.hpipe) return success
def testTransactNamedPipeBlocking(self): event = threading.Event() self.startPipeServer(event) open_mode = win32con.GENERIC_READ | win32con.GENERIC_WRITE hpipe = win32file.CreateFile(self.pipename, open_mode, 0, # no sharing None, # default security win32con.OPEN_EXISTING, 0, # win32con.FILE_FLAG_OVERLAPPED, None) # set to message mode. win32pipe.SetNamedPipeHandleState( hpipe, win32pipe.PIPE_READMODE_MESSAGE, None, None) hr, got = win32pipe.TransactNamedPipe(hpipe, str2bytes("foo\0bar"), 1024, None) self.failUnlessEqual(got, str2bytes("bar\0foo")) event.wait(5) self.failUnless(event.isSet(), "Pipe server thread didn't terminate")
def SCTDCRead(self, ROI): mybuffer = win32file.AllocateReadBuffer(1024 * 1024) data_pumped = [] data_unpumped = [] ROIlow = ROI[0] ROIhigh = ROI[1] #self.ConnectSCTDC() win32pipe.SetNamedPipeHandleState(self.hpipe, win32pipe.PIPE_READMODE_MESSAGE, None, None) result = win32pipe.TransactNamedPipe(self.hpipe, str2bytes("READ"), mybuffer, None) start = time.time() databytes = np.reshape( np.array([result[1][i] for i in range(len(result[1]))]), (-1, 4)) dataint = np.array([ struct.unpack('I', databytes[i, :])[0] for i in range(databytes.shape[0]) ]) #dataint = np.array([struct.unpack('I',databytes[i,:])[0] & 0x07FFFFFF for i in range(databytes.shape[0])]) for d in dataint: val = d & 0x07FFFFFF if ((val >= ROIlow) and (val <= ROIhigh)): if (((d & 0x08000000) >> 27) == 1): data_pumped.append(val) else: data_unpumped.append(val) stop = time.time() - start print stop return data_unpumped, data_pumped