/*---------------------------------------------------------------------- * Read the switch information byte. */ short ReadSwitchInfo(long session_handle, short ClearActivity) { short flag,rt=-1; unsigned char CRCByte; /* access and verify it is there */ flag = TMAccess(session_handle,&state_buffer); if (flag == 1) { /* reset CRC */ CRC16 = 0; /* channel access command */ flag = TMTouchByte(session_handle,0xF5); CRCByte = 0xF5; CRC16 = TMCRC(1, &CRCByte, (ushort)CRC16, 1); /* control bytes */ if (ClearActivity) { TMTouchByte(session_handle,0xD5); CRCByte = 0xD5; CRC16 = TMCRC(1, &CRCByte, (ushort)CRC16, 1); } else { TMTouchByte(session_handle,0x55); CRCByte = 0x55; CRC16 = TMCRC(1, &CRCByte, (ushort)CRC16, 1); } TMTouchByte(session_handle,0xFF); CRCByte = 0xFF; CRC16 = TMCRC(1, &CRCByte, (ushort)CRC16, 1); /* read the info byte */ rt = TMTouchByte(session_handle,0xFF); CRCByte = (unsigned char) rt; CRC16 = TMCRC(1, &CRCByte, (ushort)CRC16, 1); /* read a dummy read byte and CRC16 */ CRCByte = (unsigned char) TMTouchByte(session_handle,0xFF); CRC16 = TMCRC(1, &CRCByte, (ushort)CRC16, 1); CRCByte = (unsigned char) TMTouchByte(session_handle,0xFF); CRC16 = TMCRC(1, &CRCByte, (ushort)CRC16, 1); CRCByte = (unsigned char) TMTouchByte(session_handle,0xFF); CRC16 = TMCRC(1, &CRCByte, (ushort)CRC16, 1); /* check result */ if (CRC16 != 0xB001) rt = -1; } else rt = -1; return rt; }
//-------------------------------------------------------------------------- // The 'owAccess' function resets the 1-Wire and sends a MATCH Serial // Number command followed by the current SerialNum code. After this // function is complete the 1-Wire device is ready to accept device-specific // commands. // // 'portnum' - number 0 to MAX_PORTNUM-1. This number is provided to // indicate the symbolic port number. // // Returns: TRUE (1) : reset indicates present and device is ready // for commands. // FALSE (0): reset does not indicate presence or echos 'writes' // are not correct. // SMALLINT owAccess(int portnum) { return (TMAccess(SessionHandle[portnum], StateBuffer[portnum]) == 1); }
/*---------------------------------------------------------------------- * Set the flip flops to the 'ffstate' . */ short SetFlipFlop(short ffstate) { short printmsg=1,flag,rt=0,cnt=0,done=0; long session_handle; unsigned char CRCByte; // loop to get a session handle and to a temperature conversion */ do { /* attempt to get a session */ session_handle = TMExtendedStartSession(PortNum,PortType,NULL); if (session_handle > 0) { /* access and verify it is there */ if (TMAccess(session_handle,&state_buffer) == 1) { /* reset CRC */ CRC16 = 0; /* write status command */ flag = TMTouchByte(session_handle,0x55); CRCByte = 0x55; CRC16 = TMCRC(1, &CRCByte, 0, 1); /* send address */ TMTouchByte(session_handle,0x07); CRCByte = 0x07; CRC16 = TMCRC(1, &CRCByte, (ushort)CRC16, 1); TMTouchByte(session_handle,0x00); CRCByte = 0x00; CRC16 = TMCRC(1, &CRCByte, (ushort)CRC16, 1); /* write that status */ TMTouchByte(session_handle,ffstate); CRCByte = (unsigned char) ffstate; CRC16 = TMCRC(1, &CRCByte, (ushort)CRC16, 1); /* crc */ flag = TMTouchByte(session_handle, 0xFF); CRCByte = (unsigned char) flag; CRC16 = TMCRC(1, &CRCByte, (ushort)CRC16, 1); flag = TMTouchByte(session_handle, 0xFF); CRCByte = (unsigned char) flag; CRC16 = TMCRC(1, &CRCByte, (ushort)CRC16, 1); /* check result */ if (CRC16 == 0xB001) done = TRUE; else cnt++; } /* end the session if one is open */ TMEndSession(session_handle); } else if (session_handle == 0) { /* check if need to print the waiting message */ if (printmsg) { printmsg = FALSE; printf("\nWaiting to get access to the 1-Wire network\n\n"); } } } while (!done && (cnt < 10)); return done; }