观察串口打开、关闭、发送、接收,可以使用串口侦听软件,比如:http://www.sudt.com的SerialTrace,这个不错。
完全按照裸奔方式设计的仿MCU的WINDOW程序,以后可能只要会MCU就可设计WINDOW程序,可能......
有关MCU部分,RainLib_Base.lib, RainLib_Unsiversal.lib, RainLib_Driver.lib被封装好,按MCU玩WINDOW游戏程序,很简单了。
代码:main.cpp //--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop
#include "Main.h"
#include "..\WinTools\RainLib_WinTools.h" #include "..\Base\RainLib_Base.h" #include "..\Unsiversal\RainLib_Unsiversal.h" #include "..\Driver\RainLib_Driver.h"
//--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; struct CommunicateQueue *psCommunicateQueue_Uart8; //串口1
//--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //---------------------------------------------------------------------------
//main.c必须提供 void _stdcall CallBack_5msInterrupt( void ) { }
//main.c必须提供 void _stdcall CallBack_20msInterrupt( void ) { }
//--------------------------------------------------------------------------- //窗口初始化 //--------------------------------------------------------------------------- void __fastcall TForm1::MainFormInit( void ) { this->ClientWidth = Ini_Read( "LCD", "ScreenWidth", 640 ); this->ClientHeight = Ini_Read( "LCD", "ScreenHeight", 480 ); }
//--------------------------------------------------------------------------- //主初始化 //--------------------------------------------------------------------------- void __fastcall TForm1::MainInit( void ) { WinToolsLib_Init( "RainLib.ini" ); //INI文件名 this->MainFormInit();
BaseLib_Init( Ini_Read( "MSG", "BufferMax", 5000 ) ); //消息队列大小 UnsiversalLib_Init( Ini_Read( "PACKAGE", "PackageMax", 2 ) ); //使用包解析的任务数 DriverLib_Init( this->ClientWidth, this->ClientHeight );
psCommunicateQueue_Uart8 = CommunicateQueue_Register( 200, 200, NULL); if( Uart8_Setup( 1, psCommunicateQueue_Uart8, B38400, BIT_8|STOP_1|P_NONE ) == false ) { Application->MessageBoxA( "打开串口错误", "程序中断退出!", MB_OK ); Application->Terminate(); }
//lcd U8 *p = DrvLcd_FramePagePoint(1); for( int i=0; i<this->ClientHeight*2; i++ ) { for( int j=0; j<this->ClientWidth; j++ ) { *p++ = i; } }
//开始运行程序 Msg_RunInit(); this->Show(); }
//--------------------------------------------------------------------------- //析构 //--------------------------------------------------------------------------- void __fastcall TForm1::MainDestory( void ) { DriverLib_Destory(); UnsiversalLib_Destory(); BaseLib_Destory(); WinToolsLib_Destory(); }
//--------------------------------------------------------------------------- //主循环 //--------------------------------------------------------------------------- void __fastcall TForm1::MainLoop( void ) { static bool bTestLcdMove = false; static U8 mCounter = 0; static int my = 0;
BaseLib_MainLoop(); UnsiversalLib_MainLoop(); DriverLib_MainLoop(); WinToolsLib_MainLoop();
if( sSysTimer.bSysTouch ) { if( ++mCounter >= 2 ) { //20ms * 2时间进入 mCounter = 0;
if( bTestLcdMove ) { my--; if( my == 0 ) { bTestLcdMove = false; } } else { my++; if( my == (this->ClientHeight-1) ) { bTestLcdMove = true; } }
DrvLcd_FrameOffset( my ); DrvLcd_RefreshToForm( this->Canvas, Rect(0, 0, this->ClientWidth, this->ClientHeight) );
//向串口发送my位置 CommunicateQueue_Push( psCommunicateQueue_Uart8, (U8)my ); CommunicateQueue_Push( psCommunicateQueue_Uart8, (U8)(my/256) ); } } }
RainLibMain.cpp:WINDOW调用 //---------------------------------------------------------------------------
#include <vcl.h> #include "Main.h" #pragma hdrstop //--------------------------------------------------------------------------- USEFORM("Main.cpp", Form1); //--------------------------------------------------------------------------- WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) { try { Application->Initialize(); Application->CreateForm(__classid(TForm1), &Form1); Form1->MainInit(); //主初始化
MSG msg; ZeroMemory( &msg, sizeof(msg) ); while( msg.message != WM_QUIT ) { if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } else { Form1->MainLoop(); //主循环 } } Form1->MainDestory(); } catch (Exception &exception) { Application->ShowException(&exception); } catch (...) { try { throw Exception(""); } catch (Exception &exception) { Application->ShowException(&exception); } } return 0; } //--------------------------------------------------------------------------- |