File: write.ck

package info (click to toggle)
chuck 1.5.5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 41,260 kB
  • sloc: cpp: 124,539; ansic: 35,893; javascript: 2,111; yacc: 609; makefile: 457; python: 174; perl: 86
file content (50 lines) | stat: -rw-r--r-- 1,077 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// list serial devices
SerialIO.list() @=> string list[];
// no serial devices available
if( list.size() == 0 )
{
    cherr <= "no serial devices available\n";
    me.exit(); 
}
// print list of serial devices
chout <= "available devices\n";
for( int i; i < list.size(); i++ )
{
    chout <= i <= ": " <= list[i] <= IO.newline();
}

// parse first argument as device number
0 => int device;
if( me.args() ) me.arg(0) => Std.atoi => device;

if( device >= list.size() )
{
    cherr <= "serial device #" <= device <= "not available\n";
    me.exit(); 
}

// open serial device
SerialIO cereal;
if( !cereal.open(device, SerialIO.B9600, SerialIO.ASCII) )
{
    chout <= "unable to open serial device '" <= list[device] <= "'\n";
    me.exit();
}

// pause to let serial device finish opening
2::second => now;

// infinite time-loop
while( true )
{
    // write to serial device
    cereal <= "hi!\n";
    chout <= "<= hi!\n";
    
    // read one line
    cereal.onLine() => now;
    chout <= "=> " <= cereal.getLine() <= IO.nl();
    
    // advance time
    1::second => now;
}