
Hi everyone,
I hope it will be useful to someone, here you have two small programs, master and slave, in order to implement simple bidirectional communications between two console applications.
Code: Select all
;
;master.pb
;QuimV - 2008
;
Global quit.l, progid.l
;
OpenConsole()
b.s = GetFilePart(ProgramFilename())
b.s = Left(b.s,Len(b.s)-1-Len(GetExtensionPart(ProgramFilename())))
ConsoleTitle(b.s)
EnableGraphicalConsole(0)
;
progid.l = RunProgram("Slave.exe","","",#PB_Program_Open|#PB_Program_Read|#PB_Program_Write)
;
While quit.l = 0
Rx.s = Input()
If IsProgram(progid.l) And ProgramRunning(progid.l)
WriteProgramStringN(progid.l, Rx.s)
If FindString(Rx.s,"EXIT",1)
quit.l = 1
EndIf
Delay(50) ;Slave process the order
If AvailableProgramOutput(progid.l) > 0
PrintN(ReadProgramString(progid.l))
EndIf
Else
quit.l = 1
EndIf
Wend
;
CloseConsole()
CloseProgram(progid.l)
;
End
Code: Select all
;
;slave.pb
;QuimV - 2008
;
Global quit.l
;
OpenConsole()
b.s = GetFilePart(ProgramFilename())
b.s = Left(b.s,Len(b.s)-1-Len(GetExtensionPart(ProgramFilename())))
ConsoleTitle(b.s)
EnableGraphicalConsole(0)
;
Procedure.s DOIT()
ProcedureReturn "READY"
EndProcedure
;
While quit.l = 0
;Wait until the arrival of an order
Rx.s = Input()
;Execute the order
If FindString(Rx.s,"EXIT",1)
quit.l = 1
ElseIf FindString(Rx.s,"DOIT",1)
;Execute DOIT()
a.s = DOIT()
;Return result
PrintN("EXECUTED: DOIT() -> ANSWER: " + a.s)
Else
;Retornar resultado
PrintN("RECEIVED: " + Rx.s + " -> ANSWER: NO ACTION")
EndIf
;
Wend
;
CloseConsole()
End
;