123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- // SerialWatch.cpp : 定义 DLL 应用程序的导出函数。
- //
- #include "stdafx.h"
- #include "SerialWatch.h"
- #include "SynSerial.h"
- CSynSerial g_TVPort;
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // 唯一的应用程序对象
- CWinApp theApp;
- using namespace std;
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- int nRetCode = 0;
- // 初始化 MFC 并在失败时显示错误
- if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
- {
- // TODO: 更改错误代码以符合您的需要
- _tprintf(_T("错误: MFC 初始化失败\n"));
- nRetCode = 1;
- }
- else
- {
- // TODO: 在此处为应用程序的行为编写代码。
- if ( OpenTVPort(9, 115200, 8, NOPARITY, 1) ) {
- #ifdef _DEBUG
- printf("打开串口成功\n");
- #endif
- // 开始监听;
- StartMonitor();
- //RTK_USBUpgrade(0x1B); // ESC键升级;
- //RTK_USBUpgrade(0x09); // TAB键升级;
- //Sleep(30000);
- system("pause");
- CloseTVPort();
- }
- }
- system("pause");
- return nRetCode;
- }
- SERIALWATCH_API BOOL OpenTVPort(int nPort, DWORD dwBaudrate, BYTE ByteSize, BYTE Parity, BYTE StopBits)
- {
- // 关闭打开的;
- if ( g_TVPort.IsOpen() )
- g_TVPort.CloseSerialPort();
- g_TVPort.OpenSerialPort(nPort, dwBaudrate, ByteSize, Parity, StopBits, 0, 1000);
- return g_TVPort.IsOpen();
- }
- SERIALWATCH_API BOOL IsOpen()
- {
- return g_TVPort.IsOpen();
- }
- SERIALWATCH_API void StartMonitor()
- {
- if ( g_TVPort.IsOpen() )
- g_TVPort.StartThread();
- }
- SERIALWATCH_API void SendCommand(LPCTSTR lpCommand, int nSendCount, int nSleep)
- {
- if ( !g_TVPort.IsOpen() || !lpCommand)
- return ;
- bool bRet;
- for ( int i = 0; i < nSendCount; i++ ) {
- bRet = g_TVPort.WriteComm((BYTE*)lpCommand, _tcslen((TCHAR*)lpCommand));
- #ifdef _DEBUG
- printf(_T("写串口:%s\n"), bRet ? _T("成功") : _T("失败"));
- #endif
- Sleep(nSleep);
- }
- }
- // 关闭串口;
- SERIALWATCH_API void CloseTVPort()
- {
- g_TVPort.EndofThread();
- if ( g_TVPort.IsOpen() ) {
- g_TVPort.CloseSerialPort();
- }
- }
- SERIALWATCH_API bool WatchWord(LPCTSTR lpWatchWord, int nWatchTime)
- {
- // 设置要监听的单词,由线程去判断;
- if ( lpWatchWord == NULL )
- return false;
- return g_TVPort.FindWord(lpWatchWord);
- }
- SERIALWATCH_API void ClearBuffer()
- {
- g_TVPort.ClearBuffer();
- }
- SERIALWATCH_API bool RTK_USBUpgrade(BYTE byKey)
- {
- bool bRet = false;
- if ( IsOpen() ) {
- // 回车;
- SendCommand(_T("\r"), 5, 500);
- // 判断是否出现console字样;
- if ( WatchWord(_T("console:/")) ) {
- #ifdef _DEBUG
- printf("找到关键字\n");
- #endif
- // 重启;
- SendCommand(_T("reboot\r"), 1);
- if ( WatchWord(_T("Terminated"), 100) ) {
- ClearBuffer();
- while ( TRUE ) {
- // 连续发送150次按键;
- SendCommand((LPCTSTR)&byKey, 150);
- if ( byKey == 0x1B ){ // ESC键;
- if ( WatchWord(_T("Realtek>"), 150) ) {
- #ifdef _DEBUG
- printf("找到关键字\n");
- #endif
- SendCommand(_T("\r"), 2, 100);
- SendCommand(_T("go r\r"), 1, 1000);
- bRet = true;
- break;
- }
- } else if (byKey == 0x09) { // TAB键;
- if ( WatchWord(_T("loader start!"), 150) ) {
- #ifdef _DEBUG
- printf("找到关键字\n");
- #endif
- bRet = true;
- break;
- }
- }
- }
- }
- }
- }
- return bRet;
- }
|