📄 minst.c
字号:
#ifdef WIN32
#include <windows.h>
#include <commctrl.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <errno.h>
#ifdef WIN32
#include <direct.h>
#include "decl-32.h"
#endif
#ifdef LINUX
#include "ugpib.h"
#include "termios.h"
#include <fcntl.h>
#endif
#include <stdarg.h>
#include <ctype.h>
#include "wlantype.h"
#include "athreg.h"
#include "manlib.h"
#include "manlibInst.h"
#include "mInst.h"
#include "mConfig.h"
#ifndef LINUX
#include "addnl_inst/usb_pm.h"
#endif
#ifdef LINUX
#define sleep Sleep // redifining sleep to avoid conflict with unix sleep call
#endif
// Globals
#ifdef LINUX
#define COM1 "/dev/ttyS0"
#define COM2 "/dev/ttyS1"
struct termios oldtio;
#else
HANDLE hCom;
DCB dcb;
COMMTIMEOUTS timeouts;
#endif
A_BOOL fConnected = FALSE;
int nComErr;
// device numbers
int spa, pm, att, ps;
// model numbers
int tmp_model, spa_model, pm_model, att_model, ps_model;
void sleep( clock_t wait ) {
clock_t goal;
goal = wait + clock();
while( goal > clock() );
}
MANLIB_API void gpibSendIFC(const int board)
{
SendIFC( board );
}
MANLIB_API long gpibRSC(const int board, const int v)
{
return(ibrsc(board, v));
}
MANLIB_API long gpibSIC(const int board)
{
return(ibsic(board));
}
MANLIB_API long gpibClear(const int ud) {
return(ibclr ( ud ));
}
MANLIB_API long gpibRSP(const int ud, char *spr) {
return(ibrsp ( ud, spr ));
}
MANLIB_API long gpibONL(const int ud, const int v) {
return(ibonl ( ud, v ));
}
MANLIB_API long gpibWrite(const int ud, char *wrt) {
ibwrt ( ud, wrt, strlen(wrt) );
return ibcntl;
}
MANLIB_API char *gpibRead(const int ud, ... ) {
static char rd[512];
va_list vl;
long length;
va_start( vl, ud );
length = va_arg( vl, long );
va_end( vl );
ibrd ( ud, rd, ( length == 0L ? 511L : length ) );
rd[ibcntl] = '\0';
return rd;
}
MANLIB_API char *gpibQuery(const int ud, char *wrt, ... ) {
static char rd[512];
va_list vl;
long length;
gpibWrite ( ud, wrt );
va_start( vl, wrt );
length = va_arg( vl, long );
va_end( vl );
ibrd ( ud, rd, ( length == 0L ? 511L : length ) );
// trap gpib error and invalid ibcntl
if (((ibsta & 0x8000) != 0) && (( ibcntl > 509)||(ibcntl < 0))) {
rd[510] = '\0';
} else {
rd[ibcntl] = '\0';
}
return rd;
}
char *qq( char *format, ... ) {
static char buf[512];
va_list vl;
va_start( vl, format );
vsprintf( buf, format, vl );
va_end( vl );
return buf;
}
#ifndef LINUX
HANDLE com_open(char *port, const int model) {
// reset error byte
nComErr = 0;
// device handle
hCom = CreateFile( port, // port name
GENERIC_READ | GENERIC_WRITE, // allow r/w access
0, // always no sharing
0, // no security atributes for file
OPEN_EXISTING, // always open existing
FILE_ATTRIBUTE_NORMAL, // nonoverlapped operation
0); // always no file template
if (hCom == INVALID_HANDLE_VALUE) {
nComErr = nComErr | COM_ERROR_GETHANDLE;
return 0;
}
// port configuration
FillMemory (&dcb, sizeof(dcb),0);
dcb.DCBlength = sizeof(dcb);
if (!BuildCommDCB("9600,n,8,1", &dcb)) {
nComErr = nComErr | COM_ERROR_BUILDDCB;
return 0;
}
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDsrSensitivity = FALSE;
if (!SetCommState(hCom, &dcb)) {
nComErr = nComErr | COM_ERROR_CONFIGDEVICE;
return 0;
}
if (!SetupComm(hCom, READ_BUF_SIZE, WRITE_BUF_SIZE)) {
nComErr = nComErr | COM_ERROR_CONFIGBUFFERS;
return 0;
}
if (!EscapeCommFunction(hCom, SETDTR)) {
nComErr = nComErr | COM_ERROR_SETDTR;
return 0;
}
// connection flag
fConnected = 1;
tmp_model = model;
// set mask to notify thread if a character was received
if (!SetCommMask(hCom, EV_RXCHAR)) {
// error setting communications event mask
nComErr = nComErr | COM_ERROR_READ;
return 0;
}
return hCom;
}
int com_close(const HANDLE ud) {
// reset error byte
nComErr = 0;
if (!fConnected) return 0;
// connection flag
fConnected = 0;
// port configuration
if (!EscapeCommFunction(ud, CLRDTR)) {
nComErr = nComErr | COM_ERROR_CLEARDTR;
return 0;
}
if (!PurgeComm(ud, PURGE_RXABORT | PURGE_RXCLEAR |
PURGE_TXABORT | PURGE_TXCLEAR)) {
nComErr = nComErr | COM_ERROR_PURGEBUFFERS;
return 0;
}
// device handle
CloseHandle(ud);
return 1;
}
unsigned long com_write(const HANDLE ud, char *wrt) {
unsigned long cnt;
// reset error byte
nComErr = 0;
// write the com port
if (!WriteFile(ud, wrt, strlen(wrt), &cnt, NULL)) nComErr = nComErr | COM_ERROR_WRITE;
return cnt;
}
char *com_read(const HANDLE ud) {
static char rd[50];
unsigned long dwCommEvent;
unsigned long dwRead, pos = 0;
char chRead;
// reset error byte
nComErr = 0;
// wait for comm event
if (WaitCommEvent(ud, &dwCommEvent, NULL)) {
do {
// read receive buffer
if (ReadFile(ud, &chRead, 1, &dwRead, NULL)) {
rd[pos] = chRead;
pos++;
}
else {
nComErr = nComErr | COM_ERROR_READ;
return '\0';
}
} while (chRead != 0x06); // stop reading at termination character
}
else {
nComErr = nComErr | COM_ERROR_READ;
return '\0';
}
rd[pos] = '\0';
return rd;
}
#else
HANDLE com_open(char *port, const int model)
{
HANDLE fd;
struct termios newtio;
nComErr = 0;
fd = -1;
if (strcmp(port,"COM1") == 0) {
fd = open(COM1, O_RDWR | O_NOCTTY);
}
if (strcmp(port,"COM2") == 0) {
fd = open(COM2, O_RDWR | O_NOCTTY);
}
if (fd < 0) {
nComErr = nComErr | COM_ERROR_GETHANDLE;
return 0;
}
tcgetattr(fd,&oldtio);
memset(&newtio,0,sizeof(newtio));
/*
* BAUD 9600
* CS8 8 Bit, no parity, 1 stop nit
* CLOCAL local connection
* CREAD enable receiving
*/
newtio.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
/*
* Ignore parity and map CR to NL
*/
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag = 0;
/*
* enable canonical input
*/
newtio.c_lflag = ICANON;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &newtio);
/*
* connection flag
*/
fConnected = 1;
tmp_model = model;
return fd;
}
int com_close(const HANDLE fd)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -