SerialWatch.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SerialWatch.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "stdafx.h"
  4. #include "SerialWatch.h"
  5. #include "SynSerial.h"
  6. CSynSerial g_TVPort;
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #endif
  10. // 唯一的应用程序对象
  11. CWinApp theApp;
  12. using namespace std;
  13. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  14. {
  15. int nRetCode = 0;
  16. // 初始化 MFC 并在失败时显示错误
  17. if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  18. {
  19. // TODO: 更改错误代码以符合您的需要
  20. _tprintf(_T("错误: MFC 初始化失败\n"));
  21. nRetCode = 1;
  22. }
  23. else
  24. {
  25. // TODO: 在此处为应用程序的行为编写代码。
  26. if ( OpenTVPort(9, 115200, 8, NOPARITY, 1) ) {
  27. #ifdef _DEBUG
  28. printf("打开串口成功\n");
  29. #endif
  30. // 开始监听;
  31. StartMonitor();
  32. //RTK_USBUpgrade(0x1B); // ESC键升级;
  33. //RTK_USBUpgrade(0x09); // TAB键升级;
  34. //Sleep(30000);
  35. system("pause");
  36. CloseTVPort();
  37. }
  38. }
  39. system("pause");
  40. return nRetCode;
  41. }
  42. SERIALWATCH_API BOOL OpenTVPort(int nPort, DWORD dwBaudrate, BYTE ByteSize, BYTE Parity, BYTE StopBits)
  43. {
  44. // 关闭打开的;
  45. if ( g_TVPort.IsOpen() )
  46. g_TVPort.CloseSerialPort();
  47. g_TVPort.OpenSerialPort(nPort, dwBaudrate, ByteSize, Parity, StopBits, 0, 1000);
  48. return g_TVPort.IsOpen();
  49. }
  50. SERIALWATCH_API BOOL IsOpen()
  51. {
  52. return g_TVPort.IsOpen();
  53. }
  54. SERIALWATCH_API void StartMonitor()
  55. {
  56. if ( g_TVPort.IsOpen() )
  57. g_TVPort.StartThread();
  58. }
  59. SERIALWATCH_API void SendCommand(LPCTSTR lpCommand, int nSendCount, int nSleep)
  60. {
  61. if ( !g_TVPort.IsOpen() || !lpCommand)
  62. return ;
  63. bool bRet;
  64. for ( int i = 0; i < nSendCount; i++ ) {
  65. bRet = g_TVPort.WriteComm((BYTE*)lpCommand, _tcslen((TCHAR*)lpCommand));
  66. #ifdef _DEBUG
  67. printf(_T("写串口:%s\n"), bRet ? _T("成功") : _T("失败"));
  68. #endif
  69. Sleep(nSleep);
  70. }
  71. }
  72. // 关闭串口;
  73. SERIALWATCH_API void CloseTVPort()
  74. {
  75. g_TVPort.EndofThread();
  76. if ( g_TVPort.IsOpen() ) {
  77. g_TVPort.CloseSerialPort();
  78. }
  79. }
  80. SERIALWATCH_API bool WatchWord(LPCTSTR lpWatchWord, int nWatchTime)
  81. {
  82. // 设置要监听的单词,由线程去判断;
  83. if ( lpWatchWord == NULL )
  84. return false;
  85. return g_TVPort.FindWord(lpWatchWord);
  86. }
  87. SERIALWATCH_API void ClearBuffer()
  88. {
  89. g_TVPort.ClearBuffer();
  90. }
  91. SERIALWATCH_API bool RTK_USBUpgrade(BYTE byKey)
  92. {
  93. bool bRet = false;
  94. if ( IsOpen() ) {
  95. // 回车;
  96. SendCommand(_T("\r"), 5, 500);
  97. // 判断是否出现console字样;
  98. if ( WatchWord(_T("console:/")) ) {
  99. #ifdef _DEBUG
  100. printf("找到关键字\n");
  101. #endif
  102. // 重启;
  103. SendCommand(_T("reboot\r"), 1);
  104. if ( WatchWord(_T("Terminated"), 100) ) {
  105. ClearBuffer();
  106. while ( TRUE ) {
  107. // 连续发送150次按键;
  108. SendCommand((LPCTSTR)&byKey, 150);
  109. if ( byKey == 0x1B ){ // ESC键;
  110. if ( WatchWord(_T("Realtek>"), 150) ) {
  111. #ifdef _DEBUG
  112. printf("找到关键字\n");
  113. #endif
  114. SendCommand(_T("\r"), 2, 100);
  115. SendCommand(_T("go r\r"), 1, 1000);
  116. bRet = true;
  117. break;
  118. }
  119. } else if (byKey == 0x09) { // TAB键;
  120. if ( WatchWord(_T("loader start!"), 150) ) {
  121. #ifdef _DEBUG
  122. printf("找到关键字\n");
  123. #endif
  124. bRet = true;
  125. break;
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }
  132. return bRet;
  133. }