c7bc879b530e0d8d2a8245d84a3242b25850eccb
[fw/sdcc] / device / examples / ds390 / ow390 / tstfind.c
1 //---------------------------------------------------------------------------
2 // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved.
3 // 
4 // Permission is hereby granted, free of charge, to any person obtaining a 
5 // copy of this software and associated documentation files (the "Software"), 
6 // to deal in the Software without restriction, including without limitation 
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 
8 // and/or sell copies of the Software, and to permit persons to whom the 
9 // Software is furnished to do so, subject to the following conditions:
10 // 
11 // The above copyright notice and this permission notice shall be included 
12 // in all copies or substantial portions of the Software.
13 // 
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
16 // MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
17 // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES 
18 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
20 // OTHER DEALINGS IN THE SOFTWARE.
21 // 
22 // Except as contained in this notice, the name of Dallas Semiconductor 
23 // shall not be used except as stated in the Dallas Semiconductor 
24 // Branding Policy. 
25 //---------------------------------------------------------------------------
26 //
27 //  tstfind.C - Test application to search for all 1-Wire devices on 1-Wire 
28 //              Net.
29 //
30 //  Version: 2.00
31 //
32 //           1.02 -> 1.03  Removed caps in #includes for Linux capatibility
33 //                         Removed "ds2480.h", <windows.h> and <conio.h> 
34 //                           includes because not needed
35 //                         Added "ownet.h" include to define TRUE/FALSE
36 //                         Prompt to search again 
37 //                         Changed to use Acquire/Release 1-Wire Net functions
38 //           1.03 -> 2.00  Changed 'MLan' to 'ow'. Added support for 
39 //                         multiple ports.  Don't stop loop at end of each
40 //                         search round.  
41  
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include "ownet.h"
45
46 // local funcitons
47 static void PrintSerialNum(int);
48
49 // tini hack
50 int argc=2;
51 char *argv[]={__FILE__, "exow"};
52
53 //----------------------------------------------------------------------
54 //  Main for tstfind
55 //
56 int main() //short argc, char **argv)
57 {
58    int rslt,cnt;
59    char return_msg[128];
60    int portnum=0;
61   
62    // check for required port name
63    if (argc != 2)
64    {
65       printf("1-Wire Net name required on command line!\n"
66              " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" "
67              "(Linux DS2480),\"1\" (Win32 TMEX)\n");
68       exit(1);
69    }
70
71    // attempt to acquire the 1-Wire Net
72    if (!owAcquire(portnum,argv[1],return_msg))
73    {  
74       printf("%s",return_msg);
75       exit(1);
76    }
77
78    // success
79    printf("%s",return_msg);
80
81    //----------------------------------------
82    // Introduction
83    printf("\n/---------------------------------------------\n");
84    printf("  Loop to find all iButton on 1-Wire Net.\n\n");
85
86    do
87    {
88       printf("-------------------- Start of search\n");
89       cnt = 0;
90
91       // find the first device (all devices not just alarming)
92       rslt = owFirst(portnum, TRUE, FALSE);
93       while (rslt)
94       {
95          // print the device number
96          cnt++;
97          printf("(%d) ",cnt);
98
99          // print the Serial Number of the device just found
100          PrintSerialNum(portnum);
101
102          // find the next device
103          rslt = owNext(portnum, TRUE, FALSE);
104       }
105       printf("-------------------- End of search\n\n");
106
107    }
108    while (!key_abort());
109
110    // release the 1-Wire Net
111    owRelease(portnum,return_msg);
112    printf("%s",return_msg);
113    exit(0);
114
115    return 0;
116 }
117
118 //----------------------------------------------------------------------
119 //  Read and print the Serial Number.
120 //
121 void PrintSerialNum(int portnum)
122 {
123    uchar serial_num[8];
124    int i;
125
126    owSerialNum(portnum,serial_num,TRUE);
127    for (i = 7; i >= 0; i--)
128       printf("%02X",serial_num[i]);
129    printf("\n");
130 }