Читайте также:
|
|
#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <math.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK DiagramProc(HWND, UINT, WPARAM, LPARAM);
#define ID_LIST 15
#define ID_START 140
#define ID_KILL 666
#define ID_DEMO 500
#define ID_UPLU 168
#define ID_UMIN 169
#define ID_TPLU 178
#define ID_TMIN 179
#define TIMEOUT 2000
HINSTANCE hInst;
HWND hWnd, hGrWnd;
HWND hCombo, hCombo2, hCombo3, startbut, exitbut, demobut, Uplu, Umin,Tplu, Tmin, textb;
char ClassName[]="Window";
char AppTitle[]="Осцилограф";
int i = 0,samp=0;
int height = 800, width = 800;
int dheight, dwidth;
int sendSet(struct mess *MESS);
void demo();
HANDLE COMport = NULL;
WORD buffer[32768];
double Uscale=1, Tscale=10;
char Ugrd[5], Tgrd[5];
int SampChk(),old_freq =10;
int Recieve(unsigned char *);
void RecieveData();
RECT r;
char buf[100];
typedef struct mess
{
BYTE a;
BYTE b;
BYTE c;
};
struct mess MESS;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wc;
MSG msg;
hInst = hInstance;
memset(&wc, 0, sizeof(wc));
wc.lpszClassName=ClassName;
wc.lpfnWndProc=(WNDPROC)WndProc;
wc.style=CS_HREDRAW|CS_VREDRAW;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = CreateSolidBrush(RGB(200,200,200));
wc.lpszMenuName=NULL;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
RegisterClass(&wc);
hWnd=CreateWindow(ClassName, AppTitle, WS_BORDER /*| WS_MAXIMIZEBOX*/ | WS_MINIMIZEBOX | WS_SYSMENU, 120,120,625,395,NULL,NULL,hInst, NULL);
memset(buffer, 0,32768);
if(!hWnd)
{
MessageBox(NULL,"Create:error",
AppTitle,MB_OK|MB_ICONSTOP);
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
int SampChk()
{
TCHAR SampStr[14];
SendMessage(textb, EM_GETLINE, 0,(LPARAM) SampStr);
for (i=0; i<13; i++)
if ((SampStr[i]<'0')|| (SampStr[i]>'9')) SampStr[i]=0;
atoi(SampStr);
return atoi(SampStr);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
char *str="Osc";
HDC hDC;
PAINTSTRUCT ps;
int k, j;
switch(msg)
{
case WM_COMMAND:
if (LOWORD(wParam) == ID_START)
{
int uSelectedItem, nSize;
HDC hdc;
MESS.a=0;
MESS.b=0;
MESS.c=0;
switch (SendMessage(hCombo, CB_GETCURSEL, 0, 0L))
{
case 0: MESS.a^=128; break;
case 1: MESS.a^=64; break;
case 2: MESS.a^=192; break;
}
switch (SendMessage(hCombo2, CB_GETCURSEL, 0, 0L))
{
case 0: MESS.a^=48; break;
case 1: MESS.a^=8; break;
case 2: MESS.a^=16; break;
case 3: MESS.a^=24; break;
case 4: MESS.a^=32; break;
}
samp = SampChk();
if ((samp>32000)||(samp<1))
MessageBox(hWnd,"Number of samples should be between 1 and 32768",0,MB_OK);
else
{
MESS.b=samp>>8;
MESS.c=samp;
sendSet(&MESS);
}
}
if(LOWORD(wParam) == ID_LIST)
{
UpdateWindow(hWnd);
}
if (LOWORD(wParam) == ID_KILL)
{
SendMessage(hWnd, WM_DESTROY, NULL, NULL);
}
if (LOWORD(wParam) == ID_DEMO)
{
SendMessage(hCombo2, CB_GETCURSEL, 0, 0L);
samp = SampChk();
if ((samp>32768)||(samp<1))
MessageBox(hWnd,"Number of samples should be between 1 and 32000",0,MB_OK);
else demo();
}
if (LOWORD(wParam) == ID_UPLU)
{
if(Uscale<10)
Uscale*=1.1;
InvalidateRect(hGrWnd, NULL, true);
SendMessage(hGrWnd, WM_PAINT, NULL, NULL);
}
if (LOWORD(wParam) == ID_UMIN)
{
if(Uscale>0.4)
Uscale/=1.1;
InvalidateRect(hGrWnd, NULL, true);
SendMessage(hGrWnd, WM_PAINT, NULL, NULL);
}
if (LOWORD(wParam) == ID_TPLU)
{
Tscale*=1.1;
InvalidateRect(hGrWnd, NULL, true);
SendMessage(hGrWnd, WM_PAINT, NULL, NULL);
}
if (LOWORD(wParam) == ID_TMIN)
{
if(Tscale>0.3)
Tscale/=1.1;
InvalidateRect(hGrWnd, NULL, true);
SendMessage(hGrWnd, WM_PAINT, NULL, NULL);
}
break;
case WM_PAINT:
DefWindowProc(hWnd, msg, wParam, lParam);
break;
case WM_SIZE:
width = LOWORD(lParam);
height = HIWORD(lParam);
break;
case WM_SETFOCUS:
UpdateWindow(hWnd);
break;
case WM_MOVE:
UpdateWindow(hWnd);
break;
case WM_DESTROY:
DestroyWindow(hCombo);
PostQuitMessage(0);
break;
case WM_CREATE:
WNDCLASS Gwc;
memset(&Gwc,0,sizeof(WNDCLASS));
Gwc.lpfnWndProc = DiagramProc;
Gwc.hInstance = hInst;
Gwc.lpszClassName = "ChildWClass";
Gwc.hbrBackground = CreateSolidBrush(RGB(255,255,255));
Gwc.hCursor=LoadCursor(NULL,IDC_CROSS);
RegisterClass(&Gwc);
hGrWnd = CreateWindowEx(0,"ChildWClass",(LPCTSTR) NULL,
WS_CHILD | WS_VISIBLE,15, 15,
570, 250, hWnd, NULL, hInst, NULL);
hCombo = CreateWindow (WC_COMBOBOX,0,
WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST,
100,290,100,500, hWnd,(HMENU) ID_LIST,hInst,0);
SendMessage(hCombo,CB_ADDSTRING,0,(LPARAM)"ADC 0");
SendMessage(hCombo,CB_ADDSTRING,0,(LPARAM)"ADC 1");
SendMessage(hCombo,CB_ADDSTRING,0,(LPARAM)"ADC 0 Diff.");
SendMessage(hCombo,CB_SETCURSEL,(WPARAM)0,0);
hCombo2 = CreateWindow (WC_COMBOBOX,0,
WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST,
100,325,100,500, hWnd,(HMENU) ID_LIST,hInst,0);
SendMessage(hCombo2,CB_ADDSTRING,0,(LPARAM)"50 KHz");
SendMessage(hCombo2,CB_ADDSTRING,0,(LPARAM)"150 KHz");
SendMessage(hCombo2,CB_ADDSTRING,0,(LPARAM)"275 KHz");
SendMessage(hCombo2,CB_ADDSTRING,0,(LPARAM)"350 KHz");
SendMessage(hCombo2,CB_ADDSTRING,0,(LPARAM)"500 KHz");
SendMessage(hCombo2,CB_SETCURSEL,(WPARAM)0,0);
hCombo3 = CreateWindow (WC_COMBOBOX,0,
WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST,
320,290,100,500, hWnd,(HMENU) ID_LIST,hInst,0);
SendMessage(hCombo3,CB_ADDSTRING,0,(LPARAM)"Com1");
SendMessage(hCombo3,CB_ADDSTRING,0,(LPARAM)"Com2");
SendMessage(hCombo3,CB_ADDSTRING,0,(LPARAM)"Com3");
SendMessage(hCombo3,CB_ADDSTRING,0,(LPARAM)"Com4");
SendMessage(hCombo3,CB_SETCURSEL,(WPARAM)2,0);
textb=CreateWindow(WC_EDIT,"1700",WS_CHILD | WS_VISIBLE | ES_LEFT | ES_NUMBER,320,327,100,20, hWnd,0,hInst,0);
CreateWindow("STATIC","Mode:",WS_CHILD|WS_VISIBLE,17,292,73,20, hWnd, 0,0,0);
CreateWindow("STATIC","Frequency:",WS_CHILD|WS_VISIBLE,17,327,73,20, hWnd, 0,0,0);
CreateWindow("STATIC","Port:",WS_CHILD|WS_VISIBLE,237,292,73,20, hWnd, 0,0,0);
CreateWindow("STATIC","Samples:",WS_CHILD|WS_VISIBLE,237,327,73,20, hWnd, 0,0,0);
startbut=CreateWindow("BUTTON","Start",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,445,290,60,60, hWnd,(HMENU) ID_START,hInst,0);
exitbut=CreateWindow("BUTTON","Exit",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,515,325,60,25, hWnd,(HMENU) ID_KILL,hInst,0);
demobut=CreateWindow("BUTTON","Demo",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,515,290,60,25, hWnd,(HMENU) ID_DEMO,hInst,0);
Uplu=CreateWindow("BUTTON","+",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,592,85, 20,65, hWnd,(HMENU) ID_UPLU,hInst,0);
Umin=CreateWindow("BUTTON","-",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,592,155,20,65, hWnd,(HMENU) ID_UMIN,hInst,0);
Tplu=CreateWindow("BUTTON","+",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,320,270,65,15, hWnd,(HMENU) ID_TPLU,hInst,0);
Tmin=CreateWindow("BUTTON","-",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,250,270,65,15, hWnd,(HMENU) ID_TMIN,hInst,0);
InvalidateRect(hGrWnd, NULL, true);
SendMessage(hGrWnd, WM_PAINT, NULL, NULL);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0l;
}
int sendSet(mess *MESS)
{
DWORD namePort[5];
switch (SendMessage(hCombo3, CB_GETCURSEL, 0, 0L))
{
case 0: memcpy(namePort,"Com1",5); break;
case 1: memcpy(namePort,"Com2",5); break;
case 2: memcpy(namePort,"Com3",5); break;
case 3: memcpy(namePort,"Com4",5); break;
}
COMport = CreateFile((LPCSTR)namePort,GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (COMport == INVALID_HANDLE_VALUE)
{
MessageBox(hWnd, "Error opening COM port", "Error", MB_OK);
return false;
}
DCB dcbComm;
BOOL fSuccess;
fSuccess = GetCommState(COMport, &dcbComm);
if (!fSuccess)
{
MessageBox(hWnd, "Error configurating COM port", "Error", MB_OK);
CloseHandle(COMport);
return false;
}
PurgeComm(COMport, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
memset(&dcbComm,0,sizeof(dcbComm));
dcbComm.DCBlength = sizeof(DCB);
GetCommState(COMport, &dcbComm);
dcbComm.BaudRate = CBR_115200;
dcbComm.ByteSize = 8;
dcbComm.Parity = NOPARITY;
dcbComm.StopBits = ONESTOPBIT;
dcbComm.fAbortOnError = TRUE;
dcbComm.fDtrControl = DTR_CONTROL_DISABLE;
dcbComm.fRtsControl = RTS_CONTROL_DISABLE;
dcbComm.fBinary = TRUE;
dcbComm.fParity = FALSE;
dcbComm.fInX = dcbComm.fOutX = FALSE;
dcbComm.XonChar = 0;
dcbComm.XoffChar = BYTE(0xff);
dcbComm.fErrorChar = FALSE;
dcbComm.fNull = FALSE;
dcbComm.fOutxCtsFlow = FALSE;
dcbComm.fOutxDsrFlow = FALSE;
dcbComm.XonLim = 128;
dcbComm.XoffLim = 128;
COMMTIMEOUTS TimeOuts;
SetCommMask(COMport, EV_RXCHAR);
SetupComm(COMport, 1500, 1500);
TimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
TimeOuts.ReadTotalTimeoutMultiplier = 0;
TimeOuts.ReadTotalTimeoutConstant = TIMEOUT;
TimeOuts.WriteTotalTimeoutMultiplier = 0;
TimeOuts.WriteTotalTimeoutConstant = TIMEOUT;
if (!SetCommTimeouts(COMport, &TimeOuts))
{
MessageBox(hWnd, "Error configurating COM port", "Error", MB_OK);
CloseHandle(COMport);
return false;
}
if (!SetCommState(COMport, &dcbComm))
{
MessageBox(hWnd, "Error configurating COM port", "Error", MB_OK);
CloseHandle(COMport);
return false;
}
DWORD length = 3;
OVERLAPPED inf= {0};
WriteFile(COMport, (LPCVOID) MESS, length, NULL, &inf);
unsigned char Ready;
Recieve(&Ready);
if (Ready!=0x33)
{
MessageBox(hWnd, "Error establishing connection", "Error", MB_OK);
CloseHandle(COMport);
return false;
}
RecieveData();
if (old_freq!= SendMessage(hCombo2, CB_GETCURSEL, 0, 0L))
switch (old_freq = SendMessage(hCombo2, CB_GETCURSEL, 0, 0L))
{
case 0: Tscale=(double)10; break;
case 1: Tscale=(double)500/150; break;
case 2: Tscale=(double)500/275; break;
case 3: Tscale=(double)500/350; break;
case 4: Tscale=(double)500/500; break;
}
CloseHandle(COMport);
InvalidateRect(hGrWnd, NULL, true);
SendMessage(hGrWnd, WM_PAINT, NULL, NULL);
return 0;
}
void RecieveData()
{
unsigned char Hbyte,Lbyte;
for (i=0;i<samp;i++)
{
Recieve(&Hbyte);
Recieve(&Lbyte);
buffer[i]=Lbyte + 256*Hbyte;
}
}
int Recieve(unsigned char *byte)
{
DWORD begin = GetTickCount();
DWORD read=0;
while((GetTickCount()-begin) < DWORD(TIMEOUT))
{
if(ReadFile(COMport, byte, 1, &read, NULL))
{
return 1;
}
}
MessageBox(hWnd, "Error recieving data", "Error", MB_OK);
return 0;
}
void demo()
{
for(i=0;i<samp;i++)
buffer[i]=fabs(65535*sin((double)i/30));
InvalidateRect(hGrWnd, NULL, true);
SendMessage(hGrWnd, WM_PAINT, NULL, NULL);
}
LRESULT CALLBACK DiagramProc(HWND hGrWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT ps;
int k, j;
switch(msg)
{
case WM_PAINT:
r.bottom=360;
r.left=35;
r.right=85;
r.top=30;
hDC=BeginPaint(hGrWnd,&ps);
sprintf(buf,"%.2f",3/Uscale);
strcat(buf, " V");
DrawText(hDC,buf,strlen(buf),&r,DT_CENTER);
r.top=225;
r.bottom=390;
r.left=480;
r.right=570;
switch (SendMessage(hCombo2, CB_GETCURSEL, 0, 0L))
{
case 0: sprintf(buf,"%.2f",(float)10/Tscale); break;
case 1: sprintf(buf,"%.2f",(float)500/150/Tscale); break;
case 2: sprintf(buf,"%.2f",(float)500/275/Tscale); break;
case 3: sprintf(buf,"%.2f",(float)500/350/Tscale); break;
case 4: sprintf(buf,"%.2f",(float)1/Tscale); break;
}
strcat(buf, " ms");
DrawText(hDC,buf,strlen(buf),&r,DT_CENTER);
MoveToEx(hDC,0, 0,0);
LineTo(hDC, dwidth-1, 0);
LineTo(hDC, dwidth-1, dheight-1);
LineTo(hDC, 0, dheight-1);
LineTo(hDC, 0, 0);
MoveToEx(hDC,30, 30,0);
LineTo(hDC, 30, dheight-30);
LineTo(hDC, dwidth-30, dheight-30);
LineTo(hDC, dwidth-30, dheight-31);
LineTo(hDC, 29, dheight-31);
LineTo(hDC, 29, 29);
MoveToEx(hDC,dwidth-40, dheight-35,0);
LineTo(hDC, dwidth-40, dheight-25);
MoveToEx(hDC,30, 50,0);
LineTo(hDC, dwidth-30, 50);
MoveToEx(hDC,30, dheight/2+10,0);
LineTo(hDC, dwidth-30, dheight/2+10);
{
double U, T;
for(i=1; i<samp-1; i++)
{
U=buffer[i]*(dheight-80)*Uscale/65535;
T=(i-1)*Tscale;
MoveToEx(hDC,T+30,-U + dheight-30,0);
U=buffer[i+1]*(dheight-80)*Uscale/65535;
T=(i)*Tscale;
LineTo(hDC, T+30, -U + dheight-30);
}
}
EndPaint(hGrWnd,&ps);
break;
case WM_SIZE:
dwidth = LOWORD(lParam);
dheight = HIWORD(lParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hGrWnd, msg, wParam, lParam);
}
return 0;
}
Дата добавления: 2015-07-16; просмотров: 53 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
Организация настроек Com-port в графическом интерфейсе. | | | Исходные тексты приложения для МК. |