Satimage Previous
RS232 serial link
Home Documentation Smile The industrial interfaces RS232 serial link  
Smile lets you control by script any USB/RS232 adapter. By script, you configure the RS232 serial links and you receive and send characters. To control the serial link, you will create one or several instances of the RS232 class.

Here are the detailed instructions.
  • First, install the driver software which ships with the adapter, plug the adapter into your machine and launch Smile.
  • Query the list of the currently active ports: use serial ports. This returns a list as in the example below. Here we query serial ports on a machine with a 2-ports Keyspan USB/Serial adapter installed. (We recall that Smile's RS232 commands support any USB/RS232 adapter.)
    serial ports
      --  {{9, "/dev/cu.modem", "modem"}, {9, "/dev/cu.USA28X21P1.1", "USA28X21P1.1"}, {9, "/dev/cu.USA28X21P2.2", "USA28X21P2.2"}, {10, "en0", ""}, {10, "en1", ""}}
    Each item in the list corresponds to one given port active on your machine. The information for a given port is a list of three items: an integer, a string looking like a POSIX path, the configuration name, and another string, the port's name. The ports that you can address with Smile are those for which the integer's value is 9.

    If your machine has a built-in modem, a "modem" port will show in the list. If your machine has a built-in IR port, a "IrDA" port will show in the list.

    To control a serial port, you have to use its configuration name: your script should scan the list and retain the configuration name of the port you are interested in. To follow with the example, we choose the first port of the Keyspan adapter.
    set the_config_name to "/dev/cu.USA28X21P1.1"
  • Create an object of the RS232 class and specify what port it will control by specifying a configname property in the same instruction.
    set the_RS to make new RS232 with properties {configname: the_config_name}
  • Specify the settings for the communication by setting the RSOptions property of the RS232 object. Provide a record as described in the dictionary in the entry about Class RSOptions, for instance the following.
    set RSOptions of the_RS to {bauds:9600, databits:8, stopbits:1, parity:0}
  • The ICANON parameter in RSOptions (default value: 0) is to switch the RS232 input into canonical mode. In canonical mode, the input is processed in units of lines.
    • A line is delimited by a newline character (ASCII 10) or an end-of-file character (ASCII 255).
    • A read request will not return until an entire line has been sent.
    • Even if several lines are available, at most one line is returned in reply to a read request.

  • Activate the RS232 object by setting its enabled property to true.
    set enabled of the_RS to true
  • To read incoming bytes, read the contained data property of the RS232 object. If no string is available, the read instruction returns an error.
    try
        set the_input to contained data of the_RS
    on error
        -- no input available
    end try
  • To send bytes, set the contained data property of the RS232 to the string to send.
    set contained data of the_RS to "HELLO"
  • If you set the want idle property of the RS232 object to true, then the RS232 polls for input, with a time interval as specified in seconds by the idle delay property (1.0 by default, for a faster responsiveness set the idle delay property to a smaller value such as 0.01).
  • When some input is available Smile will automatically send the following event to the RS232 object.
    notify the_object from the_sender with data the_string
    the_object is the object being notified, the RS232 itself by default
    the_sender is the object which is responsible for the notification, here the RS232
    the_string is the received string (the characters available, or a full line in canonical mode).
  • In canonical mode, each received line triggers one notify event.
  • To handle the notify event, provide the script of the RS232 object with a notify handler (use EditObjectScript(the_object) to edit the script of a faceless object), or provide a notify handler in the global context (for instance, in a text file in Class scripts/Context additions/).
  • You can redirect the notify event by setting the RS232's target object property to some other object of Smile such as a window or a dialog (not to a script, though). By default or when target object is undefined or contains an invalid reference, the notify event is sent to the RS232 itself.
  • If needed, you can access the whole set of flags as documented in man termios: use the «class Term» property of the RS232. For instance this is how you would customize the EOL character used in the canonical behavior.
    You can read and write «class Term», like in the example below.
    set «class Term» of the_RS to {0, 0, 56064, 0, "□ˇˇ□□□ˇ□□□□□□□□ˇˇ", 19200, 19200}
    «class Term» is a list of 7 items: four integers (flags), then one string (the control characters), then two integers (input and output speed). Their exact correspondance with man termios is as follows.
        tcflag_t    c_iflag;    /* input flags */
        tcflag_t    c_oflag;    /* output flags */
        tcflag_t    c_cflag;    /* control flags */
        tcflag_t    c_lflag;    /* local flags */
        cc_t        c_cc[NCCS];    /* control chars */
        speed_t        c_ispeed;    /* input speed */
        speed_t        c_ospeed;    /* output speed */
Version française
Copyright ©2008 Paris, Satimage