//--------------------------------------------------------------------------- // The main program that performs the operations on switches // int main(int argc, char **argv) { short test; //info byte data short clear=0; //used to clear the button short done; //to tell when the user is done SwitchProps sw; //used to set Channel A and B uchar SwitchSN[MAXDEVICES][8]; //the serial number for the devices int num; //for the number of devices present int i,j,n,count,result; //loop counters and indexes char out[140]; //used for output of the info byte data int portnum=0; long select; //inputed number from user //---------------------------------------- // Introduction header printf("\n/---------------------------------------------\n"); printf(" Switch - V3.00\n" " The following is a test to excersize the \n" " setting of the state in a DS2406.\n"); printf(" Press any CTRL-C to stop this program.\n\n"); // check for required port name if (argc != 2) { printf("1-Wire Net name required on command line!\n" " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" " "(Linux DS2480),\"1\" (Win32 TMEX)\n"); exit(1); } // attempt to acquire the 1-Wire Net if ((portnum = owAcquireEx(argv[1])) < 0) { OWERROR_DUMP(stdout); exit(1); } // success printf("Port opened: %s\n",argv[1]); // this is to get the number of the devices and the serial numbers num = FindDevices(portnum, &SwitchSN[0], SWITCH_FAMILY, MAXDEVICES); // setting up the first print out for the frist device owSerialNum(portnum, SwitchSN[0], FALSE); printf("\n"); n=0; if(owAccess(portnum)) { // loop while not done do { test = ReadSwitch12(portnum, clear); for(i=7; i>=0; i--) printf("%02X", SwitchSN[n][i]); printf("\n"); count = SwitchStateToString12(test, out); printf("%s", out); // print menu select = 1; if (!EnterNum("\n\n(1) Display the switch Info\n" "(2) Clear activity Latches\n" "(3) Set Flip Flop(s) on switch\n" "(4) Select different device\n" "(5) Quit\n" "Select a Number", 1, &select, 1, 5)) break; printf("\n\n"); // do something from the menu selection clear = FALSE; switch(select) { case 1: // Display the switch Info done = FALSE; break; case 2: // Clear activity Latches clear = TRUE; done = FALSE; break; case 3: // Set Flip Flop(s) on switch select = 0; if (EnterNum("Channel A Flip Flop (1 set, 0 clear)", 1, &select, 0, 1)) { sw.Chan_A = (uchar)select; if(test & 0x40) { if (EnterNum("Channel B Flip Flop (1 set, 0 clear)", 1, &select, 0, 1)) { sw.Chan_B = (uchar)select; done = FALSE; } } else { sw.Chan_B = 0; done = FALSE; } printf("\n"); } // proceed to setting switch state if not done if (!done) { // loop to attempt to set the switch (try up to 5 times) count = 0; do { result = SetSwitch12(portnum, SwitchSN[n], sw); // if failed then delay to let things settle if (!result) msDelay(50); } while ((count++ < 5) && (result != TRUE)); // if could not set then show error if (!result) printf("Could not set Switch!\n"); } break; case 4: // Switch Devices // print the device list for(j=0; j < num; j++) { printf("%d ", j+1); for(i=7; i>=0; i--) { printf("%02X", SwitchSN[j][i]); } printf("\n"); } printf("\n"); // select the device select = 0; if (EnterNum("Select Device",1, &select, 1, num)) { n = (int)(select - 1); owSerialNum(portnum, SwitchSN[n], FALSE); done = FALSE; } break; case 5: // Done done = TRUE; break; default: break; } } while (!done); } //One Wire Access owRelease(portnum); printf("Closing port %s.\n", argv[1]); exit(0); return 0; }
//-------------------------------------------------------------------------- // This is the begining of the program that tests the different Channels int main() //short argc, char **argv) { char return_msg[128]; //returned message from 1-wire operations int i,j,k,n; //loop counters short test=0; //info byte data short clear=0; //used to clear the button SwitchProps sw; //used to set Channel A and B uchar SwitchSN[MAXDEVICES][8]; //the serial numbers for the devices int num; //for the number of devices present int ch; //inputed character from user char out[140]; //used for output of the info byte data int portnum=0; //---------------------------------------- // Introduction header printf("\n/---------------------------------------------\n"); printf(" swtest - V2.00\n" " The following is a test to excersize the\n" " different channels on the DS2406.\n"); printf(" Press any CTRL-C to stop this program.\n\n"); // check for required port name if (argc != 2) { printf("1-Wire Net name required on command line!\n" " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" " "(Linux DS2480),\"1\" (Win32 TMEX)\n"); exit(1); } // attempt to acquire the 1-Wire Net if (!owAcquire(portnum, argv[1], return_msg)) { printf("%s",return_msg); exit(1); } // success printf("%s",return_msg); // this is to get the number of the devices and the serial numbers num = FindDevices(portnum, &SwitchSN[0], SWITCH_FAMILY, MAXDEVICES); // setting up the first print out for the frist device owSerialNum(portnum, SwitchSN[0], FALSE); j=1; n=0; do { // This is for after the different combinations of channels // have been tested to reset to a different device to be tested. if( ((test & 0x40) && (j==5)) || ((!(test & 0x40)) && (j==3)) ) { printf("\n\n"); for(k=0; k < num; k++) { printf("%d ", k+1); for(i=7; i>=0; i--) { printf("%02X", SwitchSN[k][i]); } printf("\n"); } printf("%d To quit or any other key.\n", k+1); printf("\n"); printf("Pick a device\n"); ch = getkeystroke(); n = 0; n = (10*n + (ch - '0')) - 1; if( (n>num-1) || (n<0) ) { n = 0; //used to finish off the loop break; } owSerialNum(portnum, SwitchSN[n], FALSE); j = 1; } printf("\n"); test = ReadSwitch12(portnum,clear); // This looks at the info byte to determine if it is a // two or one channel device. if(test & 0x40) { switch(j) { case 1: sw.Chan_A = 0; sw.Chan_B = 0; break; case 2: sw.Chan_A = 0; sw.Chan_B = 1; break; case 3: sw.Chan_A = 1; sw.Chan_B = 0; break; case 4: sw.Chan_A = 1; sw.Chan_B = 1; break; default: sw.Chan_A = 1; sw.Chan_B = 1; j=0; break; } } else { switch(j) { case 1: sw.Chan_B = 0; sw.Chan_A = 0; break; case 2: sw.Chan_B = 0; sw.Chan_A = 1; break; default: sw.Chan_B = 0; sw.Chan_A = 1; j = 0; break; } } if(!SetSwitch12(portnum, SwitchSN[n], &sw)) { msDelay(50); if(SetSwitch12(portnum, SwitchSN[n], &sw)) msDelay(50); else printf("Switch not set\n"); } test = ReadSwitch12(portnum,clear); printf("\n"); for(i=7; i>=0; i--) { printf("%02X", SwitchSN[n][i]); } printf("\n"); SwitchStateToString12(test, out); printf("%s", out); j++; } while(1); // release the 1-Wire Net owRelease(portnum,return_msg); printf("%s",return_msg); exit(0); return 0; }
// ------------------------------------------------------------------------- // SUBROUTINE - GetDir // // This routine looks at the DS2401 serial numbers or the DS2450 volts to // determine which direction the wind is blowing. // // Returns: The direction in a 0-15 order and 16 for no DS2401s found // int GetDir(int portnum, WeatherStruct *wet) { SwitchProps st; uchar temp_dir[MAXDIR][8]; int i,j; int numdir = 0; int firstmatch = 0, secondmatch = 0; int firstindex = 0; int found = TRUE; int ret = 16; float prslt[4]; temp_dir[0][0] = 0x00; if(wet->weather_a) { st.Chan_A = FALSE; st.Chan_B = TRUE; if(SetSwitch12(portnum, &wet->dsdir[0], st)) { numdir = FindDevices(portnum, &temp_dir[0], DIR_FAMILY, MAXDIR); } else printf("Error on setting up the switch\n"); if(numdir == 0) ret = 16; if(numdir == 2) { for(i=0; i<8; i++) { for(j=0; j<8; j++) if(temp_dir[0][j] != wet->ds2401[i][j]) { found = FALSE; break; } else found = TRUE; if(found) { firstindex = i; firstmatch = conv[i]; found = FALSE; break; } } for(i=0; i<8; i++) { if(i != firstindex) { for(j=0; j<8; j++) { if(temp_dir[1][j] != wet->ds2401[i][j]) { found = FALSE; break; } else found = TRUE; } } if(found) { secondmatch = conv[i]; break; } } if(((firstmatch == 0) || (secondmatch == 0)) && ((firstmatch == 14) || (secondmatch == 14))) ret = 15; else ret = (firstmatch+secondmatch)/2; } if(numdir == 1) { for(i=0; i<8; i++) { found = TRUE; for(j=0; j<8; j++) if(temp_dir[0][j] != wet->ds2401[i][j]) found = FALSE; if(found) { ret = conv[i]; break; } } } st.Chan_A = FALSE; st.Chan_B = FALSE; if(!SetSwitch12(portnum, &wet->dsdir[0], st)) { msDelay(10); if(!SetSwitch12(portnum, &wet->dsdir[0], st)) { printf("Failed to close channel B\n"); } } } else if(wet->weather_b) { // read wind direction for the DS2450 weather station if(DoAtoDConversion(portnum, FALSE, &wet->dsdir[0])) { if(ReadAtoDResults(portnum, FALSE, &wet->dsdir[0], &prslt[0], &wet->ctrl[0])) { for(i=0; i<16; i++) { if( ((prslt[0] <= (float)conv_table[i][0]+0.25) && (prslt[0] >= (float)conv_table[i][0]-0.25)) && ((prslt[1] <= (float)conv_table[i][1]+0.25) && (prslt[1] >= (float)conv_table[i][1]-0.25)) && ((prslt[2] <= (float)conv_table[i][2]+0.25) && (prslt[2] >= (float)conv_table[i][2]-0.25)) && ((prslt[3] <= (float)conv_table[i][3]+0.25) && (prslt[3] >= (float)conv_table[i][3]-0.25)) ) { ret = i; break; } } } else { printf("\n"); printf("\nError reading channel, verify device present: %d\n", (int)owVerify(portnum, FALSE)); } } } return ret; }