📄 scope.c
字号:
////////////////////////////////////////////////////////////////////////////////
//
// Filename: scope.c
//
// This program demonstrates the Standard Instrument Control
// Library within a Windows Program.
//
// The program uses a new addressing feature for the SICL iopen()
// function called "symbolic naming". See to the Read Me First file
// for an explanation of this feature.
//
// This program requires two devices to run:
// 54601A digitizing oscilloscope (or compatible scope)
// a printer capable of printing in HP RASTER GRAPHICS STANDARD
// (e.g. ThinkJet printers)
// An optional multimeter such as an HP 34401A may also be connected.
//
// The windows application contains an "Action" menu with four
// commands:
// "get_voltage" Take voltage rading from multimeter
// "perform_meas" Program scope to make measurement, then upload results
// "show_header" Show scope settings
// "print_disp" Command scope to print its display.
//
// The commands that are sent to the scope are device dependent, and
// are found in the manual for the scope.
//
// This program is designed to illustrate several individual SICL
// commands and is not meant to be an example of a robust Windows
// application.
////////////////////////////////////////////////////////////////////////////////
#include <string.h> // strlen()
#include <stdlib.h> // exit()
#include <stdio.h> // sprintf()
#include <windows.h> // Windows API declarations
#include "sicl.h" // Standard Instrument Control Library routines
#include "scope.h" // message defines
// function prototypes
long FAR PASCAL _export WndProc(HWND hWnd, unsigned iMessage, unsigned wParam, long lParam);
void enable_io_menu_items(BOOL);
void SICLCALLBACK my_err_handler(INST, int);
void SICLCALLBACK my_srq_handler(INST);
void init_scope_io (void);
void close_scope (void);
void get_data (INST);
void get_voltage (void);
void show_scope_settings (void);
void print_disp (INST);
// defines
#define R_ELEMENTS 5000
#define TIMEOUT 5000 // 5 second timeout value (in ms)
#define MAX_LINES 20 // max number of lines printed on screen
// global data
HWND hWnd; // application window handle
HWND hInst;
INST scope; // session id for scope
int readings [R_ELEMENTS];
int num_lines=0; // number of text lines to output
int ioerror = 0;
unsigned char scopeidn[50];
unsigned char probe[8];
float pre [20];
char text_buf[MAX_LINES][80]; // used for displaying text output
/////////////////////////////////////////////////////////////////////
//
// WinMain:
//
// Parameters:
// hInstance - number that uniquely identifies this program
// hPrevInstance - handle of previous instance of this program
// lpszCmdLine - pointer to command line argument string
// nCmdShow - indicates how the window will be displayed
//
// Description:
// This routine implements the Windows event loop for the application.
//
/////////////////////////////////////////////////////////////////////
int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine,
int nCmdShow)
{
static char szAppName[] = "scope";
MSG msg;
WNDCLASS wndclass;
lpszCmdLine = lpszCmdLine;
if (!hPrevInstance) {
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (hInstance, "scopeicon");
wndclass.hCursor = LoadCursor ((HMODULE)NULL, IDC_ARROW);
wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = "scopemenu";
wndclass.lpszClassName = szAppName;
if (!RegisterClass (&wndclass))
return FALSE;
}
hWnd = CreateWindow (szAppName,
"scope",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
400,
(HWND)NULL,
(HMENU)NULL,
hInstance,
NULL);
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
init_scope_io();
while(GetMessage (&msg, (HWND)NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
/////////////////////////////////////////////////////////////////////
//
// WndProc:
//
// Parameters:
// hWnd - identifies the window receiving the message
// iMessage - identifies the message
// wParam - provides more information about the message
// lParam - provides more information about the message
//
// Description:
// The following routine processes window messages.
//
/////////////////////////////////////////////////////////////////////
long FAR PASCAL _export WndProc (HWND hWnd, unsigned iMessage, unsigned wParam, long lParam)
{
static int xChar;
static int yChar;
HDC hDC;
PAINTSTRUCT ps;
TEXTMETRIC tm;
int i;
switch (iMessage) {
case WM_CREATE:
hInst = ((LPCREATESTRUCT) lParam) -> hInstance;
hDC = GetDC (hWnd);
GetTextMetrics (hDC, &tm);
xChar = tm.tmAveCharWidth;
yChar = tm.tmHeight;
ReleaseDC (hWnd, hDC);
num_lines = 0; // set message output to top of window
break;
case WM_DESTROY:
close_scope();
PostQuitMessage (0);
break;
case WM_COMMAND:
if (!LOWORD (lParam)) {
switch (wParam) {
case SCOPE_EXIT:
SendMessage (hWnd, WM_CLOSE, 0, 0L);
break;
case SCOPE_READ_VOLT:
get_voltage();
break;
case SCOPE_GET_DATA:
get_data(scope);
break;
case SCOPE_HEADER:
show_scope_settings();
break;
case SCOPE_PRINT:
print_disp(scope);
break;
default:
return DefWindowProc (hWnd, iMessage, wParam, lParam);
}
} else {
return DefWindowProc (hWnd, iMessage, wParam, lParam);
}
break;
case WM_PAINT:
hDC = BeginPaint (hWnd, &ps);
for (i = 0; i <num_lines; i++) {
TextOut(hDC, xChar, yChar * i, text_buf[i], strlen(text_buf[i]));
}
EndPaint (hWnd, &ps);
break;
default:
return DefWindowProc (hWnd, iMessage, wParam, lParam);
}
return 0L;
}
/////////////////////////////////////////////////////////////////////
//
// enable_io_menu_items:
//
// Parameters:
// enable_menus - TRUE to enable menus items, FALSE to disable
//
// Description:
// This routine is used to disable I/O menu selections while
// calls to the Standard Instrument Control Library are in
// progress.
//
/////////////////////////////////////////////////////////////////////
void enable_io_menu_items(BOOL enable_menus)
{
HMENU hMenu;
unsigned io_menu_state;
hMenu = GetMenu(hWnd);
if (enable_menus && !ioerror)
io_menu_state = MF_ENABLED;
else
io_menu_state = MF_GRAYED;
EnableMenuItem(hMenu, SCOPE_READ_VOLT, io_menu_state);
EnableMenuItem(hMenu, SCOPE_GET_DATA, io_menu_state);
EnableMenuItem(hMenu, SCOPE_PRINT, io_menu_state);
}
/////////////////////////////////////////////////////////////////////
//
// my_err_handler:
//
// Parameters:
// id - identifies the I/O session in which an error occured.
// err - the error that occured
//
// Description:
// This routine is installed with ionerror() and is called when an
// error occurs in a call to the Standard Instrument Control Library.
// It will cause a message to be written to the application
// window and will prevent any further actions.
//
/////////////////////////////////////////////////////////////////////
void SICLCALLBACK my_err_handler(INST id, int error)
{
HMENU hMenu;
sprintf(text_buf[num_lines++],
"session id=%d, error = %d:%s", id, error, igeterrstr(error));
sprintf(text_buf[num_lines++], "Select `File | Exit' to exit program!");
InvalidateRect(hWnd, NULL, TRUE);
// If error is from scope, disable I/O actions by graying out menu picks.
if (id == scope) {
hMenu = GetMenu(hWnd);
EnableMenuItem(hMenu, SCOPE_READ_VOLT, MF_GRAYED);
EnableMenuItem(hMenu, SCOPE_GET_DATA, MF_GRAYED);
EnableMenuItem(hMenu, SCOPE_HEADER, MF_GRAYED);
EnableMenuItem(hMenu, SCOPE_PRINT, MF_GRAYED);
// set flag indicating that a scope error occurred.
ioerror = 1;
}
// set flag indicating that a scope error occurred.
ioerror = 1;
}
/////////////////////////////////////////////////////////////////////
//
// my_srq_handler:
//
// Parameters:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -