Debian Codegraph @

examples/serial/write-bytes.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.BINARY) )
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 // ASCII string: hi!\n
38 [ 'h', 'i', '!', '\n' ] @=> int bytes[];
39
40 // infinite time-loop
41 while( true )
42 {
43 // write to serial device
44 cereal.writeBytes( bytes );
45
46 // read one line
47 int byte;
48 string line;
49 while( byte != '\n' )
50 {
51 cereal.onByte() => now;
52 cereal.getByte() => byte;
53
54 line + " " => line;
55 line.setCharAt( line.length()-1, byte );
56 }
57
58 // print
59 chout <= "=> " <= line;
60
61 // advance time
62 1::second => now;
63 }