Debian Codegraph @

examples/serial/write.ck

raw
1 // list serial devices
2 SerialIO.list() @=> string list[];
3 // no serial devices available
4 if( list.size() == 0 )
5 {
6 cherr <= "no serial devices available\n";
7 me.exit();
8 }
9 // print list of serial devices
10 chout <= "available devices\n";
11 for( int i; i < list.size(); i++ )
12 {
13 chout <= i <= ": " <= list[i] <= IO.newline();
14 }
15
16 // parse first argument as device number
17 0 => int device;
18 if( me.args() ) me.arg(0) => Std.atoi => device;
19
20 if( device >= list.size() )
21 {
22 cherr <= "serial device #" <= device <= "not available\n";
23 me.exit();
24 }
25
26 // open serial device
27 SerialIO cereal;
28 if( !cereal.open(device, SerialIO.B9600, SerialIO.ASCII) )
29 {
30 chout <= "unable to open serial device '" <= list[device] <= "'\n";
31 me.exit();
32 }
33
34 // pause to let serial device finish opening
35 2::second => now;
36
37 // infinite time-loop
38 while( true )
39 {
40 // write to serial device
41 cereal <= "hi!\n";
42 chout <= "<= hi!\n";
43
44 // read one line
45 cereal.onLine() => now;
46 chout <= "=> " <= cereal.getLine() <= IO.nl();
47
48 // advance time
49 1::second => now;
50 }