void COTPPancel::OnLockOtp() { // TODO: Add your control notification handler code here OTP_ReadWrite OTPReadWrite = {0}; ReadWriteComReq(); RefreshMainDlgMsgBox("Lock OTP:"); if(MessageBox("Will Lock OTP, confirm ?",NULL,MB_YESNO|MB_ICONWARNING) == IDYES) { if(Write_OTP_FLAG(gComPortInfo.ComID,&OTPReadWrite) == TRUE) { AddInfotoMainDlgMsgBox("Success!"); } else { AddInfotoMainDlgMsgBox("Failed!"); } } else { AddInfotoMainDlgMsgBox("Cancel!"); } CloseComPort(); }
// main int main(int argc, char* argv[]){ ComPortHandle comPort; int go = TRUE; char* dev; char a; a='\0'; dev=&a; if(argc<2){//No port specified at commandline so search for attached devices dev=scandev(); if(strcmp(dev,"")!=0){ //printf("Attempting to open port...%s\n",dev); comPort = OpenComPort(dev); } else{ printf("Failed to find attached device.\n"); return FALSE; } } else{//Open port specified at commandline printf("Attempting to open port...%s\n",argv[1]); comPort = OpenComPort(argv[1]); } if(comPort > 0){ //printf("Connected. \n\n"); /* while(go){//continue until user chooses to exit unsigned int command; command = 0xc1; usleep(500000);//short sleep between commands go=CommandDialog(comPort,command); }*/ unsigned int command; command = 0xd2; CommandDialog(comPort,command); // printf("EXITING\n"); CloseComPort(comPort); } return 0; }
//设置DCB结构体 BOOL CSerialPorts::SetDCBParam() { if(FALSE==CheckPortOpen()) return FALSE; if(FALSE==GetCommState(hCom,&m_dcb)) { AfxMessageBox(_T("串口连接失败,请重新连接!")); CloseComPort(); return FALSE; } switch(m_nBaud) { case 0: m_nBaud=9600; break; case 1: m_nBaud=19200; break; case 2: m_nBaud=38400; break; case 3: m_nBaud=57600; break; case 4: m_nBaud=115200; break; } //m_dcb.BaudRate=m_nBaud; m_dcb.ByteSize=m_nDataBits; switch(m_nParity) { case 0: m_dcb.Parity=NOPARITY; break; case 1: m_dcb.Parity=ODDPARITY; break; case 2: m_dcb.Parity=EVENPARITY;break; } switch(m_nStopBits) { case 0: m_dcb.StopBits=ONESTOPBIT; break; //case 1: // m_dcb.StopBits=ONESSTOPBITS;break; case 2: m_dcb.StopBits=TWOSTOPBITS; break; } m_dcb.StopBits=m_nStopBits; SetCommState(hCom,&m_dcb); return TRUE; }
//设置端口缓冲区大小 BOOL CSerialPorts::SetPortBuffSize() { if(FALSE==CheckPortOpen()) return FALSE; if(FALSE==SetupComm(hCom,m_inputBuff,m_outputBuff)) { AfxMessageBox(_T("串口数据缓冲区设置失败,请重新连接!")); CloseComPort(); return FALSE; } else return TRUE; }
//打开并初始化串口 BOOL CSerialPorts::InitComPortSet(int nCom, int nBaud,int nParity) { //检查串口是否已经打开 if(TRUE==CheckPortOpen()) CloseComPort(); //设置异步读写事件 if(FALSE==SetEventToReadWrite()) AfxMessageBox(_T("串口连接设置失败,请重新连接!")); //设置波特率、串口名和校验方式 m_nBaud=nBaud; m_nParity=nParity; switch(nCom) { case 0: m_cName=_T("COM1"); break; case 1: m_cName=_T("COM2"); break; case 2: m_cName=_T("COM3"); break; case 3: m_cName=_T("COM4"); break; case 4: m_cName=_T("COM5"); break; case 5: m_cName=_T("COM6"); break; case 6: m_cName=_T("COM7"); break; case 7: m_cName=_T("COM8"); break; } //打开端口 hCom=CreateFile(m_cName, //端口名 GENERIC_READ|GENERIC_WRITE, //访问类型:读写 0, //是否共享端口,0为独占 NULL, //默认安全性 OPEN_EXISTING, //只打开而不创建 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, //重叠方式 NULL); //端口打开时不需要,参数为0 if(INVALID_HANDLE_VALUE==hCom) return FALSE; //设置串口缓冲区大小 SetPortBuffSize(); //设置DCB结构体 if(FALSE==SetDCBParam()) return FALSE; //设置超时结构体 SetTimerOut(); //清空串口数据缓冲区 PurgeComm(hCom,PURGE_RXABORT|PURGE_RXCLEAR|PURGE_TXCLEAR|PURGE_TXABORT); return TRUE; }
/* ----------------------------------Main----------------------------------- */ int main(int argc, char* argv[]){ ComPortHandle comPort; int go = TRUE; char* dev; char a; a='\0'; dev=&a; if(argc<2) { //No port specified at commandline so search for attached devices dev=scandev(); if(strcmp(dev,"")!=0) { //printf("Attempting to open port...%s\n",dev); comPort = OpenComPort(dev); } else { printf("Failed to find attached device.\n"); return FALSE; } } else { //Open port specified at commandline printf("Attempting to open port...%s\n",argv[1]); comPort = OpenComPort(argv[1]); } if(comPort > 0) { unsigned int command; command = 0xd2; CommandDialog(comPort,command); CloseComPort(comPort); } return 0; }
/** * * @brief Open device and perform configuration * * @return bool true on success, false if error */ bool Instek8261Serial::InitDevice(void) { bool success = false; if (OpenDevice()) { for (int i = 0; i < 4; i++) { // Try to read version number if (PrimeSerialStream()) { success = true; break; } } } else { CloseComPort(); } if (success) { sprintf(m_buffer, "sens:det:rate M\n"); // printf("%ld ms, Sending cmd: %s", cmdTimer.GetElapsedMillisec(), m_buffer); int length = strlen(m_buffer); SendCmd(m_buffer, length); } return success; }
/******************************************************************************* extern message is called by a calling application *******************************************************************************/ int APIENTRY UpsStatus(HAB hab, ULONG msg, MPARAM mp1, PUPSSTATUS pups) { int retcode = ERROR_BAD_COMMAND; int x; switch(msg) { case cmd_init: memset(&upst, 0, sizeof(UPSSTATUS)); upst.size = sizeof(UPSSTATUS); upst.strucid = STRUCTURE_ID; if(0 != (retcode = CloseComPort())) break; //stops any poll, and inits the various flags if(pups) { upst.msgfile = pups->msgfile; if(upst.msgfile) sprintmsg(infostr, upst.msgfile, "CONNECT1"); pups->infostr = infostr; } if(0 != (retcode = OpenComPort((char*)mp1))) break; threadid = _beginthread(MessageThread, NULL, 0x4000, NULL); DosSleep(1); if(0 != (retcode = waitformessage(hab, 0, NULL))) break; if(0 != (retcode = SendUPSMsg(UPS_Inf, NULL))) break; if(0 != (retcode = waitformessage(hab, 0, NULL))) break; if(0 != (retcode = SendUPSMsg(UPS_Rate, NULL))) break; retcode = waitformessage(hab, 0, pups); break; case cmd_info: if(0 != (retcode = waitformessage(hab, 0, NULL))) break; if(upst.msgfile) sprintmsg(infostr, upst.msgfile, "MEGATEC1", upspr.name, upspr.model, upspr.version, Mix_to_d(upspr.voltage), Mix_to_d(upspr.current), Mix_to_d(upspr.frequency), Mix_to_d(upspr.battery)); break; case cmd_quiet: StopPoll = TRUE; if(0 != (retcode = waitformessage(hab, 0, NULL))) break; if(_flagtest(upst.upsstatus, stat_buzzer) ^ (!mp1)) { if(0 != (retcode = SendUPSMsg(UPS_Quiet, NULL))) break; retcode = waitformessage(hab, 0, pups); break; } else retcode = NO_ERROR; break; case cmd_test: StopPoll = TRUE; if(0 != (retcode = waitformessage(hab, 0, NULL))) break; if(((int)mp1) == -1) retcode = SendUPSMsg(UPS_TestL, NULL); retcode = SendUPSMsg(UPS_Test, mp1); if(0 != retcode) break; StopPoll = FALSE; retcode = waitformessage(hab, 0, pups); break; case cmd_abort: StopPoll = TRUE; if(0 != (retcode = waitformessage(hab, 0, NULL))) break; if(_flagtest(upst.upsstatus, stat_testing)) retcode = SendUPSMsg(UPS_CancTst, NULL); if(0 != (retcode = waitformessage(hab, 0, NULL))) break; if(_flagtest(upst.upsstatus, stat_shutting)) retcode = SendUPSMsg(UPS_Cancel, NULL); if(0 != retcode) break; StopPoll = FALSE; retcode = waitformessage(hab, 0, pups); break; case cmd_shutdown: StopPoll = TRUE; if(0 != (retcode = waitformessage(hab, 0, NULL))) break; if(0 != (retcode = SendUPSMsg(UPS_Shut, mp1))) break; StopPoll = FALSE; retcode = waitformessage(hab, 0, pups); break; case cmd_exit: retcode = CloseComPort(); return retcode; case cmd_poll: retcode = waitformessage(hab, 0, pups); pups->infostr = infostr; break; default: retcode = waitformessage(hab, 0, NULL); break; } StopPoll = FALSE; return retcode; }
/**************************************************************************** * Opens com port * * \param comPort Com port to be opened. * \param baud Baud rate * \param * \return Opens the com port *****************************************************************************/ void CComPort::OpenComPort(unsigned int comPort, unsigned int baud) { CString buff; DWORD bytes_read = 0; // Number of bytes read from port DWORD bytes_written = 0; // Number of bytes written to the port int bStatus; DCB comSettings; // Contains various port settings COMMTIMEOUTS CommTimeouts; if(comPortHandle) { // If com port is already open, close it. CloseComPort(); } buff = comPortStrng[comPort]; // Open COM port if ((comPortHandle = CreateFile(buff, // open com5: GENERIC_READ | GENERIC_WRITE, // for reading and writing 0, // exclusive access NULL, // no security attributes OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) { // error processing code goes here MessageBox(NULL, "Not able to open com port!", NULL, 0); comPortHandle = NULL; } else { // Set timeouts in milliseconds CommTimeouts.ReadIntervalTimeout = 0; CommTimeouts.ReadTotalTimeoutMultiplier = 0; CommTimeouts.ReadTotalTimeoutConstant = 5; // Read time out 5ms. CommTimeouts.WriteTotalTimeoutMultiplier = 1; CommTimeouts.WriteTotalTimeoutConstant = 500; // Write time out 500ms. (void)SetCommTimeouts(comPortHandle,&CommTimeouts); // Set Port parameters. // Make a call to GetCommState() first in order to fill // the comSettings structure with all the necessary values. // Then change the ones you want and call SetCommState(). GetCommState(comPortHandle, &comSettings); baud = baudTable[baud]; comSettings.BaudRate = baud; comSettings.StopBits = ONESTOPBIT; comSettings.ByteSize = 8; comSettings.Parity = NOPARITY; // No Parity comSettings.fParity = FALSE; comSettings.fRtsControl = RTS_CONTROL_ENABLE; // Keep the RTS ON, to trigger bootloader enter bootload mode. bStatus = SetCommState(comPortHandle, &comSettings); if (bStatus == 0) { // error processing code goes here MessageBox(NULL, "Error in setting the COM port!", NULL, 0); CloseComPort(); } } }
/* Destructor */ Instek8261Serial::~Instek8261Serial(void) { CloseComPort(); }