Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You don't specify which Unix you are using, so below I'm posting some Linux production code I use.</p> <p>Pleae note below code is a class method so ignore any external (ie undeclared) references.</p> <p>Steps are as follows - </p> <p>Configure your termio structure, this is where you set any needed flags etc (ie the step you accomplished using zterm. The termio settings below configure the port to 8 databits, 1 stopbit and no parity (8-n-1). Also the port will be in "raw" (as opposed to cooked) mode so its a character stream, text isn't framed into lines etc The baud constants match the actual value, ie for 56700 baud you use "57600".</p> <p>The timing parameters mean that characters are returned from the device as soon as they are available.</p> <p>Once you have your termainal parameters set, you open the device (using POSIX open()), and then can use tcgetattr/tcsetattr to configure the device via the fd.</p> <p>At this point you can read/write to the device using the read()/write() system calls. </p> <p>Note that in the below example read() will block if no data is available so you may want to use select()/poll() if blocking is undesirable.</p> <p>Hope that helps.</p> <pre><code>termios termio tcflag_t baud_specifier; //reset device state... memset (&amp;termio, 0, sizeof (termios)); read_buffer.clear(); //get our boad rate... if (!(baud_specifier = baud_constant (baud))) { ostringstream txt; txt &lt;&lt; "invalid baud - " &lt;&lt; baud; device_status_msg = txt.str(); status = false; return (true); } //configure device state... termio.c_cflag = baud_specifier | CS8 | CLOCAL | CREAD; //do we want handshaking? if (rtscts) { termio.c_cflag |= CRTSCTS; } termio.c_iflag = IGNPAR; termio.c_oflag = 0; termio.c_lflag = 0; //com port timing, no wait between characters and read unblocks as soon as there is a character termio.c_cc[VTIME] = 0; termio.c_cc[VMIN] = 0; //open device... if ((fd = open (device.c_str(), O_RDWR | O_NOCTTY)) == -1) { ostringstream txt; txt &lt;&lt; "open(\"" &lt;&lt; device &lt;&lt; "\") failed with " &lt;&lt; errno &lt;&lt; " - " &lt;&lt; std_error_msg (errno); device_status_msg = txt.str(); status = false; return (true); } //keep a copy of curret device state... if (tcgetattr (fd, &amp;old_termio) == -1) { ostringstream txt; txt &lt;&lt; "tcgetattr() failed with " &lt;&lt; errno &lt;&lt; " - " &lt;&lt; std_error_msg (errno); device_status_msg = txt.str(); status = false; return (true); } //flush any unwanted bytes if (tcflush (fd, TCIOFLUSH) == -1) { ostringstream txt; txt &lt;&lt; "tcflush() failed with " &lt;&lt; errno &lt;&lt; " - " &lt;&lt; std_error_msg (errno); device_status_msg = txt.str(); status = false; return (true); } //apply our device config... if (tcsetattr (fd, TCSANOW, &amp;termio) == -1) { ostringstream txt; txt &lt;&lt; "tcsetattr() failed with " &lt;&lt; errno &lt;&lt; " - " &lt;&lt; std_error_msg (errno); device_status_msg = txt.str(); status = false; return (true); } node_log_f ("successfully initialised device %s at %i baud", "open_device()", device.c_str(), baud); status = true; return (true); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload