您的位置:首页 > 博客中心 > 数据库 >

一种远程修改sqlite数据库的方法

时间:2022-03-10 17:45

一、实际需求

     工业设备DA660是专门用来实现工业上可编程设备之间交换信息的交换机。现在要使得DA660采集下行设备的实时数据,然后传送到上位机显示。上位机还可以远程修改DA660的数据库配置。

二、数据库设计

     我在DA660中设计了sqlite3数据库,名为da660.db,里面存放很多表,其中有一张表示baseinfo,其结构定义如下:

gxlsystem.com,布布扣

通道号是从0到15之间的正整数,从机地址是从0到256之间的正整数,设备类型定义为0到5之间的正整数,设备类型可从{“xz2000”, “xz3000”, “xz3100”, “xz3500”,“xz6000”}匹配得到,波特率定义为0到4之间的正整数,波特率可从{“2400”, “4800”,“9600”,“192000”}。
注意:设备类型和波特率这样定义的原因是即节省了存储空间,又便于网络中传输和解析。

三、 本地数据库创建

/*
 * Database Init tool
 * */
#include <stdio.h>
#include <stdlib.h>
#include "sqlite3.h"
sqlite3 *db;
/*
 * return 0 if database can be created  successfully else return -1
 * */
int InitDatabase() {
	int rc; 
	char *sql;
	char *zErrMsg = NULL;    // error string 

	rc = sqlite3_open("da660.db", &db);  // open or create a database
	if(rc) {
		perror("can not create or open da660.db...!\n");
		rc = -1;
	}
	else {
		/*
		 * create baseinfo table
		 * */
		sql = "CREATE TABLE baseinfo(port INTEGER, addr INTEGER, type INTEGER, bps INTERGER, primary key(port, addr));";  
		rc = sqlite3_exec(db, sql, 0, 0, &zErrMsg);   // create data table
		if(rc != SQLITE_OK) {
			perror("create baseinfo table failed... !\n");
			rc = -1;
		}
		/*
		 * create xz3100 realtime_data table
		 * */
		sql = "CREATE TABLE xz3100_realtime_data(port INTEGER, addr INTEGER, 			   D0 INTEGER, D1 INTERGER, D2 INTEGER, D3 INTEGER, D4 INTEGER, D5 INTEGER, D6 INTEGER, D7 INTEGER, D8 INTEGER, D9 INTEGER, 			   D10 INTEGER, D11 INTERGER, D12 INTEGER, D13 INTEGER, D14 INTEGER, D15 INTEGER, D16 INTEGER, D17 INTEGER, D18 INTEGER, D19 INTEGER, 			   D20 INTEGER, D21 INTERGER, D22 INTEGER, D23 INTEGER, D24 INTEGER, D25 INTEGER, D26 INTEGER, D27 INTEGER, D28 INTEGER, D29 INTEGER, 			   D30 INTEGER, D31 INTERGER, D32 INTEGER, D33 INTEGER, D34 INTEGER, D35 INTEGER, D36 INTEGER, D37 INTEGER, D38 INTEGER, D39 INTEGER, 			   D40 INTEGER, D41 INTERGER, D42 INTEGER, D43 INTEGER, D44 INTEGER, D45 INTEGER, D46 INTEGER, D47 INTEGER, D48 INTEGER, D49 INTEGER, 			   D50 INTEGER, D51 INTERGER, D52 INTEGER, D53 INTEGER, D54 INTEGER, D55 INTEGER, D56 INTEGER, D57 INTEGER,									   primary key(port, addr));";  
		rc = sqlite3_exec(db, sql, 0, 0, &zErrMsg);   // create data table
		if(rc != SQLITE_OK) {
			perror("create xz3100 realtime data table failed... !\n");
			rc = -1;
		}
		/*
		 * create xz3100 SOE table
		 * */
		sql = "CREATE TABLE xz3100_soe_data(port INTEGER, addr INTEGER, 			   D0 INTEGER, D1 INTERGER, D2 INTEGER, D3 INTEGER, D4 INTEGER, D5 INTEGER, D6 INTEGER, D7 INTEGER,  D8 INTEGER,  D9 INTEGER,  			   D10 INTEGER, D11 INTERGER,  			   primary key(port, addr));";  
		rc = sqlite3_exec(db, sql, 0, 0, &zErrMsg);   // create data table
		if(rc != SQLITE_OK) {
			perror("create xz3100 soe table failed... !\n");
			rc = -1;
		}
		/*
		 * create xz3100 action table 
		 * */
		sql = "CREATE TABLE xz3100_action_data(port INTEGER, addr INTEGER, 			   D0 INTEGER, D1 INTERGER, D2 INTEGER, D3 INTEGER, D4 INTEGER, D5 INTEGER, D6 INTEGER, D7 INTEGER, D8 INTEGER, D9 INTEGER,  			   D10 INTEGER, D11 INTERGER,  			   primary key(port, addr));";  
		rc = sqlite3_exec(db, sql, 0, 0, &zErrMsg);   // create data table
		if(rc != SQLITE_OK) {
			perror("create xz3100 action table failed... !\n");
			rc = -1;
		}
		/*
		 * create xz3100 opeing table 
		 * */
		sql = "CREATE TABLE xz3100_opening_data(port INTEGER, addr INTEGER, 			   D0 INTEGER, D1 INTERGER, D2 INTEGER, D3 INTEGER, D4 INTEGER, D5 INTEGER, D6 INTEGER, D7 INTEGER, D8 INTEGER, D9 INTEGER,  			   D10 INTEGER, D11 INTERGER, D12 INTEGER, D13 INTEGER, D14 INTEGER, D15 INTEGER, D16 INTEGER, D17 INTEGER, D18 INTEGER, D19 INTEGER, 			   primary key(port, addr));";  
		rc = sqlite3_exec(db, sql, 0, 0, &zErrMsg);   // create data table
		if(rc != SQLITE_OK) {
			perror("create xz3100 opening table failed... !\n");
			rc = -1;
		}
		/*
		 * create xz3100 arg_data table
		 * */
		sql = "CREATE TABLE xz3100_arg_data(port INTEGER, addr INTEGER, 			   D0 INTEGER, D1 INTERGER, D2 INTEGER, D3 INTEGER, D4 INTEGER, D5 INTEGER, D6 INTEGER, D7 INTEGER, D8 INTEGER, D9 INTEGER, 			   D10 INTEGER, D11 INTERGER, D12 INTEGER, D13 INTEGER, D14 INTEGER, D15 INTEGER, D16 INTEGER, D17 INTEGER, D18 INTEGER, D19 INTEGER, 			   D20 INTEGER, D21 INTERGER, D22 INTEGER, D23 INTEGER, D24 INTEGER, D25 INTEGER, D26 INTEGER, D27 INTEGER, D28 INTEGER, D29 INTEGER, 			   D30 INTEGER, D31 INTERGER, D32 INTEGER, D33 INTEGER, D34 INTEGER, D35 INTEGER, D36 INTEGER, D37 INTEGER, D38 INTEGER, D39 INTEGER, 			   D40 INTEGER, D41 INTERGER, D42 INTEGER, D43 INTEGER, D44 INTEGER, D45 INTEGER, D46 INTEGER, D47 INTEGER, D48 INTEGER, D49 INTEGER, 			   D50 INTEGER, D51 INTERGER, D52 INTEGER, D53 INTEGER, D54 INTEGER, D55 INTEGER, D56 INTEGERR,									   primary key(port, addr));";  
		rc = sqlite3_exec(db, sql, 0, 0, &zErrMsg);   // create data table
		if(rc != SQLITE_OK) {
			perror("create xz3100 arguments data table failed... !\n");
			rc = -1;
		}
		/*
		 * create xz3100 statistic_data table
		 * */
		sql = "CREATE TABLE xz3100_statistic_data(port INTEGER, addr INTEGER, 			   D0 INTEGER, D1 INTERGER, D2 INTEGER, D3 INTEGER, D4 INTEGER, D5 INTEGER, D6 INTEGER, D7 INTEGER, D8 INTEGER, D9 INTEGER, 			   D10 INTEGER, D11 INTERGER, D12 INTEGER, D13 INTEGER, D14 INTEGER, D15 INTEGER, D16 INTEGER, D17 INTEGER, D18 INTEGER, D19 INTEGER, 			   D20 INTEGER, D21 INTERGER, D22 INTEGER, D23 INTEGER, D24 INTEGER, D25 INTEGER,									   primary key(port, addr));";  
		rc = sqlite3_exec(db, sql, 0, 0, &zErrMsg);   // create data table
		if(rc != SQLITE_OK) {
			perror("create xz3100 statistic data table failed... !\n");
			rc = -1;
		}
		if(!rc) {
			printf("Database Init Successfully!\n");
		}
	}
	/*
	 * never to forget to close the db
	 * */
	sqlite3_close(db);

	return rc;
}
int main() {
	if(InitDatabase()) {
		perror("Data Init failed!\n");
		exit(EXIT_FAILURE);
	}

	return 0;
}

四、本地数据库配置

/*
 * Database configure tool
 * */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#define SIZE 128
typedef unsigned char uchar;
sqlite3 *db;
sqlite3_stmt *state;  // associate with sql 
char *equipmentType[] = {"xz2000", "xz3000", "xz3100", "xz3500", "xz6000"};
char *bpsArray[] = {"2400", "4800", "9600", "192000"};
/*
 * to insert a record into baseinfo table 
 * return 0 if insert successfully else return -1
 * type and bps should be the index of corresponding array
 * */
int Insert_baseinfo(uint port, uint addr, uint type, uint bps) {
	int rc;
	char *zErrMsg = NULL;
	char insert_sql[SIZE];
	/*
	 * to open da660.db 
	 * */
	rc = sqlite3_open("da660.db", &db);
	if(rc) {
		perror("da660.db open failed...!\n");
		rc = -1;
	}
	else {
		/*
		 * insert data record
		 * */
		memset(insert_sql, 0, sizeof(insert_sql));
		sprintf(insert_sql, "INSERT INTO baseinfo(port, addr, type, bps) VALUES(%u, %u, %u, %u)", port, addr, type, bps);
		rc = sqlite3_exec(db, insert_sql, 0, 0, &zErrMsg); // execute sql statement and insert data record
		if(rc == SQLITE_OK) {
			printf("insert one data record success...!\n");
		}
		else {
			perror("insert one data record failure...!\n");
			rc = -1;
		}
		sqlite3_close(db);  // never to forget to close the db
	}

    return rc;
}
/*
 * to delete a data record from baseinfo table 
 * return 0 if insert successfully else return -1
 * */
int Delete_baseinfo(uint port, uint addr) {
	int rc;
	int nRow, nColumn;
	char *zErrMsg = NULL;
	char **dbResult;  // point to two dimension table
	char select_sql[SIZE];
	char delete_sql[SIZE];
	/*
	 * to open da660.db 
	 * */
	rc = sqlite3_open("da660.db", &db);
	if(rc) {
		perror("da660.db open failed...!\n");
		rc = -1;
	}
	else {
		/*
		 * first query the data record
		 * */
		memset(select_sql, 0, sizeof(select_sql));
		sprintf(select_sql, "select * from baseinfo where port = %u and addr = %u", port, addr);
		rc = sqlite3_get_table(db, select_sql, &dbResult, &nRow, &nColumn, &zErrMsg);
		if(rc != SQLITE_OK) {
			perror("database query error!\n");
			rc = -1;
		}
		else if(!nRow) {
			perror("data record does not exist!\n");
			rc = -1;
		}
		/*
		 * delete the data record
		 * */
		else {
			memset(delete_sql, 0, sizeof(delete_sql));
			sprintf(delete_sql, "delete from baseinfo where port = %u and addr = %u", port, addr);
			rc = sqlite3_exec(db, delete_sql, 0, 0, &zErrMsg); // execute sql statement and delete data record
			if(rc == SQLITE_OK) {
				printf("delete one data record success...!\n");
			}
			else {
				perror("delete one data record failure...!\n");
				rc = -1;
			}
		}
		sqlite3_free_table(dbResult); // never to forget to free the query table
		sqlite3_close(db);  // never to forget to close the db
	}

    return rc;
}
/*
 * query records from baseinfo table in terms with port
 * return 0 if query success else return -1
 * */
int Query_baseinfo(uint port) {
	int rc;
	int nRow, nColumn;
	int row, col;
	int index;
	uint type;   // the equipment type
	uint bps;    // the equipment bps 
	char *zErrMsg = NULL;
	char **dbResult;  // point to two dimension table
	char select_sql[SIZE];
	char *domain, *value; // the domain and value of query table
	/*
	 * to open da660
	 * */
	rc = sqlite3_open("da660.db", &db);
	if(rc != SQLITE_OK) {
		perror("da660.db open failed...!\n");
		rc = -1;
	}
	else {
		/*
		 * to select data record
		 * */
		memset(select_sql, 0, sizeof(select_sql));
		sprintf(select_sql, "select * from baseinfo where port = %u", port);
		/*
		 * to get the two dimension table, nRow means rows and nColumn means cols
		 * */
		rc = sqlite3_get_table(db, select_sql, &dbResult, &nRow, &nColumn, &zErrMsg);
		if(rc != SQLITE_OK) {
			perror("database query failed!\n");
			rc = -1;
		}
		else {
			/*
			 * query statement
			 * */
			index = nColumn;   // the first value should start from nColum
			for(row = 0;row < nRow;row++) {
				printf("-------the %d record----------\n", row + 1);
				for(col = 0;col < nColumn;col++) {
					domain = dbResult[col];
					value = dbResult[index];
					printf("\t%-5s:", domain);
					if(col == 2)  {
						type = (uint)atoi(value);
						value = equipmentType[type];
					}	
					if(col == 3) {
						bps = (uint)atoi(value);
						value = bpsArray[bps];
					}
					printf("%s\n", value);
					index++;
				}
			}
		}
	}
	sqlite3_free_table(dbResult);  // never to forget to free the query table
	sqlite3_close(db);
	
	return rc;
}
/*
 * query realtime data records from xz3100 realtime data table in terms with port and addr
 * return 0 if query success else return -1
 * */
int Query_xz3100_realtime_data(uint port, uint addr) {
	int rc;
	int nRow, nColumn;
	int row, col;
	int index;
	int type;
	char *zErrMsg = NULL;
	char **dbResult;  // point to two dimension table
	char select_sql[SIZE];
	char *domain, *value; // the domain and value of query table
	/*
	 * to open da660
	 * */
	rc = sqlite3_open("da660.db", &db);
	if(rc != SQLITE_OK) {
		perror("da660.db open failed...!\n");
		rc = -1;
	}
	else {
		/*
		 * to select data record
		 * */
		memset(select_sql, 0, sizeof(select_sql));
		sprintf(select_sql, "select * from xz3100_realtime_data where port = %u and addr = %u", port, addr);
		/*
		 * to get the two dimension table, nRow means rows and nColumn means cols
		 * */
		rc = sqlite3_get_table(db, select_sql, &dbResult, &nRow, &nColumn, &zErrMsg);
		if(rc != SQLITE_OK) {
			perror("database query failed!\n");
			rc = -1;
		}
		else {
			/*
			 * query statement
			 * */
			index = nColumn;   // the first value should start from nColum
			printf("port %d, addr %d realtime data as following:\n", port, addr);
			for(row = 0;row < nRow;row++) {
				type = 0;
				for(col = 0;col < nColumn;col++) {
					if(col > 1) {
						value = dbResult[index];
						xz3100_realtime_data_show1(type++, atoi(value));
					}
					index++;
				}
			}
		}
	}
	sqlite3_free_table(dbResult);  // never to forget to free the query table
	sqlite3_close(db);
	
	return rc;
}
/*
 * query soe data records from xz3100 soe data table in terms with port and addr
 * return 0 if query success else return -1
 * */
int Query_xz3100_soe_data(uint port, uint addr) {
	int rc;
	int nRow, nColumn;
	int row, col;
	int index;
	int type;
	char *zErrMsg = NULL;
	char **dbResult;  // point to two dimension table
	char select_sql[SIZE];
	char *domain, *value; // the domain and value of query table
	/*
	 * to open da660
	 * */
	rc = sqlite3_open("da660.db", &db);
	if(rc != SQLITE_OK) {
		perror("da660.db open failed...!\n");
		rc = -1;
	}
	else {
		/*
		 * to select data record
		 * */
		memset(select_sql, 0, sizeof(select_sql));
		sprintf(select_sql, "select * from xz3100_soe_data where port = %u and addr = %u", port, addr);
		/*
		 * to get the two dimension table, nRow means rows and nColumn means cols
		 * */
		rc = sqlite3_get_table(db, select_sql, &dbResult, &nRow, &nColumn, &zErrMsg);
		if(rc != SQLITE_OK) {
			perror("database query failed!\n");
			rc = -1;
		}
		else {
			/*
			 * query statement
			 * */
			index = nColumn;   // the first value should start from nColum
			printf("port %d, addr %d soe data as following:\n", port, addr);
			for(row = 0;row < nRow;row++) {
				type = 0;
				for(col = 0;col < nColumn;col++) {
					if(col > 1) {
						value = dbResult[index];
						xz3100_soe_data_show1(type++, atoi(value));
					}
					index++;
				}
			}
		}
	}
	sqlite3_free_table(dbResult);  // never to forget to free the query table
	sqlite3_close(db);
	
	return rc;
}
/*
 * query action data records from xz3100 action data table in terms with port and addr
 * return 0 if query success else return -1
 * */
int Query_xz3100_action_data(uint port, uint addr) {
	int rc;
	int nRow, nColumn;
	int row, col;
	int index;
	int type;
	char *zErrMsg = NULL;
	char **dbResult;  // point to two dimension table
	char select_sql[SIZE];
	char *domain, *value; // the domain and value of query table
	/*
	 * to open da660
	 * */
	rc = sqlite3_open("da660.db", &db);
	if(rc != SQLITE_OK) {
		perror("da660.db open failed...!\n");
		rc = -1;
	}
	else {
		/*
		 * to select data record
		 * */
		memset(select_sql, 0, sizeof(select_sql));
		sprintf(select_sql, "select * from xz3100_action_data where port = %u and addr = %u", port, addr);
		/*
		 * to get the two dimension table, nRow means rows and nColumn means cols
		 * */
		rc = sqlite3_get_table(db, select_sql, &dbResult, &nRow, &nColumn, &zErrMsg);
		if(rc != SQLITE_OK) {
			perror("database query failed!\n");
			rc = -1;
		}
		else {
			/*
			 * query statement
			 * */
			index = nColumn;   // the first value should start from nColum
			printf("port %d, addr %d action data as following:\n", port, addr);
			for(row = 0;row < nRow;row++) {
				type = 0;
				for(col = 0;col < nColumn;col++) {
					if(col > 1) {
						value = dbResult[index];
						xz3100_action_data_show1(type++, atoi(value));
					}
					index++;
				}
			}
		}
	}
	sqlite3_free_table(dbResult);  // never to forget to free the query table
	sqlite3_close(db);
	
	return rc;
}
/*
 * query opening data records from xz3100 action data table in terms with port and addr
 * return 0 if query success else return -1
 * */
int Query_xz3100_opening_data(uint port, uint addr) {
	int rc;
	int nRow, nColumn;
	int row, col;
	int index;
	int type;
	char *zErrMsg = NULL;
	char **dbResult;  // point to two dimension table
	char select_sql[SIZE];
	char *domain, *value; // the domain and value of query table
	/*
	 * to open da660
	 * */
	rc = sqlite3_open("da660.db", &db);
	if(rc != SQLITE_OK) {
		perror("da660.db open failed...!\n");
		rc = -1;
	}
	else {
		/*
		 * to select data record
		 * */
		memset(select_sql, 0, sizeof(select_sql));
		sprintf(select_sql, "select * from xz3100_opening_data where port = %u and addr = %u", port, addr);
		/*
		 * to get the two dimension table, nRow means rows and nColumn means cols
		 * */
		rc = sqlite3_get_table(db, select_sql, &dbResult, &nRow, &nColumn, &zErrMsg);
		if(rc != SQLITE_OK) {
			perror("database query failed!\n");
			rc = -1;
		}
		else {
			/*
			 * query statement
			 * */
			index = nColumn;   // the first value should start from nColum
			printf("port %d, addr %d opening data as following:\n", port, addr);
			for(row = 0;row < nRow;row++) {
				type = 0;
				for(col = 0;col < nColumn;col++) {
					if(col > 1) {
						value = dbResult[index];
						xz3100_opening_data_show1(type++, atoi(value));
					}
					index++;
				}
			}
		}
	}
	sqlite3_free_table(dbResult);  // never to forget to free the query table
	sqlite3_close(db);

	return rc;
}
/*
 * query arguments data records from xz3100 arg data table in terms with port and addr
 * return 0 if query success else return -1
 * */
int Query_xz3100_arg_data(uint port, uint addr) {
	int rc;
	int nRow, nColumn;
	int row, col;
	int index;
	int type;
	char *zErrMsg = NULL;
	char **dbResult;  // point to two dimension table
	char select_sql[SIZE];
	char *domain, *value; // the domain and value of query table
	/*
	 * to open da660
	 * */
	rc = sqlite3_open("da660.db", &db);
	if(rc != SQLITE_OK) {
		perror("da660.db open failed...!\n");
		rc = -1;
	}
	else {
		/*
		 * to select data record
		 * */
		memset(select_sql, 0, sizeof(select_sql));
		sprintf(select_sql, "select * from xz3100_arg_data where port = %u and addr = %u", port, addr);
		/*
		 * to get the two dimension table, nRow means rows and nColumn means cols
		 * */
		rc = sqlite3_get_table(db, select_sql, &dbResult, &nRow, &nColumn, &zErrMsg);
		if(rc != SQLITE_OK) {
			perror("database query failed!\n");
			rc = -1;
		}
		else {
			/*
			 * query statement
			 * */
			index = nColumn;   // the first value should start from nColum
			printf("port %d, addr %d arguments data as following:\n", port, addr);
			for(row = 0;row < nRow;row++) {
				type = 0;
				for(col = 0;col < nColumn;col++) {
					if(col > 1) {
						value = dbResult[index];
						xz3100_arg_data_show1(type++, atoi(value));
					}
					index++;
				}
			}
		}
	}
	sqlite3_free_table(dbResult);  // never to forget to free the query table
	sqlite3_close(db);
	
	return rc;
}
/*
 * query statistic data records from xz3100 statistic data table in terms with port and addr
 * return 0 if query success else return -1
 * */
int Query_xz3100_statistic_data(uint port, uint addr) {
	int rc;
	int nRow, nColumn;
	int row, col;
	int index;
	int type;
	char *zErrMsg = NULL;
	char **dbResult;  // point to two dimension table
	char select_sql[SIZE];
	char *domain, *value; // the domain and value of query table
	/*
	 * to open da660
	 * */
	rc = sqlite3_open("da660.db", &db);
	if(rc != SQLITE_OK) {
		perror("da660.db open failed...!\n");
		rc = -1;
	}
	else {
		/*
		 * to select data record
		 * */
		memset(select_sql, 0, sizeof(select_sql));
		sprintf(select_sql, "select * from xz3100_statistic_data where port = %u and addr = %u", port, addr);
		/*
		 * to get the two dimension table, nRow means rows and nColumn means cols
		 * */
		rc = sqlite3_get_table(db, select_sql, &dbResult, &nRow, &nColumn, &zErrMsg);
		if(rc != SQLITE_OK) {
			perror("database query failed!\n");
			rc = -1;
		}
		else {
			/*
			 * query statement
			 * */
			index = nColumn;   // the first value should start from nColum
			printf("port %d, addr %d statistic data as following:\n", port, addr);
			for(row = 0;row < nRow;row++) {
				type = 0;
				for(col = 0;col < nColumn;col++) {
					if(col > 1) {
						value = dbResult[index];
						xz3100_statistic_data_show1(type++, atoi(value));
					}
					index++;
				}
			}
		}
	}
	sqlite3_free_table(dbResult);  // never to forget to free the query table
	sqlite3_close(db);
	
	return rc;
}
void UI() {
	uchar choice;
	uint port, addr, type, bps;
	char typestr[SIZE];
	char bpsstr[SIZE];
	int i, j;
	int n;
	/*
	 * UI tips
	 * */
	printf("\t%10s", "Welcome to DA660 database configure System\n");
	printf("\t%10s", "0 -- insert data record to baseinfo table\n");
	printf("\t%10s", "1 -- query data record from baseinfo table\n");
	printf("\t%10s", "2 -- delete data record from baseinfo table\n");
	printf("\t%10s", "3 -- query data record from xz3100\n");
	printf("\t%10s", "4 -- quit the system\n");
	while(1) {
		printf("please make your choice:");
		if(scanf("%d", &choice) != 1) {
			perror("input data error, system crash!\n");
			exit(EXIT_FAILURE);
		}
		if(choice != 0 && choice != 1 && choice != 2 && choice != 3 && choice != 4) {
			perror("Sorry, You have made wrong choice...!\n");
			continue;
		}
		switch(choice) {
			case 0:
				printf("input the number of records that should be inserted:");
				if(scanf("%d", &n) != 1) {
					perror("input data error, system crash!\n");
					exit(EXIT_FAILURE);
				}
				if(n <= SIZE && n >= 1) {
					for(i = 1;i <= n;i++) {
						printf("the %d record---\n", i);
						/*
						 * input port 
						 * */
						printf("port = ");
						if(scanf("%d", &port) != 1) {
							perror("input data error, system crash!\n");
							exit(EXIT_FAILURE);
						}
						if(port < 0 || port >= 16) {
							perror("port should be 0 to 15...!");
							i--;
							continue;
						}
						/*
						 * input addr
						 * */
						printf("addr = ");
						if(scanf("%d", &addr) != 1) {
							perror("input data error, system crash!\n");
							exit(EXIT_FAILURE);
						}
						if(addr < 0 || addr >= 257) {
							perror("port should be 0 to 256...!");
							i--;
							continue;
						}
						/*
						 * input type
						 * */
						printf("type = ");
						if(scanf("%s", typestr) != 1) {
							perror("input data error, system crash!\n");
							exit(EXIT_FAILURE);
						}
						for(j = 0;j < 5;j++) {
							if(strcmp(equipmentType[j], typestr) == 0) {
								type = j;
							   	break;
							}
						}
						if(j >= 5) {
							perror("equipment type should be xz2000, xz3000, xz3100, xz3500, xz6000...!\n");
							i--;
							continue;
						}
						/*
						 * input bps
						 * */
						printf("bps = ");
						if(scanf("%s", &bpsstr) != 1) {
							perror("input data error, system crash!\n");
							exit(EXIT_FAILURE);
						}
						for(j = 0;j < 4;j++) {
							if(strcmp(bpsArray[j], bpsstr) == 0) {
								bps = j;
							   	break;
							}
						}
						if(j >=4 ) {
							perror("equiment bps should be 2400, 4800, 9600, 126000...!\n");
							i--;
							continue;
						}
						if(Insert_baseinfo(port, addr, type, bps)) {
							perror("Insert data record failed!\n");
						}
					}
				}
				break;
			case 1:
				printf("please input port:");
				if(scanf("%d", &port) != 1) {
					perror("input data error, system crash!\n");
					exit(EXIT_FAILURE);
				}
				if(Query_baseinfo(port)) {
					perror("Query data record failed!\n");
				}
				break;
			case 2:
				printf("please input port:");
				if(scanf("%d", &port) != 1) {
					perror("input data error, system crash!\n");
					exit(EXIT_FAILURE);
				}
				if(port < 0 || port >= 16) {
					perror("port should be 0 to 15...!");
					break;
				}
				printf("please input addr:");
				if(scanf("%d", &addr) != 1) {
					perror("input data error, system crash!\n");
					exit(EXIT_FAILURE);
				}
				if(addr < 0 || addr >= 257) {
					perror("port should be 0 to 256...!");
					break;
				}
				if(Delete_baseinfo(port, addr)) {
					perror("Delete data record failed!\n");
				}
				break;
			case 3:
				printf("please input port:");
				if(scanf("%d", &port) != 1) {
					perror("input data error, system crash!\n");
					exit(EXIT_FAILURE);
				}
				if(port < 0 || port >= 16) {
					perror("port should be 0 to 15...!");
					break;
				}
				printf("please input addr:");
				if(scanf("%d", &addr) != 1) {
					perror("input data error, system crash!\n");
					exit(EXIT_FAILURE);
				}
				if(addr < 0 || addr >= 257) {
					perror("port should be 0 to 256...!");
					break;
				}
				Query_xz3100_realtime_data(port, addr);
				Query_xz3100_soe_data(port, addr);
				Query_xz3100_action_data(port, addr);
				Query_xz3100_opening_data(port, addr);
				Query_xz3100_arg_data(port, addr);
				Query_xz3100_statistic_data(port, addr);
				break;
			case 4:
				printf("Thank you for your using, GoodBye...!\n");
				return;
		}
	}
}
int main() {
	UI();

	return 0;
}

五、本地数据库测试

1. 测试数据库正常产生
将交叉编译好的dbCreate文件推入MOXA DA660,在DA660中执行命令./dbCreate,显示如下:

gxlsystem.com,布布扣


2.  测试数据插入

gxlsystem.com,布布扣

由测试结果可知,数据能够正常插入,因为baseinfo表中port和addr被定义为主码,因此测试结果符合设定要求。

3. 测试数据查询

gxlsystem.com,布布扣

4. 测试数据删除

gxlsystem.com,布布扣

gxlsystem.com,布布扣


六、远程修改数据库的方法

客户端f发送数据库操作报文,包括查询,删除和插入,DA660解析报文并发回回送报文。

gxlsystem.com,布布扣

gxlsystem.com,布布扣

七、DA660里数据库线程

数据库线程在端口4001(专门为数据库操作而设置)不停侦测有无客户端连接请求,如果有响应该请求。

#include <sys/time.h>
#include "database.h"
#include "socket.h"
#include "serial.h"
#include "xz3100.h"
#include <signal.h>
#define db_port 4001
#define realtime_data_port 4002
#define equipment_control_port 4003
char *equipmentType[] = {"xz2000", "xz3000", "xz3100", "xz3500", "xz6000"};
char *bpsArray[] = {"2400", "4800", "9600", "192000"};
baseinfoNode *baseinfoNode_array[16];
port_lock_type lock_array[16];
xz3100_realtime_data_Node *xz3100_realtime_data_Node_array[16];
xz3100_soe_data_Node *xz3100_soe_data_Node_array[16];
xz3100_action_data_Node *xz3100_action_data_Node_array[16];
xz3100_opening_data_Node *xz3100_opening_data_Node_array[16];
xz3100_arg_data_Node *xz3100_arg_data_Node_array[16];
xz3100_statistic_data_Node *xz3100_statistic_data_Node_array[16];

uint first_addr, last_addr;
struct timeval start, end;
unsigned long timer;
uint portInit[16];   // whether the port is initialized, 0 or 1
/*
 * db_thread
 * accept request about database from client 
 * */
void *db_thread(void *ptr) {
	uchar sbuf[BUFFER_LEN];
	uchar rbuf[BUFFER_LEN];
	uchar clientaddr[BUFFER_LEN];
	int len;
	int s_len, r_len;
	int serverfd, clientfd;
	int i;
	/*
	 * Init TCP Server
	 * */
	TCPServerInit(db_port, &serverfd);
	if(serverfd < 0) {
		perror("failed to init server!\n");
		exit(EXIT_FAILURE);
	}
	/*
	 * to listen and connect the client 
	 * */
	while(1) {
		TCPServerWaitConnection(serverfd, &clientfd, clientaddr); // if there is no connection request, blocked here
		if(clientfd < 0) {
			perror("failed to connect client!\n");
			exit(EXIT_FAILURE);
		}
		while(1) {
			len = TCPBlockRead(clientfd, rbuf, BUFFER_LEN); // blocked until there is data coming into the port
			if(len <= 0) {  // if can not read data, sleep and then continue
				sleep(1);
				break;
			}
			else {
				Database_resolve(sbuf, rbuf, &s_len, &r_len);
				if(TCPWrite(clientfd, sbuf, s_len) == s_len) {
					printf("db control data have been sent...!\n");
			//		for(i = 0;i < s_len;i++) {       Testing: db control paragram is OK!
			//			printf("%d\t", sbuf[i]);
			//		}
//					printf("\n");
				}
				else perror("db control data sent error...!\n");
			}
		}
	}
	TCPConnectionClose(serverfd);

	return 0;
}
/*
 * find the equipment type from baseinfoNode array
 * @port: port
 * @addr: equipment addr
 * @type: equipment type
 * return 0 if find successfully else return -1
 * */
int find_equipment_type(uint port, uint addr, uint *type) {
	baseinfoNode *p;
	int rc = 0;
	if(port < 0 || port > 15) {
		printf("query tips: port %d is wrong...!\n", port);
		rc = -1;
	}
	if(!baseinfoNode_array[port]) {
		printf("query tips: port %d is wrong...!", port);
		rc = -1;
	}
	p = baseinfoNode_array[port]->link;
	while(p) {                    // there is a equipment
		if(p->info[0] == port && p->info[1] == addr) {
			*type = p->info[2];
			break;
		}
		p = p->link;
	}
	if(!p) {
		printf("query tips: addr %d is wrong...!", addr);
		rc = -1;
	}

	return rc;
}
/*
 * read all data from 
 * xz3100_realtime_data_Node linklist
 * xz3100_soe_data_Node linklist
 * xz3100_action_data_Node linklist
 * xz3100_opening_data_Node linklist
 * xz3100_arg_data_Node linklist
 * xz3100_statistic_data_Node linklist
 * @port: port
 * @addr: equipment addr
 * @type: equipment type
 * @buf: buf to receive data from equipment link list 
 * @len: the length of buf
 * */
void xz3100_readbuf(uint port, uint addr, uint type, uchar *buf, uint *len) {
	uint index = 0;
	uint i;
	uchar low_8, high_8;
	xz3100_realtime_data_Node *p_realtime_data;
	xz3100_soe_data_Node *p_soe_data;
	xz3100_action_data_Node *p_action_data;
	xz3100_opening_data_Node *p_opening_data;
	xz3100_arg_data_Node *p_arg_data;
	xz3100_statistic_data_Node *p_statistic_data;
	/*
	 * read data from xz3100_realtime_data link list
	 * */
	for(p_realtime_data = xz3100_realtime_data_Node_array[port];p_realtime_data != NULL;p_realtime_data = p_realtime_data ->link ) {
		if(p_realtime_data->addr == addr) {
			for(i = 0;i < 58;i++) {
				dto2uchar(p_realtime_data->realtime_data[i], &high_8, &low_8);
				buf[index++] = low_8;
				buf[index++] = high_8;
			}
			break;
		}
	}
	/*
	 * read data from xz3100_soe_data link list
	 * */
	for(p_soe_data = xz3100_soe_data_Node_array[port];p_soe_data != NULL;p_soe_data = p_soe_data ->link ) {
		if(p_soe_data->addr == addr) {
			for(i = 0;i < 12;i++) {
				dto2uchar(p_soe_data->soe_data[i], &high_8, &low_8);
				buf[index++] = low_8;
				buf[index++] = high_8;
			}
			break;
		}
	}
	/*
	 * read data from xz3100_action_data link list
	 * */
	for(p_action_data = xz3100_action_data_Node_array[port];p_action_data != NULL;p_action_data = p_action_data ->link ) {
		if(p_action_data->addr == addr) {
			for(i = 0;i < 12;i++) {
				dto2uchar(p_action_data->action_data[i], &high_8, &low_8);
				buf[index++] = low_8;
				buf[index++] = high_8;
			}
			break;
		}
	}
	/*
	 * read data from xz3100_opening_data link list
	 * */
	for(p_opening_data = xz3100_opening_data_Node_array[port];p_opening_data != NULL;p_opening_data = p_opening_data ->link ) {
		if(p_opening_data->addr == addr) {
			for(i = 0;i < 20;i++) {
				dto2uchar(p_opening_data->opening_data[i], &high_8, &low_8);
				buf[index++] = low_8;
				buf[index++] = high_8;
			}
			break;
		}
	}
	/*
	 * read data from xz3100_arg_data link list
	 * */
	for(p_arg_data = xz3100_arg_data_Node_array[port];p_arg_data != NULL;p_arg_data = p_arg_data ->link ) {
		if(p_arg_data->addr == addr) {
			for(i = 0;i < 57;i++)  {
				dto2uchar(p_arg_data->arg_data[i], &high_8, &low_8);
				buf[index++] = low_8;
				buf[index++] = high_8;
			}
			break;
		}
	}
	/*
	 * read data from xz3100_statistic_data link list
	 * */
	for(p_statistic_data = xz3100_statistic_data_Node_array[port];p_statistic_data != NULL;p_statistic_data = p_statistic_data ->link ) {
		if(p_statistic_data->addr == addr) {
			for(i = 0;i < 26;i++) {
				dto2uchar(p_statistic_data->statistic_data[i], &high_8, &low_8);
				buf[index++] = low_8;
				buf[index++] = high_8;
			}
			break;
		}
	}
	/*
	 * enclose pargaram
	 * */
	buf[index++] = port;
	buf[index++] = addr;
	buf[index++] = type;
	*len = index;
}
/*
 * realtime data thread
 * accept paragram as (port addr) from client
 * generate query link list 
 * sending pargaram to target equipment 
 * collect back paragram
 * sending back to the client
 * */
void *realtime_data_thread(void *ptr) {
	uchar sbuf[BUFFER_LEN], rbuf[BUFFER_LEN];
	uchar clientaddr[BUFFER_LEN];
	uint len;
	int serverfd, clientfd;
	int i;
	uint port, addr, type;
	/*
	 * Init TCP Server
	 * */
	TCPServerInit(realtime_data_port, &serverfd);
	if(serverfd < 0) {
		perror("failed to init server!\n");
		exit(EXIT_FAILURE);
	}
	/*
	 * to listen and connect the client 
	 * */
	while(1) {
		TCPServerWaitConnection(serverfd, &clientfd, clientaddr); // if there is no connection request, blocked here
		if(clientfd < 0) {
			perror("failed to connect client!\n");
			exit(EXIT_FAILURE);
		}
		while(1) {
			len = TCPBlockRead(clientfd, rbuf, BUFFER_LEN); // blocked until there is data coming into the port
			if(len <= 0) {  // if can not read data, sleep and then continue
				sleep(1);
				break;
			}
			else {
				port = rbuf[0];
				addr = rbuf[1];
				if(port < 0 || port > 15) {
					printf("query tips: port %d is wrong, running stop...!\n", port);
					exit(EXIT_FAILURE);
				}
				if(find_equipment_type(port, addr, &type)) goto Exit;
				switch(type) {
					/*
					 * xz2000
					 * */
					case 0:
						break;
					/*
					 * xz3000
					 * */
					case 1:
						break;
					/*
					 * xz3100
					 * */
					case 2:
						xz3100_readbuf(port, addr, type, sbuf, &len);
						/*
						 * sending through tcp sockect
						 * */
						if(TCPWrite(clientfd, sbuf, len) == len) {
				//			printf("%d bytes realtime data from port %d, addr %d have been sent...!\n", len, port, addr); Testing: It is OK!
						}
						else {  // tcp connection disconnect
							printf("realtime data from port %d, addr %d sent error...!\n", port, addr);
						}
						break;
					/*
					 * xz3500
					 * */
					case 3:
						break;
					/*
					 * xz6000
					 * */
					case 4:
						break;
				}
			}
		}
	}
Exit:
	TCPConnectionClose(serverfd);

	return 0;
}
/*
 * equipment control paragram__thread
 * accept request from client 
 * */
void *equipment_control_thread(void *ptr) {
	uchar sbuf[BUFFER_LEN];
	uchar rbuf[BUFFER_LEN];
	uchar clientaddr[BUFFER_LEN];
	int r_len, s_len;
	uchar len;
	int serverfd, clientfd;
	int i;
	uint port, addr, type;
	/*
	 * Init TCP Server
	 * */
	TCPServerInit(equipment_control_port, &serverfd);
	if(serverfd < 0) {
		perror("failed to init server!\n");
		exit(EXIT_FAILURE);
	}
	/*
	 * to listen and connect the client 
	 * */
	while(1) {
		TCPServerWaitConnection(serverfd, &clientfd, clientaddr); // if there is no connection request, blocked here
		if(clientfd < 0) {
			perror("failed to connect client!\n");
			exit(EXIT_FAILURE);
		}
		while(1) {
			r_len = TCPBlockRead(clientfd, rbuf, BUFFER_LEN); // blocked until there is data coming into the port
			if(r_len <= 0) {  // if can not read data, sleep and then continue
				sleep(1);
				break;
			}
			else {
				port = rbuf[0];
				addr = rbuf[1];
				if(find_equipment_type(port, addr, &type)) {
					goto exit;
				}
				switch(type) {
					/*
					 * xz2000
					 * */
					case 0:
						break;
					/*
					 * xz3000
					 * */
					case 1:
						break;
					/*
					 * xz3100
					 * */
					case 2:
						if(xz3100_resolve_control_paragram(rbuf, sbuf, &r_len, &s_len)) {
							goto exit;
						}
					//	for(i = 0;i < s_len;i++) printf("%d\t", sbuf[i]); // Testing: Equipment Control Paragram is OK!
					//	printf("\n");
						/*
						 * sending control paragram
						 * */
						Serial_lock(&lock_array[port]);
						WriteSerial(port, sbuf, s_len);   // sending request paragram
						usleep(1e5);
						ReadSerial(port, rbuf, &len);   // collecting the back paragram
					//	for(i = 0;i < len;i++) printf("%d\t", rbuf[i]); // Testing: Equipment Control Paragram is OK!
					//	printf("\n");
						Serial_unlock(&lock_array[port]);
						break;
					/*
					 * xz3500
					 * */
					case 3:
						break;
					/*
					 * xz6000
					 * */
					case 4:
						break;
				}
			}
		}
	}
exit:
	TCPConnectionClose(serverfd);

	return 0;
}

/*
 * to locate the target node whose addr is equal to the addr in the port of xz3100_realtime_data_Node_array
 * return the pointer of target node
 * */
xz3100_realtime_data_Node *locate_xz3100_realtime_data_pointer(uint addr, uint port) {
	xz3100_realtime_data_Node *p = xz3100_realtime_data_Node_array[port];
	while(p) {
		if(p->addr == addr) {   // find the target node
			return p;
		}
		p = p->link;
	}
	if(!p) {        // not found, insert the node in the front of link list
		p = (xz3100_realtime_data_Node *)malloc(sizeof(xz3100_realtime_data_Node));
		if(!p) {
			perror("memory error, Running stop...!");
			exit(EXIT_FAILURE);
		}
		p->addr = addr;
		p->link = xz3100_realtime_data_Node_array[port];
		xz3100_realtime_data_Node_array[port] = p;
	}
	return p;
}
/*
 * to locate the target node whose addr is equal to the addr in the port of xz3100_soe_data_Node_array
 * return the pointer of target node
 * */
xz3100_soe_data_Node *locate_xz3100_soe_data_pointer(uint addr, uint port) {
	xz3100_soe_data_Node *p = xz3100_soe_data_Node_array[port];
	while(p) {
		if(p->addr == addr) {   // find the target node
			return p;
		}
		p = p->link;
	}
	if(!p) {        // not found, insert the node in the front of link list
		p = (xz3100_soe_data_Node *)malloc(sizeof(xz3100_soe_data_Node));
		if(!p) {
			perror("memory error, Running stop...!");
			exit(EXIT_FAILURE);
		}
		p->addr = addr;
		p->link = xz3100_soe_data_Node_array[port];
		xz3100_soe_data_Node_array[port] = p;
	}
	return p;
}
/*
 * to locate the target node whose addr is equal to the addr in the port of xz3100_action_data_Node_array
 * return the pointer of target node
 * */
xz3100_action_data_Node *locate_xz3100_action_data_pointer(uint addr, uint port) {
	xz3100_action_data_Node *p = xz3100_action_data_Node_array[port];
	while(p) {
		if(p->addr == addr) {   // find the target node
			return p;
		}
		p = p->link;
	}
	if(!p) {        // not found, insert the node in the front of link list
		p = (xz3100_action_data_Node *)malloc(sizeof(xz3100_action_data_Node));
		if(!p) {
			perror("memory error, Running stop...!");
			exit(EXIT_FAILURE);
		}
		p->addr = addr;
		p->link = xz3100_action_data_Node_array[port];
		xz3100_action_data_Node_array[port] = p;
	}
	return p;
}
/*
 * to locate the target node whose addr is equal to the addr in the port of xz3100_opening_data_Node_array
 * return the pointer of target node
 * */
xz3100_opening_data_Node *locate_xz3100_opening_data_pointer(uint addr, uint port) {
	xz3100_opening_data_Node *p = xz3100_opening_data_Node_array[port];
	while(p) {
		if(p->addr == addr) {   // find the target node
			return p;
		}
		p = p->link;
	}
	if(!p) {        // not found, insert the node in the front of link list
		p = (xz3100_opening_data_Node *)malloc(sizeof(xz3100_opening_data_Node));
		if(!p) {
			perror("memory error, Running stop...!");
			exit(EXIT_FAILURE);
		}
		p->addr = addr;
		p->link = xz3100_opening_data_Node_array[port];
		xz3100_opening_data_Node_array[port] = p;
	}
	return p;
}
/*
 * to locate the target node whose addr is equal to the addr in the port of xz3100_arg_data_Node_array
 * return the pointer of target node
 * */
xz3100_arg_data_Node *locate_xz3100_arg_data_pointer(uint addr, uint port) {
	xz3100_arg_data_Node *p = xz3100_arg_data_Node_array[port];
	while(p) {
		if(p->addr == addr) {   // find the target node
			return p;
		}
		p = p->link;
	}
	if(!p) {        // not found, insert the node in the front of link list
		p = (xz3100_arg_data_Node *)malloc(sizeof(xz3100_arg_data_Node));
		if(!p) {
			perror("memory error, Running stop...!");
			exit(EXIT_FAILURE);
		}
		p->addr = addr;
		p->link = xz3100_arg_data_Node_array[port];
		xz3100_arg_data_Node_array[port] = p;
	}
	return p;
}
/*
 * to locate the target node whose addr is equal to the addr in the port of xz3100_statistic_data_Node_array
 * return the pointer of target node
 * */
xz3100_statistic_data_Node *locate_xz3100_statistic_data_pointer(uint addr, uint port) {
	xz3100_statistic_data_Node *p = xz3100_statistic_data_Node_array[port];
	while(p) {
		if(p->addr == addr) {   // find the target node
			return p;
		}
		p = p->link;
	}
	if(!p) {        // not found, insert the node in the front of link list
		p = (xz3100_statistic_data_Node *)malloc(sizeof(xz3100_statistic_data_Node));
		if(!p) {
			perror("memory error, Running stop...!");
			exit(EXIT_FAILURE);
		}
		p->addr = addr;
		p->link = xz3100_statistic_data_Node_array[port];
		xz3100_statistic_data_Node_array[port] = p;
	}
	return p;
}
/*
 * construct xz3100_realtime_data link list
 * construct xz3100_soe_data link list
 * construct xz3100_action_data link list
 * construct xz3100_opening_data link list
 * construct xz3100_arg_data link list
 * construct xz3100_statistic_data link list
 * */
int update_xz3100_linklist() {
	uchar sbuf[BUFFER_LEN], rbuf[BUFFER_LEN];
	uint i, j, k;
	uchar len;
	uchar port;  // the port of equipment
	uint addr;  // the addr of equipment
	uint type;  // the type of equipment
	uint bps;   // the speed of equipment
	uint speed; 
	baseinfoNode *p;

	xz3100_realtime_data_Node *p_realtime_data;
	xz3100_soe_data_Node *p_soe_data;
	xz3100_action_data_Node *p_action_data;
	xz3100_opening_data_Node *p_opening_data;
	xz3100_arg_data_Node *p_arg_data;
	xz3100_statistic_data_Node *p_statistic_data;
	/*
	 * core process
	 * */
	for(i = 0;i < 16;i++) {   // transfer every port
		if(!baseinfoNode_array[i]) continue;  // there is null pointer, pay attention
		p = baseinfoNode_array[i]->link;
		while(p) {                    // there is a equipment
			port = p->info[0];
			addr = p->info[1];
			type = p->info[2];
			bps  = p->info[3];
			speed = atoi(bpsArray[bps]);
			if(!portInit[port]) {   // to initialize the port
				SerialInit(port, speed);
				portInit[port] = 1;
			}
			SerialSetSpeed(port, speed);            // to set the speed for the port
			switch(type) {
				/*
				 * xz2000
				 * */
				case 0:
					break;
				/*
				 * xz3000
				 * */
				case 1:
					break;
				/*
				 * xz3100
				 * */
				case 2:
					/*
					 * find every port's equipment and then send and collect paragram
					 * */
					p_realtime_data = locate_xz3100_realtime_data_pointer(addr, i);
					p_soe_data = locate_xz3100_soe_data_pointer(addr, i);
					p_action_data = locate_xz3100_action_data_pointer(addr, i);
					p_opening_data = locate_xz3100_opening_data_pointer(addr, i);
					p_arg_data = locate_xz3100_arg_data_pointer(addr, i);
					p_statistic_data = locate_xz3100_statistic_data_pointer(addr, i);
					for(j = 0;j < xz3100_funcArray0Num;j++) {
						xz3100_funcArray0[j](addr, sbuf, &len);
						usleep(1e5);                   // let os schedule other threads
						Serial_lock(&lock_array[port]);
						WriteSerial(port, sbuf, len);   // sending request paragram
						usleep(1e5);
						ReadSerial(port, rbuf, &len);   // collecting the back paragram
						Serial_unlock(&lock_array[port]);
						/*
						 * xz3100 realtime data process
						 * */
						if(j < 58) {
							p_realtime_data->realtime_data[j] = x2tod(rbuf[3], rbuf[4]);  // update the corresponing value
						}
						/*
						 * xz3100 soe data process
						 * */
						if(j == 58) {
							for(k = 0;k < 12;k++) p_soe_data->soe_data[k] = rbuf[2 + k];
						}
						/*
						 * xz3100 action data process
						 * */
						if(j == 59) {
							for(k = 0;k < 12;k++) p_action_data->action_data[k] = rbuf[2 + k];
						}
						/*
						 * xz3100 opening data process
						 * */
						if(j == 60) {
							for(k = 0;k < 20;k++) p_opening_data->opening_data[k] = rbuf[2 + k];
						}
						/*
						 * xz3100 arg data process
						 * */
						if(j > 60 && j < 118) {
							p_arg_data->arg_data[j - 61] = x2tod(rbuf[3], rbuf[4]);  // update the corresponing value
						}
						/*
						 * xz3100 statistic data process
						 * */
						if(j >= 118) {
							p_statistic_data->statistic_data[j - 118] = x2tod(rbuf[3], rbuf[4]);  // update the corresponing value
						}
					}
next:
					break;
				/*
				 * xz3500
				 * */
				case 3:
					break;
				/*
				 * xz6000
				 * */
				case 4:
					break;
			}
			p = p->link;
		}

	}
	return 0;
}
/* port0 thread
 * construct xz3100_realtime_data link list
 * construct xz3100_soe_data link list
 * construct xz3100_action_data link list
 * construct xz3100_opening_data link list
 * construct xz3100_arg_data link list
 * construct xz3100_statistic_data link list
 * */
void *port0_update_xz3100_linklist(void *ptr) {
	uchar sbuf[BUFFER_LEN], rbuf[BUFFER_LEN];
	uint j, k;
	uchar len;
	uchar port;  // the port of equipment
	uint addr;  // the addr of equipment
	uint type;  // the type of equipment
	uint bps;   // the speed of equipment
	uint speed; 
	baseinfoNode *p;

	xz3100_realtime_data_Node *p_realtime_data;
	xz3100_soe_data_Node *p_soe_data;
	xz3100_action_data_Node *p_action_data;
	xz3100_opening_data_Node *p_opening_data;
	xz3100_arg_data_Node *p_arg_data;
	xz3100_statistic_data_Node *p_statistic_data;
	while(1) {
		/*
		 * core process
		 * */
		if(!baseinfoNode_array[0]) break;  // there is null pointer, pay attention
		p = baseinfoNode_array[0]->link;
		while(p) {                    // there is a equipment
			port = p->info[0];
			addr = p->info[1];
			type = p->info[2];
			bps  = p->info[3];
			speed = atoi(bpsArray[bps]);
			if(!portInit[port]) {   // to initialize the port
				SerialInit(port, speed);
				portInit[port] = 1;
			}
			SerialSetSpeed(port, speed);            // to set the speed for the port
			switch(type) {
				/*
				 * xz2000
				 * */
				case 0:
					break;
				/*
				 * xz3000
				 * */
				case 1:
					break;
				/*
				 * xz3100
				 * */
				case 2:
					/*
					 * find corresponding port's equipment and then send and collect paragram
					 * */
					p_realtime_data = locate_xz3100_realtime_data_pointer(addr, port);
					p_soe_data = locate_xz3100_soe_data_pointer(addr, port);
					p_action_data = locate_xz3100_action_data_pointer(addr, port);
					p_opening_data = locate_xz3100_opening_data_pointer(addr, port);
					p_arg_data = locate_xz3100_arg_data_pointer(addr, port);
					p_statistic_data = locate_xz3100_statistic_data_pointer(addr, port);
					for(j = 0;j < xz3100_funcArray0Num;j++) {
						xz3100_funcArray0[j](addr, sbuf, &len);
					//	usleep(1e5);                   // let os schedule other threads
					//	Serial_lock(&lock_array[port]);
						WriteSerial(port, sbuf, len);   // sending request paragram
						usleep(1e5);
						ReadSerial(port, rbuf, &len);   // collecting the back paragram
					//  Serial_unlock(&lock_array[port]);
						/*
						 * xz3100 realtime data process
						 * */
						if(j < 58) {
							p_realtime_data->realtime_data[j] = x2tod(rbuf[3], rbuf[4]);  // update the corresponing value
						}
						/*
						 * xz3100 soe data process
						 * */
						if(j == 58) {
							for(k = 0;k < 12;k++) p_soe_data->soe_data[k] = rbuf[2 + k];
						}
						/*
						 * xz3100 action data process
						 * */
						if(j == 59) {
							for(k = 0;k < 12;k++) p_action_data->action_data[k] = rbuf[2 + k];
						}
						/*
						 * xz3100 opening data process
						 * */
						if(j == 60) {
							for(k = 0;k < 20;k++) p_opening_data->opening_data[k] = rbuf[2 + k];
						}
						/*
						 * xz3100 arg data process
						 * */
						if(j > 60 && j < 118) {
							p_arg_data->arg_data[j - 61] = x2tod(rbuf[3], rbuf[4]);  // update the corresponing value
						}
						/*
						 * xz3100 statistic data process
						 * */
						if(j >= 118) {
							p_statistic_data->statistic_data[j - 118] = x2tod(rbuf[3], rbuf[4]);  // update the corresponing value
						}
					}
					break;
					/*
					 * xz3500
					 * */
					case 3:
						break;
					/*
					 * xz6000
					 * */
					case 4:
						break;
				}
//			if(addr == first_addr) {
//				gettimeofday(&start, NULL);
//			}
//			if(addr == last_addr) {
//				gettimeofday(&end, NULL);
//				timer = 1e6 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;
//				printf("elapsing time: from the first equipment to the last equipment  = %ldus\n", timer);
//			}
			p = p->link;
		}
	}
}
/* port1 thread
 * construct xz3100_realtime_data link list
 * construct xz3100_soe_data link list
 * construct xz3100_action_data link list
 * construct xz3100_opening_data link list
 * construct xz3100_arg_data link list
 * construct xz3100_statistic_data link list
 * */
void *port1_update_xz3100_linklist(void *ptr) {
	uchar sbuf[BUFFER_LEN], rbuf[BUFFER_LEN];
	uint j, k;
	uchar len;
	uchar port;  // the port of equipment
	uint addr;  // the addr of equipment
	uint type;  // the type of equipment
	uint bps;   // the speed of equipment
	uint speed; 
	baseinfoNode *p;

	xz3100_realtime_data_Node *p_realtime_data;
	xz3100_soe_data_Node *p_soe_data;
	xz3100_action_data_Node *p_action_data;
	xz3100_opening_data_Node *p_opening_data;
	xz3100_arg_data_Node *p_arg_data;
	xz3100_statistic_data_Node *p_statistic_data;
	while(1) {
		/*
		 * core process
		 * */
		if(!baseinfoNode_array[1]) break;  // there is null pointer, pay attention
		p = baseinfoNode_array[1]->link;
		while(p) {                    // there is a equipment
			port = p->info[0];
			addr = p->info[1];
			type = p->info[2];
			bps  = p->info[3];
			speed = atoi(bpsArray[bps]);
			if(!portInit[port]) {   // to initialize the port
				SerialInit(port, speed);
				portInit[port] = 1;
			}
			SerialSetSpeed(port, speed);            // to set the speed for the port
			switch(type) {
				/*
				 * xz2000
				 * */
				case 0:
					break;
				/*
				 * xz3000
				 * */
				case 1:
					break;
				/*
				 * xz3100
				 * */
				case 2:
					/*
					 * find corresponding port's equipment and then send and collect paragram
					 * */
					p_realtime_data = locate_xz3100_realtime_data_pointer(addr, port);
					p_soe_data = locate_xz3100_soe_data_pointer(addr, port);
					p_action_data = locate_xz3100_action_data_pointer(addr, port);
					p_opening_data = locate_xz3100_opening_data_pointer(addr, port);
					p_arg_data = locate_xz3100_arg_data_pointer(addr, port);
					p_statistic_data = locate_xz3100_statistic_data_pointer(addr, port);
					for(j = 0;j < xz3100_funcArray0Num;j++) {
						xz3100_funcArray0[j](addr, sbuf, &len);
					//	usleep(1e5);                   // let os schedule other threads
					//	Serial_lock(&lock_array[port]);
						WriteSerial(port, sbuf, len);   // sending request paragram
						usleep(1e5);
						ReadSerial(port, rbuf, &len);   // collecting the back paragram
					//  Serial_unlock(&lock_array[port]);
						/*
						 * xz3100 realtime data process
						 * */
						if(j < 58) {
							p_realtime_data->realtime_data[j] = x2tod(rbuf[3], rbuf[4]);  // update the corresponing value
						}
						/*
						 * xz3100 soe data process
						 * */
						if(j == 58) {
							for(k = 0;k < 12;k++) p_soe_data->soe_data[k] = rbuf[2 + k];
						}
						/*
						 * xz3100 action data process
						 * */
						if(j == 59) {
							for(k = 0;k < 12;k++) p_action_data->action_data[k] = rbuf[2 + k];
						}
						/*
						 * xz3100 opening data process
						 * */
						if(j == 60) {
							for(k = 0;k < 20;k++) p_opening_data->opening_data[k] = rbuf[2 + k];
						}
						/*
						 * xz3100 arg data process
						 * */
						if(j > 60 && j < 118) {
							p_arg_data->arg_data[j - 61] = x2tod(rbuf[3], rbuf[4]);  // update the corresponing value
						}
						/*
						 * xz3100 statistic data process
						 * */
						if(j >= 118) {
							p_statistic_data->statistic_data[j - 118] = x2tod(rbuf[3], rbuf[4]);  // update the corresponing value
						}
					}
					break;
					/*
					 * xz3500
					 * */
					case 3:
						break;
					/*
					 * xz6000
					 * */
					case 4:
						break;
				}
	//		if(addr == first_addr) {
	//			gettimeofday(&start, NULL);
	//		}
	//		if(addr == last_addr) {
	//			gettimeofday(&end, NULL);
	//			timer = 1e6 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;
	//			printf("elapsing time: from the first equipment to the last equipment  = %ldus\n", timer);
	//		}
			p = p->link;
		}
	}
}
/* port2 thread
 * construct xz3100_realtime_data link list
 * construct xz3100_soe_data link list
 * construct xz3100_action_data link list
 * construct xz3100_opening_data link list
 * construct xz3100_arg_data link list
 * construct xz3100_statistic_data link list
 * */
void *port2_update_xz3100_linklist(void *ptr) {
	uchar sbuf[BUFFER_LEN], rbuf[BUFFER_LEN];
	uint j, k;
	uchar len;
	uchar port;  // the port of equipment
	uint addr;  // the addr of equipment
	uint type;  // the type of equipment
	uint bps;   // the speed of equipment
	uint speed; 
	baseinfoNode *p;

	xz3100_realtime_data_Node *p_realtime_data;
	xz3100_soe_data_Node *p_soe_data;
	xz3100_action_data_Node *p_action_data;
	xz3100_opening_data_Node *p_opening_data;
	xz3100_arg_data_Node *p_arg_data;
	xz3100_statistic_data_Node *p_statistic_data;
	while(1) {
		/*
		 * core process
		 * */
		if(!baseinfoNode_array[2]) break;  // there is null pointer, pay attention
		p = baseinfoNode_array[2]->link;
		while(p) {                    // there is a equipment
			port = p->info[0];
			addr = p->info[1];
			type = p->info[2];
			bps  = p->info[3];
			speed = atoi(bpsArray[bps]);
			if(!portInit[port]) {   // to initialize the port
				SerialInit(port, speed);
				portInit[port] = 1;
			}
			SerialSetSpeed(port, speed);            // to set the speed for the port
			switch(type) {
				/*
				 * xz2000
				 * */
				case 0:
					break;
				/*
				 * xz3000
				 * */
				case 1:
					break;
				/*
				 * xz3100
				 * */
				case 2:
					/*
					 * find corresponding port's equipment and then send and collect paragram
					 * */
					p_realtime_data = locate_xz3100_realtime_data_pointer(addr, port);
					p_soe_data = locate_xz3100_soe_data_pointer(addr, port);
					p_action_data = locate_xz3100_action_data_pointer(addr, port);
					p_opening_data = locate_xz3100_opening_data_pointer(addr, port);
					p_arg_data = locate_xz3100_arg_data_pointer(addr, port);
					p_statistic_data = locate_xz3100_statistic_data_pointer(addr, port);
					for(j = 0;j < xz3100_funcArray0Num;j++) {
						xz3100_funcArray0[j](addr, sbuf, &len);
					//	usleep(1e5);                   // let os schedule other threads
					//	Serial_lock(&lock_array[port]);
						WriteSerial(port, sbuf, len);   // sending request paragram
						usleep(1e5);
						ReadSerial(port, rbuf, &len);   // collecting the back paragram
					//  Serial_unlock(&lock_array[port]);
						/*
						 * xz3100 realtime data process
						 * */
						if(j < 58) {
							p_realtime_data->realtime_data[j] = x2tod(rbuf[3], rbuf[4]);  // update the corresponing value
						}
						/*
						 * xz3100 soe data process
						 * */
						if(j == 58) {
							for(k = 0;k < 12;k++) p_soe_data->soe_data[k] = rbuf[2 + k];
						}
						/*
						 * xz3100 action data process
						 * */
						if(j == 59) {
							for(k = 0;k < 12;k++) p_action_data->action_data[k] = rbuf[2 + k];
						}
						/*
						 * xz3100 opening data process
						 * */
						if(j == 60) {
							for(k = 0;k < 20;k++) p_opening_data->opening_data[k] = rbuf[2 + k];
						}
						/*
						 * xz3100 arg data process
						 * */
						if(j > 60 && j < 118) {
							p_arg_data->arg_data[j - 61] = x2tod(rbuf[3], rbuf[4]);  // update the corresponing value
						}
						/*
						 * xz3100 statistic data process
						 * */
						if(j >= 118) {
							p_statistic_data->statistic_data[j - 118] = x2tod(rbuf[3], rbuf[4]);  // update the corresponing value
						}
					}
					break;
					/*
					 * xz3500
					 * */
					case 3:
						break;
					/*
					 * xz6000
					 * */
					case 4:
						break;
				}
	//		if(addr == first_addr) {
	//			gettimeofday(&start, NULL);
	//		}
	//		if(addr == last_addr) {
	//			gettimeofday(&end, NULL);
	//			timer = 1e6 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;
	//			printf("elapsing time: from the first equipment to the last equipment  = %ldus\n", timer);
	//		}
			p = p->link;
		}
	}
}
/* port3 thread
 * construct xz3100_realtime_data link list
 * construct xz3100_soe_data link list
 * construct xz3100_action_data link list
 * construct xz3100_opening_data link list
 * construct xz3100_arg_data link list
 * construct xz3100_statistic_data link list
 * */
void *port3_update_xz3100_linklist(void *ptr) {
	uchar sbuf[BUFFER_LEN], rbuf[BUFFER_LEN];
	uint j, k;
	uchar len;
	uchar port;  // the port of equipment
	uint addr;  // the addr of equipment
	uint type;  // the type of equipment
	uint bps;   // the speed of equipment
	uint speed; 
	baseinfoNode *p;

	xz3100_realtime_data_Node *p_realtime_data;
	xz3100_soe_data_Node *p_soe_data;
	xz3100_action_data_Node *p_action_data;
	xz3100_opening_data_Node *p_opening_data;
	xz3100_arg_data_Node *p_arg_data;
	xz3100_statistic_data_Node *p_statistic_data;
	while(1) {
		/*
		 * core process
		 * */
		if(!baseinfoNode_array[3]) break;  // there is null pointer, pay attention
		p = baseinfoNode_array[3]->link;
		while(p) {                    // there is a equipment
			port = p->info[0];
			addr = p->info[1];
			type = p->info[2];
			bps  = p->info[3];
			speed = atoi(bpsArray[bps]);
			if(!portInit[port]) {   // to initialize the port
				SerialInit(port, speed);
				portInit[port] = 1;
			}
			SerialSetSpeed(port, speed);            // to set the speed for the port
			switch(type) {
				/*
				 * xz2000
				 * */
				case 0:
					break;
				/*
				 * xz3000
				 * */
				case 1:
					break;
				/*
				 * xz3100
				 * */
				case 2:
					/*
					 * find corresponding port's equipment and then send and collect paragram
					 * */
					p_realtime_data = locate_xz3100_realtime_data_pointer(addr, port);
					p_soe_data = locate_xz3100_soe_data_pointer(addr, port);
					p_action_data = locate_xz3100_action_data_pointer(addr, port);
					p_opening_data = locate_xz3100_opening_data_pointer(addr, port);
					p_arg_data = locate_xz3100_arg_data_pointer(addr, port);
					p_statistic_data = locate_xz3100_statistic_data_pointer(addr, port);
					for(j = 0;j < xz3100_funcArray0Num;j++) {
						xz3100_funcArray0[j](addr, sbuf, &len);
					//	usleep(1e5);                   // let os schedule other threads
					//	Serial_lock(&lock_array[port]);
						WriteSerial(port, sbuf, len);   // sending request paragram
						usleep(1e5);
						ReadSerial(port, rbuf, &len);   // collecting the back paragram
					//  Serial_unlock(&lock_array[port]);
						/*
						 * xz3100 realtime data process
						 * */
						if(j < 58) {
							p_realtime_data->realtime_data[j] = x2tod(rbuf[3], rbuf[4]);  // update the corresponing value
						}
						/*
						 * xz3100 soe data process
						 * */
						if(j == 58) {
							for(k = 0;k < 12;k++) p_soe_data->soe_data[k] = rbuf[2 + k];
						}
						/*
						 * xz3100 action data process
						 * */
						if(j == 59) {
							for(k = 0;k < 12;k++) p_action_data->action_data[k] = rbuf[2 + k];
						}
						/*
						 * xz3100 opening data process
						 * */
						if(j == 60) {
							for(k = 0;k < 20;k++) p_opening_data->opening_data[k] = rbuf[2 + k];
						}
						/*
						 * xz3100 arg data process
						 * */
						if(j > 60 && j < 118) {
							p_arg_data->arg_data[j - 61] = x2tod(rbuf[3], rbuf[4]);  // update the corresponing value
						}
						/*
						 * xz3100 statistic data process
						 * */
						if(j >= 118) {
							p_statistic_data->statistic_data[j - 118] = x2tod(rbuf[3], rbuf[4]);  // update the corresponing value
						}
					}
					break;
					/*
					 * xz3500
					 * */
					case 3:
						break;
					/*
					 * xz6000
					 * */
					case 4:
						break;
				}
	//		if(addr == first_addr) {
	//			gettimeofday(&start, NULL);
	//		}
	//		if(addr == last_addr) {
	//			gettimeofday(&end, NULL);
	//			timer = 1e6 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;
	//			printf("elapsing time: from the first equipment to the last equipment  = %ldus\n", timer);
	//		}
			p = p->link;
		}
	}
}
/* port4 thread
 * construct xz3100_realtime_data link list
 * construct xz3100_soe_data link list
 * construct xz3100_action_data link list
 * construct xz3100_opening_data link list
 * construct xz3100_arg_data link list
 * construct xz3100_statistic_data link list
 * */
void *port4_update_xz3100_linklist(void *ptr) {
	uchar sbuf[BU                            

热门排行

今日推荐

热门手游