// Filename : C328_C_Demo.cpp // Version : 1.0 // Description : Function Call To communicate with C328: // Send Sync Commend to C328 // Receive ACK from C328 // Send Ack to C328 // Status : Stable. // Created at : 2004-3 // Copyright (c) COMedia Ltd. // Note: To use these function call, users must initialize the COM Port first typedef struct comedia_CMD { WORD ID; // Command ID BYTE Parameter1; // Parameter1 BYTE Parameter2; // Parameter2 BYTE Parameter3; // Parameter3 BYTE Parameter4; // Parameter4 } comedia_c328_CMD; BOOL Send_SYNC_Cmd(); BOOL Send_Cmd(comedia_c328_CMD Cmd); BOOL Wait_For_ACK(comedia_c328_CMD *pRet_Resp_Cmd, DWORD dwTimeOut); BOOL Send_ACK_Cmd(); const DWORD c328_CMD_SYNC= 0x0DAA; const DWORD c328_CMD_ACK= 0x0EAA; //----------------------------------------------------------------------------------------------- BOOL Send_SYNC_Cmd() // use this function to connect with c328. if success ,return ture,else return false. { BOOL bRet; comedia_c328_CMD SendCmd, ReceivedCmd; int i = 0; while(1) { i++; if(i > 60) // you can edit the max times of sending sync command { bRet = FALSE; break; } SendCmd.ID = c328_CMD_SYNC; // send SYNC command SendCmd.Parameter1 = 0x00; SendCmd.Parameter2 = 0x00; SendCmd.Parameter3 = 0x00; SendCmd.Parameter4 = 0x00; bRet = Send_Cmd(SendCmd); if(!bRet) { break; } Sleep(50); bRet = Wait_For_ACK(&ReceivedCmd, 2); // read ACK command if(!bRet || ReceivedCmd.ID != c328_CMD_ACK ||ReceivedCmd.Parameter1 != (c328_CMD_SYNC & 0xFF00) >> 8) { continue; } bRet = Wait_For_ACK(&ReceivedCmd, 2); // read SYNC command if(!bRet || ReceivedCmd.ID != c328_CMD_SYNC) { continue; } bRet = Send_ACK_Cmd(); // send ACK command break; } Sleep(50); return bRet; } BOOL Send_ACK_Cmd() // use this function to send ACK command { BOOL bRet; comedia_c328_CMD SendCmd; SendCmd.ID = c328_CMD_ACK; SendCmd.Parameter1 = 0; SendCmd.Parameter2 = 0; SendCmd.Parameter3 = 0; SendCmd.Parameter4 = 0; bRet = Send_Cmd(SendCmd); Sleep(1); return bRet; } BOOL Send_Cmd(comedia_c328_CMD Cmd) // use this function to send command { BOOL bRet; comedia_c328_CMD *pCmd; DWORD dwBytesWritten; pCmd = &Cmd; bRet = WriteFile(hComm, pCmd, sizeof(comedia_c328_CMD), &dwBytesWritten, NULL); if(!bRet || dwBytesWritten != sizeof(comedia_c328_CMD)) { bRet=false; } return bRet; } BOOL Wait_For_ACK(comedia_c328_CMD *pRet_Resp_Cmd, DWORD dwTimeOut) // use this function to get ACK command { BOOL bRet; BYTE Temp_Cmd_Buff[6] = {0, 0, 0, 0, 0, 0}; DWORD i; DWORD dwTotalBytesNumberToRead = 6; DWORD dwBytesNumberRead = 0; DWORD dwBytesNumberToRead; DWORD dwTotalBytesNumberRead = 0; pRet_Resp_Cmd->ID = 0x00AA; dwBytesNumberToRead = dwTotalBytesNumberToRead; if(dwTimeOut == 0) { bRet = ReadFromDevice(Temp_Cmd_Buff, dwBytesNumberToRead, &dwBytesNumberRead); dwTotalBytesNumberRead += dwBytesNumberRead; } else { i = 0; while((i < dwTimeOut) && (dwTotalBytesNumberRead < dwTotalBytesNumberToRead)) { Sleep(1); bRet = ReadFromDevice(Temp_Cmd_Buff + dwTotalBytesNumberRead, dwBytesNumberToRead, &dwBytesNumberRead); if(!bRet) { break; } if(dwBytesNumberRead != 0) { dwTotalBytesNumberRead += dwBytesNumberRead; dwBytesNumberToRead -= dwBytesNumberRead; } i++; } } if(!bRet) { return FALSE; } if(dwTotalBytesNumberRead != dwTotalBytesNumberToRead) { return FALSE; } if(Temp_Cmd_Buff[0] == 0xAA && Temp_Cmd_Buff[1] >= 0x01 && Temp_Cmd_Buff[1] <= 0x0F) { bRet = TRUE; memcpy(pRet_Resp_Cmd, Temp_Cmd_Buff, 6); } else { bRet = FALSE; } return bRet; }