The most flexible way of passing data between ECLiPSe and Tcl is via the I/O facilities of the two languages, ie. via ECLiPSe queue streams which can be connected to Tcl channels.
Currently, a communication channel between ECLiPSe and Tcl is created from Tcl, which appears as an ECLiPSe queue in ECLiPSe, and a channel in Tcl. The queue has a symbolic name and a stream number in ECLiPSe, and has a channel name in Tcl. Facilities are provided to interconvert between these names.
Queues pass data between ECLiPSe and Tcl in one direction: either from ECLiPSe to Tcl (from-ECLiPSe), or from Tcl to ECLiPSe (to-ECLiPSe). Queues are created with the direction specified. The queues should be viewed as communication channels: data is written to the queue, and it only becomes available to the other side when the queue is flushed. This is done by calling the predicate flush/1 on the ECLiPSe side, and by invoking ec_flush on the Tcl side. The flush also has the effect of briefly transferring control to the other side to allow handlers to handle the data (see section 5.6)1
{}
). To create a queue from ECLiPSe to Tcl, use ec_queue_create with the mode argument set to fromec, e.g.
Tcl: set my_in_channel [ec_queue_create my_out_queue fromec]
Once the queue is created, it can be used, e.g. by writing into it with ECLiPSe’s write/2 builtin, and reading using Tcl’s read command:
ECLiPSe: write(my_out_queue, hello), flush(my_out_queue). Tcl: set result [read $my_in_channel 5]
The disadvantage of using these low-level primitives is that for reading one must know exactly how many bytes to read. It is therefore recommended to use the EXDR (ECLiPSe external data representation, see section 5.8) format for communication. This allows to send and receive structured and typed data. The primitives to do that are write_exdr/2 on the ECLiPSe side and ec_read_exdr (section 5.8) on the Tcl side:
ECLiPSe: write_exdr(my_out_queue, foo(bar,3)), flush(my_out_queue). Tcl: set result [ec_read_exdr $my_in_channel]
In the example, the Tcl result will be the list {foo bar 3}. For details about the mapping see section 5.8.
To create a queue from Tcl to ECLiPSe to Tcl, use ec_queue_create with the mode argument set to toec, e.g.
Tcl: set my_out_channel [ec_queue_create my_in_queue toec]
Now the queue can be used, e.g. by writing into it with Tcl’s puts command and by reading using ECLiPSe’s read_string/4 builtin:
Tcl: puts $my_out_channel hello ec_flush [ec_streamname_to_streamnum my_in_queue] 5 ECLiPSe: read_string(my_in_queue, "", 5, Result).
The disadvantage of using these low-level primitives is that for reading one must know exactly how many bytes to read, or define a delimiter character. It is therefore recommended to use the EXDR (ECLiPSe external data representation, see section 5.8) format for communication. This allows to send and receive structured and typed data. The primitives to do that are ec_read_exdr (section 5.8) on the Tcl side and read_exdr/2 on the ECLiPSe side:
Tcl: ec_write_exdr $my_out_channel {foo bar 3} (SI) ec_flush [ec_streamname_to_streamnum my_in_queue] ECLiPSe: read_exdr(my_in_queue, Result).
In the example, the ECLiPSe result will be the term foo("bar",3). For details about the mapping see section 5.8.