/* $Revision: 1.3 $ */ // Automatically generated by MATLAB Project Wizard version 1.0 // // This is the gateway routine for a MATLAB Math/Graphics Library-based // C MATLAB MEX File. /* 本文的实例的开发和运行环境为 V i s u a l++6.0 和 Matlab 6.5,为了方便读者阅读,将yplot.c 文件 的主要程序代码源代码整理如下 link: kernel32.lib user32.lib gdi32.lib winspool.lib libmx.lib libmex.lib libmatlb.lib libmat.lib >>myplot(1:1000,randn(1,1000)) */ #include "mex.h" #include "windows.h" #include "HKCAN.h" //////////////////////////////////////////////////CAN变量定义//////////////// DWORD dwReadThreadID_can; //thread id DWORD dwSendThreadID_can; //thread id BOOL ReadThreadFlag_can=1; //thread flag BOOL SendThreadFlag_can=1; //thread flag DWORD dwShowCanThreadID; //thread id BOOL ShowCanThreadFlag=1; //thread flag BOOL bInitFlag; HANDLE hReadEvent_port1[2]; //event handle HANDLE hReadFrameThread_can; //thread handle HANDLE hSendFrameThread_can; //thread handle HANDLE hShowCanThread; //thread handle HKCANFRAME m_snd_can,m_rec_can; //send and read struct(发送和接受 结构体) HKCANHANDLE m_devicehandle_can; //devicehandle struct char BoardType1[12]="HKCAN30\0"; //open string int m_boardtype=1; DWORD SendNUM_can=0; DWORD ReadNUM_can=0; // CString readdata_can; // CString rn_can; // CString WriteString_can; //use set text // CString sn_w_can; int nnnpport1=0; //int SendAgain = 0,ReadAgain = 0; ULONG err1=1; int errcountdll1=0; FILE *fp1; int repair1=0; UINT CanBPS1=0xc0a3; //1M ///////////////////////////////////////////////////////////////////////// BOOL OnInitCAN(); void ShowCAN(HWND hWnd,HDC hDC); UINT ReadThreadProc_can(LPVOID pParam); UINT SendThreadProc_can(LPVOID pParam); LRESULT WINAPI MainWndProc( HWND, UINT, WPARAM,LPARAM); BOOL DrawData( HWND hWnd, //窗口句柄 HDC hDC, //窗口DC的句柄 double * datax, //绘制图形的:轴数据 double * datay, //绘制图形的Y轴数据 double minx, //x轴数据的最小值 double maxi, //x轴数据的最大值 double miny, //y轴数据的最小值 double maxy, //y轴数据的最大值 int ndatanum //数据点数 ); //图形绘制需要的全局变量 double*datax=NULL; double*datay=NULL; int datanum=0; int m_isFreeData=0; double maxx=0; double maxy=0; double minx=999999; double miny=999999; //要求输入为x和Y两个数值实阵列 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { WNDCLASS wc; MSG msg; HWND hWnd ; HINSTANCE hInstance = NULL; int i = 0; /* // 根据输入参数的个数作不同处理 switch (nrhs) { case 2: { datax = mxGetPr(prhs[0]); datay = mxGetPr(prhs[1]); datanum = mxGetNumberOfElements(prhs[0]) < mxGetNumberOfElements(prhs[1])? mxGetNumberOfElements(prhs[0]) : mxGetNumberOfElements(prhs[1]); maxx = -999999; maxy = -999999; minx = 999999; miny = 999999; // 寻找 datay 的最大值和最小值 for(i=0;i<datanum;i++) { if(datay[i]>maxy) maxy = datay[i]; if(datax[i]>maxx) maxx = datax[i]; if(datay[i]<miny) miny = datay[i]; if(datax[i]<minx) minx = datax[i]; } break; } default:// 外部输入数据错误,绘制坐标 { mexPrintf(" 输入参数错误,本函数需要两个参数(x,y)!\n"); return; } } */ //初始化窗口的属性 wc.lpszClassName="GenericAppClass"; // wc.lpszClassName="窗口"; wc.lpfnWndProc=MainWndProc;/// 回调函数 wc.style=CS_OWNDC|CS_VREDRAW|CS_HREDRAW;// // wc.style=0; wc.hInstance=hInstance; wc.hIcon=LoadIcon( NULL, IDI_APPLICATION); wc.hCursor=LoadCursor( NULL, IDC_ARROW); wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1); wc.lpszMenuName="GenericAppMenu"; wc.cbClsExtra=0; wc.cbWndExtra=0; RegisterClass(&wc);//注册窗口类 /* //注册窗口类 if(!RegisterClass(&wc)) { MessageBeep(0); mexPrintf("注册窗口类失败\n"); // return ; } */ //创建窗口 hWnd=CreateWindow( "GenericAppClass", // "窗口", "绘制曲线", WS_OVERLAPPEDWINDOW, 100,//窗口创建时的位置 100, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL ); //显示窗口 ShowWindow( hWnd ,SW_SHOW ); UpdateWindow(hWnd); while(GetMessage(&msg, NULL,0,0 )) { TranslateMessage( &msg ); DispatchMessage( &msg ); } // return msg.wParam;// OnInitCAN(); } // 处理 Windows 窗口消息的系统回调函数 LRESULT CALLBACK MainWndProc( HWND hWnd , UINT msg , WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hDC; switch( msg ) { case WM_PAINT: hDC = BeginPaint( hWnd, &ps); // DrawData(hWnd,hDC,datax,datay, // minx,maxx,miny,maxy,datanum); // ShowCAN(hWnd,hDC); EndPaint( hWnd, &ps ); break; /* case WM_COMMAND: switch( wParam ) { break; } break; */ case WM_DESTROY: PostQuitMessage(0); break; default: return( DefWindowProc(hWnd, msg, wParam, lParam )); } return 0; } // 绘制二维曲线 BOOL DrawData(HWND hWnd,HDC hDC, double *datax, double *datay, double minx, double maxx, double miny, double maxy, int ndatanum) { POINT org; SIZE size; RECT winRect; double dx,dy; int i; HPEN hOld=NULL; HPEN linePen; //设置DC的映射方式 SetMapMode( hDC, MM_ANISOTROPIC ); GetClientRect(hWnd,&winRect); SetWindowOrgEx(hDC,0,0,&org); SetWindowExtEx( hDC,1000,-1000,&size); SetViewportOrgEx(hDC,0,abs(winRect.top-winRect.bottom),&org); SetViewportExtEx(hDC,abs(winRect.left-winRect.right),abs(winRect.top-winRect.bottom),&size); //设置DC的背景方式 SetBkMode(hDC,TRANSPARENT); //创建绘制曲线的画笔 linePen=CreatePen(PS_SOLID,2,RGB(0,0,200)); hOld=SelectObject(hDC,linePen); //绘制边框 Rectangle(hDC,10,10,1000-10,1000-10); if(datanum ==0) return FALSE; //绘制曲线 dx =900.0/(maxx-minx); dy=900.0/(maxy-miny); SetROP2(hDC,R2_COPYPEN); MoveToEx(hDC,10+datax[0]*dx,10+datay[0]*dy,NULL); for(i=1;i<datanum;i++) { LineTo (hDC,10+ (datax[i]-minx)*dx,10+(datay[i]-miny)*dy); } //恢复DC画笔的默认设置 SelectObject(hDC,hOld); return TRUE; } // BOOL OnInitCAN() { UCHAR temp[9]; // printf("OnInitCAN()... \n"); bInitFlag=FALSE; m_snd_can.nPort = nnnpport1; m_snd_can.mFrame[0] = 0x0a; m_snd_can.mFrame[1] = 0x88; m_snd_can.mFrame[2] = 'a'; m_snd_can.mFrame[3] = 'b'; m_snd_can.mFrame[4] = 'c'; m_snd_can.mFrame[5] = 'd'; m_snd_can.mFrame[6] = 'e'; m_snd_can.mFrame[7] = 'f'; m_snd_can.mFrame[8] = 'g'; m_snd_can.mFrame[9] = 'h'; // memcpy(temp,&m_snd_can.mFrame[2],8); // temp[8]='\0'; // m_senddata.Format("%s",temp); // UpdateData(FALSE); // SetDlgItemText(IDC_BOARDTYPE,"CAN30B"); // SetDlgItemText(IDC_BPS,"1M"); hReadEvent_port1[0] = CreateEvent(NULL,TRUE,FALSE,NULL); hReadEvent_port1[1] = CreateEvent(NULL,TRUE,FALSE,NULL); // hReadFrameThread_can=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ReadThreadProc_can,this,0,&dwReadThreadID_can); hReadFrameThread_can=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ReadThreadProc_can,NULL,0,&dwReadThreadID_can); hSendFrameThread_can=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)SendThreadProc_can,NULL,0,&dwSendThreadID_can); hShowCanThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ShowCAN,NULL,0,&dwShowCanThreadID); /* fp1 =fopen("c:\\RootTable.txt","w"); if ( fp1==NULL ) { printf("新建文件RootTable.txt出错!"); } fprintf( fp1, "\n\n"); fprintf( fp1, "\t//错误码\n" ); fprintf( fp1, "\t错误码 = {\n\t\t"); */ // printf("OnInitCAN()...end \n"); return TRUE; // return TRUE unless you set the focus to a control } void ShowCAN(HWND hWnd,HDC hDC) { POINT org; SIZE size; RECT winRect; double dx,dy; int i,j; HPEN hOld=NULL; HPEN linePen; UCHAR str[10];//dong printf("ShowCan()..."); //设置DC的映射方式 SetMapMode( hDC, MM_ANISOTROPIC ); GetClientRect(hWnd,&winRect); SetWindowOrgEx(hDC,0,0,&org); SetWindowExtEx( hDC,1000,-1000,&size); SetViewportOrgEx(hDC,0,abs(winRect.top-winRect.bottom),&org); SetViewportExtEx(hDC,abs(winRect.left-winRect.right),abs(winRect.top-winRect.bottom),&size); //设置DC的背景方式 SetBkMode(hDC,TRANSPARENT); //创建绘制曲线的画笔 linePen=CreatePen(PS_SOLID,2,RGB(0,0,200)); hOld=SelectObject(hDC,linePen); //绘制边框 Rectangle(hDC,10,10,1000-10,1000-10); // if(datanum ==0) return FALSE; //显示文字 TextOut(hDC,20,900,"Can采集数据:",strlen("Can采集数据:"));//?? // itoa(12345678,str, 10);//itoa()将整数转换为字符串 for (i=0;i<400;i++) { // m_snd_can.mFrame[i]=('0'+i); // printf("m_snd_can.mFrame[%d]=%c \t",i,m_snd_can.mFrame[i]); if((i%40)==0) j=(i/40)*50; TextOut(hDC,20+(i%40)*16,850-j,&m_snd_can.mFrame[i%10],1); // TextOut(hDC,20+i*16,700,&str[i],1); // Sleep(20); } Sleep(50); //dong-add //恢复DC画笔的默认设置 SelectObject(hDC,hOld); return TRUE; } UINT ReadThreadProc_can(LPVOID pParam) { // HWND hWnd=((CCantestDlg *)pParam)->m_hWnd; int Read; int Temp; UCHAR temp[9]; printf("ReadThreadProc_can... "); do{ DWORD Statuofport=WaitForMultipleObjects(2,hReadEvent_port1,FALSE,INFINITE); switch(Statuofport) { case WAIT_OBJECT_0 : ResetEvent(hReadEvent_port1[0]); Temp = 0; break; case WAIT_OBJECT_0+1 : ResetEvent(hReadEvent_port1[1]); Temp = 1; break; } Sleep(50); //dong-add while( HKCanReadFrame(&m_devicehandle_can,Temp,&m_rec_can) >= 0) { ReadNUM_can++; // TRACE("READ %d",Read); printf("ReadNUM_can=%u ",ReadNUM_can); //display the data received /* memcpy(temp,&m_rec_can.mFrame[2],8); temp[8]='\0'; readdata_can.Format("%s",temp); rn_can.Format("%u",ReadNUM_can); */ // ((CCantestDlg *)pParam)->SetDlgItemText(IDC_READDATA,readdata_can); // ((CCantestDlg *)pParam)->SetDlgItemText(IDC_RN,rn_can); } }while(ReadThreadFlag_can); printf("ReadThreadProc_can...end "); return 0L; } //send frame thread UINT SendThreadProc_can(LPVOID pParam) { // ((CCantestDlg *)pParam)->SetDlgItemText(IDC_SEND,"开始SendCAN线程");//DONG-ADD int Send=0; UCHAR temp[9]; int tempdll; int i; SendThreadFlag_can=1; Send = -1; printf("SendThreadProc_can... "); // CWnd *SetText =((CCantestDlg *)pParam)->GetDlgItem(IDC_SENDDATA); // CWnd *SendNum =((CCantestDlg *)pParam)->GetDlgItem(IDC_SN); do{ tempdll = Send; Send = HKCanSendFrame(&m_devicehandle_can,&m_snd_can); Sleep(200); // SetText->SetWindowText(WriteString); SendNUM_can ++; // printf("SendNUM_can=%u ",SendNUM_can); /* memcpy(temp,&m_snd_can.mFrame[2],8); temp[8]='\0'; WriteString_can.Format("%s",temp); sn_w_can.Format("%u",SendNUM_can); */ // SetText->SetWindowText(WriteString_can); // SendNum->SetWindowText(sn_w_can); for(i=0;i<8;i++) { m_snd_can.mFrame[2+i]++; if(m_snd_can.mFrame[2+i] > 'z') m_snd_can.mFrame[2+i] = 'a'; } // mexPrintf("发送字节数:%s \t",sn_w_can); //XXXX ??? // mexPrintf("发送内容:%s \n",WriteString_can); }while(SendThreadFlag_can); // ((CCantestDlg *)pParam)->SetDlgItemText(IDC_SEND,"结束SendCAN线程");//DONG-ADD // mexPrintf("发送字节数:%u \n",SendNUM_can); return 0L; }
正文
我的CAN_mex2006-10-09 20:35:00
【评论】 【打印】 【字体:大 中 小】 本文链接:http://blog.pfan.cn/embed/19163.html
阅读(323) | 评论(0)
版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!
评论