⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainprog.cpp

📁 一个完整的RTU程序源码,用DOS平台,支持16串口,在天津港用的很多,8个规约103,modbus,cdt,1801,u4f
💻 CPP
字号:
#include "sc_cfg.h"
#include "common.h"
#include "class.h"
#include "extern.h"

CWindows	win;

#ifdef CACULATE
CCaculate caculate;
#endif

CProtocol *PortArray[MAX_PORT];

INT16U CardStatusYx;
INT16U ParaStatusYx;
BOOLEAN RunState;
sclock boot_time[BOOT_LOG_SIZE];
INT16U boot_record_count;

static void SystemInit(void);
static void InitCard(void);
static void ResetCard(void);
void Watchdog(void);
void ClearWatchdog(void);
static void boot_record(void);
static void DetectCard(void);
static void SCQuit(void);
static void Interface(void);

static void SCQuit(void)
{
	int i;
	for (i = 0; i < MAX_PORT; i++)
		delete PortArray[i];
	ClearWatchdog();
	ResetTimer();
	ResetCard();
	textbackground(BLACK);
	textcolor(WHITE);
	clrscr();
}

static void boot_record(void)
{
	char buf[MAXFILEBUF];
	FILE *fp;
	int start, i;
	sclock tempclk[BOOT_LOG_SIZE];

	if ((fp = fopen("bootlog.cfg", "r+t")) == NULL) {
		return;
	}
	fscanf(fp, "%*s%*s%u\n", &start);
	fscanf(fp, "%*s%*s%u\n", &boot_record_count);
	if (boot_record_count < BOOT_LOG_SIZE) boot_record_count++;
	for (i = 0; i < boot_record_count; i++) {
		if (i == start) {
			fseek(fp, 0L, SEEK_CUR);
			ReadTime(&tempclk[i]);
			fprintf(fp, "%d   %4d %2d %2d %2d %2d %2d\n", start, tempclk[i].year, tempclk[i].month,
						tempclk[i].day, tempclk[i].hour, tempclk[i].minute, tempclk[i].second);
			fseek(fp, 0L, SEEK_CUR);
			continue;
		}
		fscanf(fp, "%*d%4d%2d%2d%2d%2d%2d\n", &tempclk[i].year, &tempclk[i].month, &tempclk[i].day,
					&tempclk[i].hour, &tempclk[i].minute, &tempclk[i].second);
	}
	fseek(fp, 0L, SEEK_SET);
	if (++start >= BOOT_LOG_SIZE) start = 0;
	fprintf(fp, "start = %2d\n", start);
	fprintf(fp, "count = %2d\n", boot_record_count);
	fclose(fp);
	for (i = 0; i < boot_record_count; i++) {
		if (boot_record_count < BOOT_LOG_SIZE)
			memcpy(&boot_time[i], &tempclk[i], sizeof(sclock));
		else {
			memcpy(&boot_time[i], &tempclk[start], sizeof(sclock));
			if (++start >= BOOT_LOG_SIZE) start = 0;
		}
	}
}

static void SystemInit(void)
{
	int i;

	LoadConfig();
	boot_record();
	win.initial();
	board_yx_init();
	board_yc_init();
	board_ym_init();
	board_yk_init();
	CommInit();

	for (i = 0; i < MAX_PORT; i++) {
		PortArray[i] = (CProtocol *) NULL;

		switch (PortSet[i].Protocol) {
			case NEW_BB_CDT:
				PortArray[i] = (CProtocol *) new CNbb(i);
				break;
			case IEC103:
				PortArray[i] = (CProtocol *) new CIec103M(i);
				break;
			case DL645:
				PortArray[i] = (CProtocol *) new CDL645(i);
				break;
			case DCJZ:
				PortArray[i] = (CProtocol *) new CJZDC(i);
				break;
			case TTC31X:
				PortArray[i] = (CProtocol *) new CTTC31X(i);
				break;
			case DISA:
				PortArray[i] = (CProtocol *) new CDISA3(i);
				break;
			case STMODBUS:
				PortArray[i] = (CProtocol *) new CSTModbus(i);
				break;
		//	case SPABUS:
		//	PortArray[i] = (CProtocol *) new CSPABUS(i);
		//		break;
//			case MODBUS:
//				PortArray[i] = (CProtocol *) new CModbus(i);
//				break;
//			case GST:
//				PortArray[i] = (CProtocol *) new CGst(i);
//				break;
//			case LFP:
//				PortArray[i] = (CProtocol *) new CLfp(i);
//				break;
//			case DCMODBUS:
//				PortArray[i] = (CProtocol *) new CDCModbus(i);
//				break;
			default:
				break;
		}
	}

	DetectCard();
	InitCard();
	SetTimer();						// setup int8 interrupt
	rtdbmsinit();
}

void main(void)
{
	RunState = TRUE;
	SystemInit();
	do {
//		Watchdog();
		selectayc();
		doyk();
#ifdef CACULATE
	caculate.deal();
#endif
		yxgetdeal();
		ymdeal();
		for (int i = 0; i < MAX_PORT; i++) {
			if (NULL == PortArray[i]) continue;
			if (PortSet[i].isWork && PortSet[i].isReady)
				PortArray[i]->deal();
		}
		ycdeal();
		Interface();
	} while (RunState == TRUE);
	SCQuit();
}

static void DetectCard(void)
{
	int i;
	CardStatusYx = ParaStatusYx = 0;

	CardStatusYx &= ~0x3;
	for (i = 0; i < _sio1total + _sio2total; i++) {
		if (inp(PortSet[i].BaseAddr + 2) != 0xff) {
			PortSet[i].isReady = TRUE;
			if (i < _sio1total)	CardStatusYx |= 0x1;
			else CardStatusYx |= 0x2;
		}
		else PortSet[i].isReady = FALSE;
	}

	if (inp(_adbase + 2) != 0xff) CardStatusYx |= 0x4;
	else CardStatusYx &= ~0x4;							// 12 bit ad card

	if (plattype == MPC104) {
		outp(_dio1base + 9, 0x00);
	}

	outp(_dio1base + 3, 0x80);
	outp(_dio2base + 3, 0x80);

	if (plattype == MPC104) {
		outp(_dio1base + 8, 0xff);
		outp(_dio1base + 9, 0xff);
	}

	outp(_dio1base, 0xaa);
	if (inp(_dio1base) == 0xaa) ParaStatusYx |= 0x1;
	else ParaStatusYx &= ~0x1;

	outp(_dio1base + 1, 0xaa);
	if (inp(_dio1base + 1) == 0xaa) ParaStatusYx |= 0x2;
	else ParaStatusYx &= ~0x2;

	outp(_dio1base + 2, 0xaa);
	if (inp(_dio1base + 2) == 0xaa) ParaStatusYx |= 0x4;
	else ParaStatusYx &= ~0x4;

	if (ParaStatusYx & 0x0007) CardStatusYx |= 0x8;
	else CardStatusYx &= ~0x8;							// 24 bit parallel card (1)

	outp(_dio2base, 0xaa);
	if (inp(_dio2base) == 0xaa) ParaStatusYx |= 0x8;
	else ParaStatusYx &= ~0x8;

	outp(_dio2base + 1, 0xaa);
	if (inp(_dio2base + 1) == 0xaa) ParaStatusYx |= 0x10;
	else ParaStatusYx &= ~0x10;

	outp(_dio2base + 2, 0xaa);
	if (inp(_dio2base + 2) == 0xaa) ParaStatusYx |= 0x20;
	else ParaStatusYx &= ~0x20;

	if (ParaStatusYx & 0x0038) CardStatusYx |= 0x10;
	else CardStatusYx &= ~0x10;							// 24 bit parallel card (2)
}

static void Interface(void)
{
	INT8U keychar;
	INT16U rdkey;

	win.UpdateClock();

	if (win.window_flag == TRUE) win.UpdateInfo();

	if (bioskey(1) == 0) return;
	else {
		rdkey = bioskey(0);
		switch (rdkey) {
			case UPARR:	 win.mainuparr();	 break;
			case DNARR:	 win.maindnarr();	 break;
			case RTARR:	 win.mainrtarr();	 break;
			case LTARR:	 win.mainltarr();	 break;
			case ENTER:	 win.mainenter();	 break;
			case ESC:		 win.mainesc();		 break;
			case F2:		 win.mainf2();		 break;
			case F4:		 win.mainf4();		 break;
			case F5:     win.mainf5();	   break;
			case F6:     win.mainf6();	   break;
			case F7:	   win.mainf7();	   break;
			case F9:	   win.mainf9();	   break;
			case PGUP:	 win.mainpgup();	 break;
			case PGDN:	 win.mainpgdn();	 break;
			case F10:		 RunState = FALSE; break;
			case BACKSP: win.KeyDrop();		 break;
		}
		keychar = rdkey & 0xff;

		if (keychar == '.' || keychar == '-' || keychar == '+'
			|| isalpha(keychar) || isdigit(keychar))
			win.parakeyin(toupper(keychar));

		win.UpdateWindow();
	}
}

static void InitCard(void)
{
	if (CardStatusYx & 0x1) InitMoxa1();
	if (CardStatusYx & 0x2) InitMoxa2();
	if (CardStatusYx & 0x4) InitAD();
	if (plattype == MPC104) {
		outp(_dio1base + 9, 0x00); // disable all gates
	}

	outp(_dio1base + 3, 0x90); // init 8255 #0
	outp(_dio2base + 3, 0x90);
	outp(_dio2base + 1, 0x00);
	outp(_dio2base + 2, 0x8f);

	if (plattype == MPC104) {
		outp(_dio1base + 8, 0x1b);
		outp(_dio1base + 9, 0xff);
	}
}

static void ResetCard(void)
{
	if (CardStatusYx & 0x1) ResetMoxa1();
	if (CardStatusYx & 0x2) ResetMoxa2();
	if (plattype == MPC104) {
		outp(_dio1base + 8, 0x00);
		outp(_dio1base + 9, 0x00);
	}
	outp(_dio1base + 3, 0x00);
	outp(_dio2base + 3, 0x00);
}

void Watchdog(void)
{
	if (plattype == MPC104) outp(0x443, 1);
	if (plattype == MPCMIC) outp(0x443, 1); //inportb(0x443); mic2340 use
}

void ClearWatchdog(void)
{
	if (plattype == MPC104) outp(0x443, 0);
	if (plattype == MPCMIC) inp(0x443); //inportb(0x043); mic2340 use
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -