📄 lab_rs.c.bak
字号:
#include <minigui/common.h>#include <minigui/minigui.h>#include <minigui/gdi.h>#include <minigui/window.h>#include <minigui/control.h>#include <minigui/mgext.h>#include <minigui/skin.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <termios.h>#include <errno.h>#include <string.h>#include "lab_main.h"#define RS_SEND 701#define RS_LISTBOX 702#define RS_COM 703#define RS_EDITREC 704#define RS_EDITSEND 705#define RS_BACK 706#define RS_TIMER 707#define RS_ANIMATION_X 42#define RS_ANIMATION_Y 40#define RS_ANIMATION_W 146#define RS_ANIMATION_H 153//static HWND hIMEWnd; static char *rs_bmp_name[] = { "./res/rs232/rs232bj.png", "./res/rs232/button.png", "./res/rs232/send.png", "./res/common/back.png", "./res/rs232/rs232dt.png"};static BITMAP rs_bmp[5];static CTRLDATA ctrl_data[] = { { CTRL_COMBOBOX, WS_CHILD|CBS_BMP_16X16|CBS_DROPDOWNLIST|CBS_EDITNOBORDER|CBS_NOTIFY|WS_VISIBLE, 360, 20, 110, 13, RS_COM, "COM1", 0, WS_EX_NONE }, { CTRL_MEDIT, WS_CHILD | ES_AUTOWRAP | ES_AUTOVSCROLL | WS_VISIBLE, 213, 35, 264, 210, RS_EDITREC, "", 0, WS_EX_NONE }, { CTRL_EDIT, WS_CHILD | ES_AUTOSELECT | WS_VISIBLE, 213, 250, 200, 20, RS_EDITSEND, "test string", 0, WS_EX_NONE }};static skin_item_t rs_skin_items[] ={ {RS_COM, SI_TYPE_CONTROL | SI_TEST_SHAPE_RECT | SI_STATUS_VISIBLE, 360, 20, {}, 0, "", 0, &ctrl_data[0]}, {RS_EDITREC, SI_TYPE_CONTROL | SI_TEST_SHAPE_RECT | SI_STATUS_VISIBLE, 213, 35, {}, 0, "", 0, &ctrl_data[1]}, {RS_EDITSEND, SI_TYPE_CONTROL | SI_TEST_SHAPE_RECT | SI_STATUS_VISIBLE, 213, 250, {}, 0, "", 0, &ctrl_data[2]}, {RS_SEND, SI_TYPE_CMDBUTTON | SI_TEST_SHAPE_RECT | SI_STATUS_VISIBLE, 420, 250, {}, 2, ""}, {RS_BACK, SI_TYPE_CMDBUTTON | SI_TEST_SHAPE_RECT | SI_STATUS_VISIBLE, 4, 232, {}, 3, ""}};static skin_head_t rs_skin = { "", SKIN_STYLE_TOOLTIP, NULL, NULL, 0, 5, rs_skin_items, FALSE};void load_rs_skin_bmps(skin_head_t *skin, BOOL load){ int i, bmp_num = sizeof(rs_bmp_name)/sizeof(char *); if(load) { for(i=0; i<bmp_num;i++) { (void)LoadBitmapFromFile(HDC_SCREEN, &rs_bmp[i], rs_bmp_name[i]); } skin->bmps = rs_bmp; } else { for(i=0;i<bmp_num; i++) UnloadBitmap(&rs_bmp[i]); skin->bmps = NULL; }}static int animation_index = 0;static char * str_com[]={ "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8"};static int fd_rs;#define BUFFER_LEN 512static char buffer[BUFFER_LEN]={0};static HWND hRSWnd=HWND_INVALID;static pthread_t pid;static int rs_event_cb(HWND hWnd, skin_item_t *item, int event, void *data){ if (event == SIE_BUTTON_CLICKED) { switch(item->id) { case RS_COM: break; case RS_SEND: { #if 1 HWND hctrl=GetDlgItem(hWnd, RS_EDITSEND); if(hctrl == HWND_INVALID) break; else { char * str=NULL; int str_len=0, nwrite; str_len = SendMessage(hctrl, MSG_GETTEXTLENGTH, 0, 0); fprintf(stderr, "get text len=%d\n", str_len); if(str_len > 0) { str = malloc((str_len+1) * sizeof(char)); if(str == NULL) break; memset((void *)str, 0, sizeof(str)); SendMessage(hctrl, MSG_GETTEXT, str_len, (LPARAM)str); if((nwrite = write(fd_rs, str, sizeof(str_len)) )>0) { fprintf(stderr, "send %s to rs\n", str); } free(str); } }#else int nwrite; char buf[]="test string from lab\n"; if((nwrite = write(fd_rs, buf, sizeof(buf)) )>0) fprintf(stderr, "send %s\n", buf);#endif break; } case RS_BACK: SendMessage(hWnd, MSG_CLOSE, 0, 0); break; default: break; } } return 0;}#ifdef INCLUDE_DRIVERvoid read_data_from_rs(void){ int len; //struct timeval timeout; int fd_max; fd_set readfds; FD_ZERO(&readfds); FD_SET(fd_rs, &readfds); fd_max = fd_rs + 1; /* Do the select */ while(1) {#if 0 /* Initialize the timeout structure */ timeout.tv_sec = 1; timeout.tv_usec = 0; n = select(fd_max, &readfds, NULL, NULL, &timeout); if ( FD_ISSET(fd_rs, &readfds) ) { memset(buffer, 0, sizeof(buffer)); len = read(fd_rs, buffer, BUFFER_LEN); fprintf(stderr, "Readed %d data:%s\n",sizeof(buffer), buffer); PostMessage(hRSWnd, MSG_DATARECEIVE, len, 0); }#else while( (len=read(fd_rs, buffer, BUFFER_LEN)) > 0) { //memset(buffer, 0, sizeof(buffer)); buffer[len] = '\0'; fprintf(stderr, "Readed %d data:%s\n",sizeof(buffer), buffer); PostMessage(hRSWnd, MSG_DATARECEIVE, len, 0); }#endif } return;}#endifstatic int rs_msg_cb(HWND hWnd, int message, WPARAM wparam, LPARAM lparam, int *result){ switch(message) { case MSG_CREATE: { #ifdef INCLUDE_DRIVER struct termios termopt, saveopt; fd_rs = open("/dev/ttyS0", O_RDWR); if(fd_rs < 0) fprintf(stderr, "Cannot Open Serial Port !\n"); else fprintf(stderr, "open Serial Port success!\n"); tcgetattr(fd_rs, &saveopt); tcgetattr(fd_rs, &termopt); cfsetispeed(&termopt, B115200); cfsetospeed(&termopt, B115200); termopt.c_lflag &= ~(ICANON | ECHO); termopt.c_cflag &= ~CSIZE; termopt.c_cflag |= CS8; termopt.c_iflag = 0; termopt.c_cc[VTIME] = 1; termopt.c_cc[VMIN] = 1; if( tcsetattr( fd_rs, TCSAFLUSH, &termopt) < 0 ) { fprintf(stderr, "tcsetattr fail !\n"); exit(1); } pthread_create(&pid, NULL, (void *)read_data_from_rs, NULL); #endif/*#ifdef _IME_GB2312#ifdef _LITE_VERSION hIMEWnd = GBIMEWindowEx (hWnd, 0, 240, 200, 30, TRUE);#else hIMEWnd = GBIMEWindow(hWnd);#endif#endif*/ SetTimer(hWnd, RS_TIMER, 30); SetWindowText(GetDlgItem(hWnd, RS_EDITSEND), "test for lab string\n"); break; } case MSG_ACTIVE: { HWND hctrl = GetDlgItem(hWnd, RS_COM); if(hctrl != HWND_INVALID) { int i; for (i =0; i< sizeof(str_com)/sizeof(char *);i++) { if ( SendMessage(hctrl, CB_ADDSTRING, 0, (LPARAM)str_com[i]) >= 0) //fprintf(stderr, "add %s to combox\n", str_com[i]); ; } SendMessage(hctrl, CB_SETCURSEL, 0, 0); } break; } case MSG_DATARECEIVE: { int i; HWND hctrl=GetDlgItem(hWnd, RS_EDITREC); for(i=0; i<strlen(buffer); i++) SendMessage(hctrl, MSG_CHAR, buffer[i], 0L); SendMessage(hctrl, MSG_CHAR, '\n', 0L); break; } case MSG_COMMAND: { int id = LOWORD(wparam); int code = HIWORD(wparam); if ( id == RS_COM && code == CBN_SELCHANGE) fprintf(stderr, "RS com(id=%d, hwnd=%d) change\n", id, (int)lparam); break; } case MSG_TIMER: { int bmp_animation_index = sizeof(rs_bmp_name)/sizeof(char *) -1; HDC hdc = GetClientDC(hWnd); FillBoxWithBitmapPart(hdc, RS_ANIMATION_X, RS_ANIMATION_Y, RS_ANIMATION_W, RS_ANIMATION_H, 0, 0, &rs_bmp[0], RS_ANIMATION_X, RS_ANIMATION_Y); FillBoxWithBitmapPart(hdc, RS_ANIMATION_X, RS_ANIMATION_Y, RS_ANIMATION_W, RS_ANIMATION_H, 0, 0, &rs_bmp[bmp_animation_index], RS_ANIMATION_W*animation_index, 0); animation_index++; if(animation_index >= rs_bmp[bmp_animation_index].bmWidth/RS_ANIMATION_W) animation_index=0; ReleaseDC(hdc); break; } case MSG_PAINT: { int bmp_animation_index = sizeof(rs_bmp_name)/sizeof(char *) -1; HDC hdc = BeginPaint(hWnd); FillBoxWithBitmapPart(hdc, RS_ANIMATION_X, RS_ANIMATION_Y, RS_ANIMATION_W, RS_ANIMATION_H, 0, 0, &rs_bmp[bmp_animation_index], 0, 0); EndPaint(hWnd, hdc); break; } case MSG_CLOSE: { KillTimer(hWnd, RS_TIMER); #ifdef INCLUDE_DRIVE close(fd_rs); pthread_cancel(pid); #endif skin_deinit(&rs_skin); load_rs_skin_bmps(&rs_skin, FALSE); break; } default: break; } return 0;}void CreateRSWindow(HWND hOwner){ load_rs_skin_bmps(&rs_skin, TRUE); if (! skin_init(&rs_skin, rs_event_cb, rs_msg_cb) ) fprintf(stderr, "skin init fail!\n"); else hRSWnd = create_skin_main_window(&rs_skin, hOwner, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, FALSE);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -