⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uartapp.c

📁 基于东南大学开发的SEP3203的ARM7中的所有驱动
💻 C
字号:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include <asixwin.h>
#include <asixapp.h>
#include <asixwin\asix_ed.h>
#include <asixwin\asix_mn.h>
#include <asixwin\asix_tb.h>
#include <asixwin\select.h>
#include <asixwin\asix_lb.h>

#include <sys\sysmsg.h>
#include <resource\bitmap.h>

#include "uartsrv.h"
#include "uartdrv.h"
#include "uartapp.h"




TASKDESCRIPTION UartServerDesp = 
{"uart_server",	ASIX_APP,	2,	zi_chuankou,	2048,	1,	LCD_WIDTH, LCD_HEIGHT,	UartServer,	APP_PRI};


//TASKDESCRIPTIONLIST	UartServerDespLst =
//{NULL,	NULL,	&UartServerDesp, UARTSERVER_ID, uart_server_stk};

#ifdef UART_DBG
void UartServer(void)
{
	char *pRecFile = NULL;
	
	init_uart1(70000000, 38400, 8, 8);
	prints(0, "Uart ok8\n");
	pRecFile = (char *)SysLmalloc(WRITENUM*sizeof(char));
	SysReadUart(DEVICE, pRecFile, WRITENUM, WAIT_TMO);
}

#else
void UartServer(void)
{
	U32		mainwin;
//	U32		start;
	U32		ack_start;
	U32		receive;
	U32		transmit;
	U32		recdisp;
	U32		tradisp;
	U32		close;
	U32		config;
	
	
	MSG		msg;
	U8      quit = 0;
	
	char *pRecFile = NULL;
	char *pTraFile = NULL;
	
	int		n;
	
	mainwin = CreateWindow(WNDCLASS_WIN,
							"UartServe",
							WS_OVERLAPPEDWINDOW,
							0,
							0,
							LCD_WIDTH,
							LCD_HEIGHT,
							0,
							0,
							NULL);
							
/*	
	start = CreateWindow(WNDCLASS_BUTTON,
							"开始",
							WS_CHILD|BS_ICON|BS_FLAT|BS_LEFT,
							20,
							40,
							30,
							30,
							mainwin,
							0,
							NULL);
	
	ack_start = CreateWindow(WNDCLASS_EDITOR,
							"请初始化设备",
							WS_CHILD|ES_SINGLE_REGULAR,
							70,
							40,
							140,
							30,
							mainwin,
							0,
							NULL);
*/						
	receive = CreateWindow(WNDCLASS_BUTTON,
							"接收",
							WS_CHILD|BS_ICON|BS_FLAT|BS_LEFT,
							20,
							100,
							30,
							30,
							mainwin,
							0,
							NULL);
							
	recdisp = CreateWindow(WNDCLASS_EDITOR, 
							"点击接收后开始", 
							WS_CHILD|ES_MULTI_REGULAR, 
							70,
							80, 
							140, 
							75, 
                            mainwin, 
							DISPLAYSIZE, 
							NULL); 
	
	transmit = CreateWindow(WNDCLASS_BUTTON,
							"发送",
							WS_CHILD|BS_ICON|BS_FLAT|BS_LEFT,
							20,
							190,
							30,
							30,
							mainwin,
							0,
							NULL);   
							
	tradisp = CreateWindow(WNDCLASS_EDITOR,             
							"请发送数据",                       
							WS_CHILD|ES_MULTI_REGULAR,  
							70,                  
							165,                  
							140,                  
							75,                  
							mainwin,                    
							DISPLAYSIZE,                
							NULL);                      
    
    close = CreateWindow(WNDCLASS_BUTTON,
							"关闭",
							WS_CHILD|BS_ICON|BS_FLAT|BS_LEFT,
							20,
							260,
							30,
							30,
							mainwin,
							0,
							NULL);   
							
	config = CreateWindow(WNDCLASS_BUTTON,
							"配置",
							WS_CHILD|BS_ICON|BS_FLAT|BS_LEFT,
							160,
							260,
							30,
							30,
							mainwin,
							0,
							NULL);    						
	
	SysDevTableInit();
	SysOpenUart(DEVICE);

	while(!quit)
	{
		ASIXGetMessage(&msg, NULL, 0, 0); 
		switch(msg.message)
		{
			case WM_COMMAND:
			{
/*				if(msg.lparam == start)
				{
					if((SysOpenUart(DEVICE)) == SYS_OK)
				 	{
				 		SetWindowText(ack_start, "initial ok!", NULL);
				 		prints(DEVICE, "ok");
				 	}
				 	else
				 		SetWindowText(ack_start, "initial err!", NULL);
					
				} 	
*/
				if(msg.lparam == config)
					ConfigUart();	
					
				else if(msg.lparam == receive)
				{	
					SetWindowText(recdisp, "请输入数据", NULL);
					pRecFile = (char *)SysLmalloc((WRITENUM + 1)*sizeof(char));
					if(pRecFile == NULL)
						break; //return FALSE;
					n = SysReadUart(DEVICE, pRecFile, WRITENUM, WAIT_TMO);
					if(n == SYS_OK)
					{
						*(pRecFile + WRITENUM) = '\0';
						SetWindowText(recdisp, "接收完毕", NULL);
						prints(DEVICE, pRecFile);
					}
					else
						SetWindowText(recdisp, "receive err", NULL);

					init_uart(DEVICE);
					
				}	
				
				else if(msg.lparam == transmit)
				{
					pTraFile = (char *)SysLmalloc(READNUM*sizeof(char));
					if(pTraFile == NULL)
						break; //return FALSE;
					pTraFile = "abcdefghi1abcdefghi2abcdefghi3abcdefghi4abcdefghi5abcdefghi6abcdefghi7abcdefghi8abcdefghi9abcdefghi0ABCDEFGH";
					n = SysWriteUart(DEVICE, pTraFile, READNUM, WAIT_TMO);
					if(n == SYS_OK)
						SetWindowText(tradisp, "发送完毕", NULL);
					else
						SetWindowText(tradisp, "transmit err!", NULL);
						
					init_uart(DEVICE);
				
				}
				
				else if(msg.lparam == close)
				{	
					SysCloseUart(DEVICE);
					quit = 1;					
				}
				
								
			}
			break;
			
			case WM_QUIT:
				SysCloseUart(DEVICE);
				quit = 1;
				break;
				
			default:
				break;		
		}	
		
		DefWindowProc(msg.message, msg.lparam, msg.data, msg.wparam); 

	}     
	
    Lfree(pRecFile);
    Lfree(pTraFile);
    
	DestroyWindow( mainwin ); 
	EndofTask(); 
	return;  
}
#endif




// 串口设置
void ConfigUart(void)
{
	MSG 	msg;
	U8		quit = 0;
//	U32		hGC;

 	// 窗口和控件ID
	U32		mainwin;		// 主窗口
	U32		lb[4];			// 配置选项列表
	U32		bt_ok;			// 确定键
	U32		bt_cancel;		// 取消键
	U32		st[4];			// 选项提示
	U32		tskbar;			// 任务栏

	S8		*stcap[5] = {"串口", "速率", "数据位", "校验", "停止位" };
/*	struct LBOX_ITEM	lb_baud[] =
	{	
		ICON_ENABLE,	UNUNDERLINE,	"38400",
		ICON_ENABLE,	UNUNDERLINE,	"28800",
		ICON_ENABLE,	UNUNDERLINE,	"19200",
		ICON_ENABLE,	UNUNDERLINE,	"14400",
		ICON_ENABLE,	UNUNDERLINE,	"9600",
		ICON_ENABLE,	UNUNDERLINE,	"4800",
		ICON_ENABLE,	UNUNDERLINE,	"1200",
		ICON_ENABLE,	UNUNDERLINE,	"600",
		ICON_ENABLE,	UNUNDERLINE,	"300",
		ICON_END,		UNUNDERLINE,	NULL
	};
*/
	struct LBOX_ITEM	lb_com[] = 
	{
		ICON_ENABLE,	UNUNDERLINE,	"COM1",    //COM1: UART0, DEVID = 0
		ICON_ENABLE,	UNUNDERLINE,	"COM2",    //COM2: UART1, DEVID = 1
		ICON_END,		UNUNDERLINE,	NULL      
	
	};
	
	struct LBOX_ITEM	lb_baud[] =               
	{	                                          
		ICON_ENABLE,	UNUNDERLINE,	"115200",  
		ICON_ENABLE,	UNUNDERLINE,	"57600",  
		ICON_ENABLE,	UNUNDERLINE,	"56000",  
		ICON_ENABLE,	UNUNDERLINE,	"43000",  
		ICON_ENABLE,	UNUNDERLINE,	"38400",   
		ICON_ENABLE,	UNUNDERLINE,	"19200",   
		ICON_ENABLE,	UNUNDERLINE,	"9600",   
		ICON_ENABLE,	UNUNDERLINE,	"1200",    
		ICON_ENABLE,	UNUNDERLINE,	"300",    
		ICON_END,		UNUNDERLINE,	NULL      
	};                                            
	
	struct LBOX_ITEM	lb_data[] =
	{	
		ICON_ENABLE,	UNUNDERLINE,	"8",
		ICON_ENABLE,	UNUNDERLINE,	"7",
		ICON_ENABLE,	UNUNDERLINE,	"6",
		ICON_ENABLE,	UNUNDERLINE,	"5",
		ICON_END,		UNUNDERLINE,	NULL
	};
	struct LBOX_ITEM	lb_ck[] =
	{	
		ICON_ENABLE,	UNUNDERLINE,	"无",
		ICON_ENABLE,	UNUNDERLINE,	"偶",
		ICON_ENABLE,	UNUNDERLINE,	"奇",
		ICON_END,		UNUNDERLINE,	NULL
	};
	struct LBOX_ITEM	lb_stp[] =
	{	
		ICON_ENABLE,	UNUNDERLINE,	"1",
		ICON_ENABLE,	UNUNDERLINE,	"1.5",
		ICON_ENABLE,	UNUNDERLINE,	"2",
		ICON_END,		UNUNDERLINE,	NULL
	};

	struct LBOX_ITEM	*lb_cap[5];


	U8		i;
//	U8		UartCnfgValue[4];

	
	U16		temp;
	
	U16		temp_value[5] = {0,6,0,0,0};
	
	U32		devid;
	U32		baudrate;
	U32		databit;
	
	
	// WINDOW ARRANGEMENT
	const	U16		form_w = PHY_LCD_W, form_h = PHY_LCD_H, form_x = 0, form_y = 0;
	const	U16		st_w = 48, st_h = 24, st_x = form_x + 5, st_y = form_y + 20 + ( form_h - 72 - st_h * 5 )/2 - 20;
	const	U16		lb_w = LCD_WIDTH*1/2, lb_h = st_h, lb_x = st_x + st_w + 5, lb_y = st_y;
	const	U16		bt_w = (PHY_LCD_W-2)/2, bt_h = st_h, bt_x = form_x + 1, bt_y = form_y + PHY_LCD_H - 20 - bt_h;

	lb_cap[0] = lb_com;
	lb_cap[1] = lb_baud;
	lb_cap[2] = lb_data;
	lb_cap[3] = lb_ck;
	lb_cap[4] = lb_stp;

	mainwin = CreateWindow(	WNDCLASS_WIN,			
					"串口设置",			
					WS_OVERLAPPEDWINDOW,
					form_x,form_y,				
					form_w,form_h,
					0,				
					0,				
					NULL);	
							
	tskbar = CreateWindow(WNDCLASS_TSKBAR, NULL, WS_CHILD|TBS_TYPICAL, 0, 0, 0, 0,mainwin, 0, NULL);
	
	temp = st_y;
	for( i = 0; i < 5; i++ )
	{
//		temp_value[i] = UartCnfgValue[i];

		// 提示
		st[i] = CreateWindow( WNDCLASS_STATIC, 
								stcap[i], 
								WS_CHILD | SS_TEXT | SS_RIGHT,
								st_x,temp,
								st_w,st_h,
								mainwin,0,NULL);
		// 选项
		lb[i] = CreateWindow( WNDCLASS_LIST, 
								NULL,
								WS_CHILD | BS_ICON,// | BS_REGULAR,
								lb_x,temp,
								lb_w,lb_h,
								mainwin,0,lb_cap[i]);
		SelectListBox( lb[i], temp_value[i] );
		temp += st_h + 8;
	}

	bt_ok = CreateWindow( WNDCLASS_BUTTON, 
							"确定", 
							WS_CHILD | BS_REGULAR | BS_FLAT | BS_BOARD,
							bt_x,bt_y,
							bt_w,bt_h,
							mainwin,0,NULL);
	bt_cancel = CreateWindow( WNDCLASS_BUTTON, 
							"取消", 
							WS_CHILD | BS_REGULAR | BS_FLAT | BS_BOARD,
							(U16)(bt_x + bt_w),bt_y,
							bt_w,bt_h,
							mainwin,0,NULL);

	SetFocus( bt_ok );
	
	while( !quit )
	{
		ASIXGetMessage( &msg, NULL, 0, 0 );
		switch( msg.message )
		{			
			case WM_COMMAND:
				if( msg.lparam == bt_ok )
				{
					// 保存设置
//					for( i = 0; i < 4; i++ )
//					{
//						UartCnfgValue[i] = temp_value[i];
//					}
					
					devid = temp_value[0];
					sscanf(lb_baud[temp_value[1]].item_text, "%d", &baudrate);
					sscanf(lb_data[temp_value[2]].item_text, "%d", &databit);
					
					init_uart(devid);
					conf_uart(devid, SYSCLK, baudrate, databit, TRIGERLEVEL);
					quit = 1;
				}
				else if( msg.lparam == bt_cancel )
				{
					quit = 1;
				}
				break;
			case WM_LISTBOX:
				for( i = 0; i < 5; i++ )
				{
					if( msg.lparam == lb[i] )
					{
						temp_value[i] = (U16)msg.wparam;
						break;
					}
				}
				break;
			case WM_KEYDOWN:
			case WM_KEYUP:
				// 切换焦点
				switch( msg.wparam )
				{
					case ASIX_KEY_UP:
					case ASIX_KEY_LEFT:
					case ASIX_KEY_DOWN:
					case ASIX_KEY_RIGHT:
						if( msg.lparam == bt_ok )
							SetFocus( bt_cancel );
						else if( msg.lparam == bt_cancel )
							SetFocus( bt_ok );
						break;
				}
				break;
			case WM_QUIT:
				quit = 1;
				break;
		}
		DefWindowProc(msg.message, msg.lparam, msg.data, msg.wparam); 
	}
	
	DestroyWindow( mainwin );
//	EndofTask();
	
	return;
}



















⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -