| 
 | 
續上面的CODE  
------------------------------------- 
void CUSB_I2C_SRF08Dlg::OnTimer(UINT nIDEvent)  
{ 
static int x=0; 
CString s; 
DWORD n; 
static int search_addr=0xe0; 
char cmd[10]; 
static int idx=1, dir=0, scan=0, old_idx=1, cm02_ver; 
BYTE sbuf[100]; 
 
        if(CommPort==0) { 
                SetDlgItemText( IDC_MSG, "Set Com Port" ); 
        }         
        else if(cm02_ver==0) { 
                sbuf[0] = CM01_CMD;                        // send read battery command 
                sbuf[1] = VERSION; 
                sbuf[2] = 0x00; 
                sbuf[3] = 0x00; 
            WriteFile(hCom, &sbuf, 4, &n, NULL); 
                ReadFile(hCom, &sbuf, 1, &n, NULL); 
                cm02_ver = sbuf[0]; 
                s.Format("%i", sbuf[0]); 
                SetDlgItemText( IDC_USB_I2C_VER, s ); 
        } 
        else if(Addr==0) {                        // find srf08 
                sbuf[0] = I2C_CMD;                        // send sonar read command 
                sbuf[1] = search_addr+1; 
                sbuf[2] = 0x00; 
                sbuf[3] = 0x01; 
            WriteFile(hCom, &sbuf, 4, &n, NULL); 
                ReadFile(hCom, &sbuf, 1, &n, NULL); 
                if(sbuf[0]<20) { 
                        Addr = search_addr; 
                        s.Format("0x%02X ", Addr); 
                        SetDlgItemText( IDC_SRF08_ADDR, s ); 
                        SetDlgItemText( IDC_MSG, "SRF Found" ); 
//                        ((CButton*)GetDlgItem(IDC_UPDATE))->EnableWindow(FALSE); 
                } 
                else { 
                        search_addr += 2; 
                        if(search_addr>0xfe) search_addr = 0xe0; 
                        s.Format("0x%02X", search_addr); 
                        SetDlgItemText( IDC_SRF08_ADDR, s ); 
                        SetDlgItemText( IDC_MSG, "Searching" ); 
                } 
                Invalidate(TRUE); 
        } 
        else { 
                sbuf[0] = I2C_CMD;                        // send sonar read command 
                sbuf[1] = Addr+1; 
                sbuf[2] = 0x00; 
                sbuf[3] = 0x04; 
            WriteFile(hCom, &sbuf, 4, &n, NULL); 
 
                ReadFile(hCom, &sbuf, 4, &n, NULL); 
                s.Format("%i", sbuf[0]); 
                SetDlgItemText( IDC_SRF08_VER, s ); 
                if(sbuf[0]>20) Addr=0; 
 
                s.Format("%i ", sbuf[1]); 
                SetDlgItemText( IDC_SRF08_LIGHT, s ); 
 
                n = sbuf[2]<<8; 
                n |= sbuf[3]; 
                s.Format("%i", n); 
                SetDlgItemText( IDC_SRF08_US, s ); 
                s.Format("%i", n/58); 
                SetDlgItemText( IDC_SRF08_CM, s ); 
                s.Format("%i", n/148); 
                SetDlgItemText( IDC_SRF08_INCH, s ); 
 
                sbuf[0] = I2C_CMD;                        // send gain limit 
                sbuf[1] = Addr; 
                sbuf[2] = 0x01; 
                sbuf[3] = 0x01; 
                sbuf[4] = 20; 
            WriteFile(hCom, &sbuf, 5, &n, NULL); 
                ReadFile(hCom, &sbuf, 1, &n, NULL); 
 
                sbuf[0] = I2C_CMD;                        // send sonar rangeing (uS) command 
                sbuf[1] = Addr; 
                sbuf[2] = 0x00; 
                sbuf[3] = 0x01; 
                sbuf[4] = 0x52; 
            WriteFile(hCom, &sbuf, 5, &n, NULL); 
                ReadFile(hCom, &sbuf, 1, &n, NULL); 
 
/*                sbuf[0] = I2CD_CMD;                        // send direct to chip write command 
                sbuf[1] = 0x40; 
                if((x&3)==3) sbuf[2] = 0xfe; 
                else sbuf[2] = 0xff; 
                x++; 
            WriteFile(hCom, &sbuf, 3, &n, NULL); 
                ReadFile(hCom, &sbuf, 1, &n, NULL); 
 
                sbuf[0] = I2CD_CMD;                        // send direct to chip read command 
                sbuf[1] = 0x41; 
            WriteFile(hCom, &sbuf, 2, &n, NULL); 
                ReadFile(hCom, &sbuf, 1, &n, NULL); 
 
                s.Format("0x%x ", sbuf[0]); 
                SetDlgItemText( IDC_SRF08_LIGHT, s ); 
*/ 
        } 
        CDialog::OnTimer(nIDEvent); 
} |   
 
 
 
 |