一、实际需求
工业设备DA660是专门用来实现工业上可编程设备之间交换信息的交换机。现在要使得DA660采集下行设备的实时数据,然后传送到上位机显示。上位机还可以远程修改DA660的数据库配置。
二、数据库设计
我在DA660中设计了sqlite3数据库,名为da660.db,里面存放很多表,其中有一张表示baseinfo,其结构定义如下:
通道号是从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,显示如下:
2. 测试数据插入
由测试结果可知,数据能够正常插入,因为baseinfo表中port和addr被定义为主码,因此测试结果符合设定要求。
3. 测试数据查询
4. 测试数据删除
客户端f发送数据库操作报文,包括查询,删除和插入,DA660解析报文并发回回送报文。
七、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[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[4]) break; // there is null pointer, pay attention p = baseinfoNode_array[4]->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; } } } /* port5 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 *port5_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[5]) break; // there is null pointer, pay attention p = baseinfoNode_array[5]->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; } } } /* port6 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 *port6_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[6]) break; // there is null pointer, pay attention p = baseinfoNode_array[6]->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; } } } /* port7 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 *port7_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[7]) break; // there is null pointer, pay attention p = baseinfoNode_array[7]->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; } } } /* port8 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 *port8_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[8]) break; // there is null pointer, pay attention p = baseinfoNode_array[8]->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; } } } /* port9 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 *port9_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[9]) break; // there is null pointer, pay attention p = baseinfoNode_array[9]->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; } } } /* port10 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 *port10_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[10]) break; // there is null pointer, pay attention p = baseinfoNode_array[10]->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; } } } /* port11 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 *port11_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[11]) break; // there is null pointer, pay attention p = baseinfoNode_array[11]->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; } } } /* port12 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 *port12_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[12]) break; // there is null pointer, pay attention p = baseinfoNode_array[12]->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; } } } /* port13 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 *port13_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[13]) break; // there is null pointer, pay attention p = baseinfoNode_array[13]->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; } } } /* port14 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 *port14_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[14]) break; // there is null pointer, pay attention p = baseinfoNode_array[14]->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; } } } /* port15 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 *port15_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[15]) break; // there is null pointer, pay attention p = baseinfoNode_array[15]->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; } } } /* * collect equipment info including xz2000, xz3000, xz3100, xz3500, xz6000 * */ void *collectEquipmentInfo(void *ptr) { pthread_t threads[16]; void* thread_result[16]; pthread_create(&threads[0], NULL, port0_update_xz3100_linklist, NULL); // Testing: It is OK! pthread_create(&threads[1], NULL, port1_update_xz3100_linklist, NULL); // Testing: It is OK! pthread_create(&threads[2], NULL, port2_update_xz3100_linklist, NULL); // Testing: It is OK! pthread_create(&threads[3], NULL, port3_update_xz3100_linklist, NULL); // Testing: It is OK! pthread_create(&threads[4], NULL, port4_update_xz3100_linklist, NULL); // Testing: It is OK! pthread_create(&threads[5], NULL, port5_update_xz3100_linklist, NULL); // Testing: It is OK! pthread_create(&threads[6], NULL, port6_update_xz3100_linklist, NULL); // Testing: It is OK! pthread_create(&threads[7], NULL, port7_update_xz3100_linklist, NULL); // Testing: It is OK! pthread_create(&threads[8], NULL, port8_update_xz3100_linklist, NULL); // Testing: It is OK! pthread_create(&threads[9], NULL, port9_update_xz3100_linklist, NULL); // Testing: It is OK! pthread_create(&threads[10], NULL, port10_update_xz3100_linklist, NULL); // Testing: It is OK! pthread_create(&threads[11], NULL, port11_update_xz3100_linklist, NULL); // Testing: It is OK! pthread_create(&threads[12], NULL, port12_update_xz3100_linklist, NULL); // Testing: It is OK! pthread_create(&threads[13], NULL, port13_update_xz3100_linklist, NULL); // Testing: It is OK! pthread_create(&threads[14], NULL, port14_update_xz3100_linklist, NULL); // Testing: It is OK! pthread_create(&threads[15], NULL, port15_update_xz3100_linklist, NULL); // Testing: It is OK! pthread_join(threads[0], &thread_result[0]); pthread_join(threads[1], &thread_result[1]); pthread_join(threads[2], &thread_result[2]); pthread_join(threads[3], &thread_result[3]); pthread_join(threads[4], &thread_result[4]); pthread_join(threads[5], &thread_result[5]); pthread_join(threads[6], &thread_result[6]); pthread_join(threads[7], &thread_result[7]); pthread_join(threads[8], &thread_result[8]); pthread_join(threads[9], &thread_result[9]); pthread_join(threads[10], &thread_result[10]); pthread_join(threads[11], &thread_result[11]); pthread_join(threads[12], &thread_result[12]); pthread_join(threads[13], &thread_result[13]); pthread_join(threads[14], &thread_result[14]); pthread_join(threads[15], &thread_result[15]); } /* * periodically update database * write all equipments info in the form of link list back to the database * */ void updateDatabase() { /* * xz3100 writeback operation * */ /* * test xz3100 soe_data_Node_array * It is Ok! * */ /* xz3100_statistic_data_Node *p; int i, j, k; k = 0; for(i = 0;i < 16;i++) { p = xz3100_statistic_data_Node_array[i]; printf("i = %d, p = %d\n", i, p); while(p) { printf("addr:%d\n", p->addr); for(j = 0;j < 57;j++) printf("%d\t", p->statistic_data[j]); p = p->link; k++; } printf("\n"); printf("totoal node:%d\n", k); } */ xz3100_writeback_realtime_data_table(xz3100_realtime_data_Node_array); xz3100_writeback_soe_data_table(xz3100_soe_data_Node_array); xz3100_writeback_action_data_table(xz3100_action_data_Node_array); xz3100_writeback_opening_data_table(xz3100_opening_data_Node_array); xz3100_writeback_arg_data_table(xz3100_arg_data_Node_array); xz3100_writeback_statistic_data_table(xz3100_statistic_data_Node_array); alarm(20); // never to forget this } /* * to find the addrs of the first and last equipment in the baseinfo linklist * */ void findFirstLastAddr() { int i, j; uint addr, N; baseinfoNode *p; j = 0; N = 0; 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 N++; p = p->link; } } 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 addr = p->info[1]; j++; if(j == 1) first_addr = addr; if(j == N) last_addr = addr; p = p->link; } } // printf("first_addr is %d, last_addr is %d\n", first_addr, last_addr); Testing: It is OK! } int main() { int i; pthread_t threads[19]; void* thread_result[19]; signal(SIGALRM, updateDatabase); alarm(20); // update the database in the periodicity of 2 minutes /* * to initialize baseinfoNode array * */ Init_baseinfoNode_Array(baseinfoNode_array); // Testing: this is ok! findFirstLastAddr(); // Testing: it is OK! /* * to initialize xz3100 modules * */ xz3100_Init0(); // xz3100_Init1(); /* * to create threads * */ pthread_create(&threads[0], NULL, collectEquipmentInfo, NULL); // Testing: It is OK! pthread_create(&threads[1], NULL, db_thread, NULL); // Testing: It is OK! pthread_create(&threads[2], NULL, realtime_data_thread, NULL); // Testing: It is OK! pthread_create(&threads[3], NULL, equipment_control_thread, NULL); // Testing: It is OK! /* * to free threads * */ pthread_join(threads[0], &thread_result[0]); pthread_join(threads[1], &thread_result[1]); pthread_join(threads[2], &thread_result[2]); pthread_join(threads[3], &thread_result[3]); /* * to close the ports * */ for(i = 0;i < 16;i++) SerialClose(i); return 0; }
#include <signal.h> #include <setjmp.h> #include "socket.h" #include "xz3100.h" #define BUFFER_LEN 512 char *serveraddr = "192.168.3.127"; char *equipmentType[] = {"xz2000", "xz3000", "xz3100", "xz3500", "xz6000"}; char *bpsArray[] = {"2400", "4800", "9600", "192000"}; uchar sbuf[BUFFER_LEN]; uchar rbuf[BUFFER_LEN]; uchar port, addr, type, bps; int clientfd; // just be the global int db_port = 4001; int realtime_data_port = 4002; int control_cmd_port = 4003; jmp_buf position; /* * to configure the da660 database remotely * */ int configureDA660() { uchar choice; uchar user_choice[50]; // the no of record which user choice to delete uchar port_addr[50]; // all address in one port should be dynamic created and freed uchar typestr[20]; uchar bpsstr[20]; int len; int i, j; int n; int try = 0; printf("---- Welcome to use DA660 Configure Tool ----\n"); /* * to initialize client * */ TCPClientInit(&clientfd); if(clientfd < 0) { perror("client socket created failed...!\n"); } while(TCPClientConnect(clientfd, serveraddr, db_port)) { perror("client connected failed...!\n"); try++; usleep(1e5); if(try > 5) { // to try 5 times to connect perror("client connected failed...!\n"); goto Exit; } } while(1) { printf("---- 0:Query data, 1:Delet data, 2:Insert data, 3:Quit ----\n"); printf("please input your choice:"); if(scanf("%d", &choice) != 1) { perror("input argument invalid...!\n"); goto Exit; } /* * to check the user's choice * */ if(choice != 0 && choice != 1 && choice !=2 && choice != 3) { perror("Sorry, you have made wrong choice...!\n"); goto Exit; } switch(choice) { case 0: printf("input the port:"); if(scanf("%d", &port) != 1) { perror("input argument invalid...!\n"); goto Exit; } else if(port < 0 || port > 15) { perror("port should be 0 ~ 15...!\n"); goto Exit; } /* * to send query database paragram * */ sbuf[0] = 0; sbuf[1] = port; len = TCPWrite(clientfd, sbuf, 2); if(len <= 0) { perror("Sorry, data have not been sent from client...!\n"); goto Exit; } len = TCPBlockRead(clientfd, rbuf, BUFFER_LEN); if(len <= 0 || rbuf[1]) { fprintf(stderr, "query port %d failed...!\n", port); goto Exit; } else { printf("\t----No----\t----port----\t----addr----\t----type----\t----bps-----\n"); i = 3; len = rbuf[2]; for(j = 1;j <= len;j++) { printf("\t----%2d----\t----%2d----\t----%3d----\t----%6s----\t----%4s-----\n", j, rbuf[i], rbuf[i + 1], equipmentType[rbuf[i + 2]], bpsArray[rbuf[i + 3]]); port_addr[j - 1] = rbuf[i + 1]; i += 4; } port_addr[j - 1] = '\0'; } break; case 1: printf("please input the number of records which need to be deleted, separated with spacing..."); getchar(); fgets(user_choice, 50, stdin); for(i = 0, j = 0;i < strlen(user_choice);i++) { // user have make query operation if(user_choice[i] != ' ' && user_choice[i] != '\n') { j = j * 10 + user_choice[i] - '0'; // to generate the user's choice number } else { if(j - 1 < 0 || j -1 >= strlen(port_addr)) continue; sbuf[0] = 1; sbuf[1] = port; sbuf[2] = port_addr[j - 1]; j = 0; len = TCPWrite(clientfd, sbuf, 3); if(len != 3) { perror("Sorry, data have not been sent correctly from client...!\n"); goto Exit; } len = TCPBlockRead(clientfd, rbuf, BUFFER_LEN); if(len <= 0 || rbuf[1]) { fprintf(stderr, "delete port %d addr %d failed...!\n", sbuf[1], sbuf[2]); goto Exit; } else if(!rbuf[1]) { fprintf(stdout, "delete port %d addr %d successfully...!\n", sbuf[1], sbuf[2]); } } } break; case 2: printf("input the number of records that should be inserted:"); if(scanf("%d", &n) != 1) { perror("input data error, system crash!\n"); goto Exit; } if(n <= 50 && 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"); goto Exit; } 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"); goto Exit; } 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"); goto Exit; } 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"); goto Exit; } 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; } sbuf[0] = 2; sbuf[1] = port; sbuf[2] = addr; sbuf[3] = type; sbuf[4] = bps; len = TCPWrite(clientfd, sbuf, 5); if(len != 5) { perror("Sorry, data have not been sent correctly from client...!\n"); goto Exit; } len = TCPBlockRead(clientfd, rbuf, BUFFER_LEN); if(len <= 0 || rbuf[1]) { fprintf(stderr, "insert data port %d addr %d failed...!\n", sbuf[1], sbuf[2]); goto Exit; } else if(!rbuf[1]) { fprintf(stdout, "insert data port %d addr %d successfully...!\n", sbuf[1], sbuf[2]); } } } break; default: goto Exit; } } Exit: TCPConnectionClose(clientfd); return 0; } /* * to realtime data query remotely * */ int queryDA660() { uint port, addr; uchar sbuf[BUFFER_LEN], rbuf[BUFFER_LEN]; int len; int i, j; int n; int try = 0; char ch; printf("---- Welcome to use DA660 Query Tool ----\n"); /* * to initialize client * */ TCPClientInit(&clientfd); if(clientfd < 0) { perror("client socket created failed...!\n"); } while(TCPClientConnect(clientfd, serveraddr, realtime_data_port)) { perror("client connected failed...!\n"); try++; usleep(1e5); if(try > 5) { // to try 5 times to connect perror("client connected failed...!\n"); goto Exit; } } /* * input port * */ printf("input the port:"); if(scanf("%d", &port) != 1) { perror("input argument invalid...!\n"); goto Exit; } if(port < 0 || port > 15) { perror("port should be 0 ~ 15...!\n"); goto Exit; } /* * input addr * */ printf("input the addr:"); if(scanf("%d", &addr) != 1) { perror("input data error, system crash!\n"); goto Exit; } if(addr < 0 || addr >= 257) { perror("port should be 0 to 256...!"); goto Exit; } /* * to send realtime data query paragram * */ sbuf[0] = port; sbuf[1] = addr; Next: len = TCPWrite(clientfd, sbuf, 2); if(len <= 0) { perror("Sorry, data have not been sent from client...!\n"); goto Exit; } len = TCPBlockRead(clientfd, rbuf, BUFFER_LEN); if(len <= 0) { fprintf(stderr, "realtime data query from port %d addr %d failed...!\n", port, addr); goto Exit; } switch(rbuf[len - 1]) { // to identify equipment type /* * xz2000 realtime data show * */ case 0: break; /* * xz3000 realtime data show * */ case 1: break; /* * xz3100 realtime data show * */ case 2: xz3100_data_show(rbuf, len); break; /* * xz3500 realtime data show * */ case 3: break; /* * xz6000 realtime data show * */ case 4: break; } printf("Do you want to refresh these equipment data...?Y/N:"); getchar(); ch = getchar(); if(ch == 'Y' || ch == 'y') { goto Next; } else goto Exit; Exit: TCPConnectionClose(clientfd); return 0; } /* * to send equipment control paragram remotely * */ int controlDA660() { uint port, addr; int len; int i, j; int n; int try = 0; int choice; struct tm *ptm; time_t ts; printf("---- Welcome to use DA660 Equipment Control Tool ----\n"); /* * to initialize client * */ TCPClientInit(&clientfd); if(clientfd < 0) { perror("client socket created failed...!\n"); } while(TCPClientConnect(clientfd, serveraddr, control_cmd_port)) { perror("client connected failed...!\n"); try++; usleep(1e5); if(try > 5) { // to try 5 times to connect perror("client connected failed...!\n"); goto Exit; } } /* * input port * */ printf("input the port:"); if(scanf("%d", &port) != 1) { perror("input argument invalid...!\n"); goto Exit; } if(port < 0 || port > 15) { perror("port should be 0 ~ 15...!\n"); goto Exit; } /* * input addr * */ printf("input the addr:"); if(scanf("%d", &addr) != 1) { perror("input data error, system crash!\n"); goto Exit; } if(addr < 0 || addr >= 257) { perror("port should be 0 to 256...!"); goto Exit; } sbuf[0] = port; sbuf[1] = addr; /* * operation UI * */ next: printf("---- 0:reset, 1:reset and alarm, 2:start A, 3:start B, 4:stop, 5:broadcastly timing, 6:nonbroadcastly timing, 7:exit ----\n"); printf("please input your choice:"); if(scanf("%d", &choice) != 1) { perror("input argument invalid, Stop to run...!\n"); goto Exit; } /* * to check the user's choice * */ if(!(choice <= 7 && choice >= 0)) { perror("Sorry, you have made wrong choice...!\n"); goto Exit; } switch(choice) { case 0: // reset operation sbuf[2] = 0; len = TCPWrite(clientfd, sbuf, 3); if(len <= 0) { perror("Sorry, data have not been sent from client...!\n"); goto Exit; } else printf("reset at port %d addr %d successfully...!\n", port, addr); break; case 1: sbuf[2] = 1; // reset and alarm operation len = TCPWrite(clientfd, sbuf, 3); if(len <= 0) { perror("Sorry, data have not been sent from client...!\n"); goto Exit; } else printf("reset and alarm at port %d addr %d successfully...!\n", port, addr); break; case 2: sbuf[2] = 2; // start A operation len = TCPWrite(clientfd, sbuf, 3); if(len <= 0) { perror("Sorry, data have not been sent from client...!\n"); goto Exit; } else printf("start A at port %d addr %d successfully...!\n", port, addr); break; case 3: sbuf[2] = 3; // start B operation len = TCPWrite(clientfd, sbuf, 3); if(len <= 0) { perror("Sorry, data have not been sent from client...!\n"); goto Exit; } else printf("start B at port %d addr %d successfully...!\n", port, addr); break; case 4: sbuf[2] = 4; // stop operation len = TCPWrite(clientfd, sbuf, 3); if(len <= 0) { perror("Sorry, data have not been sent from client...!\n"); goto Exit; } else printf("stop at port %d addr %d successfully...!\n", port, addr); break; case 5: // broadcastly timing sbuf[2] = 5; time(&ts); ptm = localtime(&ts); sbuf[3] = (ptm->tm_year + 1900) / 100; sbuf[4] = (ptm->tm_year + 1900) % 100; sbuf[5] = ptm->tm_mon + 1; sbuf[6] = ptm->tm_mday; sbuf[7] = ptm->tm_hour; sbuf[8] = ptm->tm_min; sbuf[9] = ptm->tm_sec; len = TCPWrite(clientfd, sbuf, 10); if(len <= 0) { perror("Sorry, data have not been sent from client...!\n"); goto Exit; } else printf("timing broadcastly at port %d successfully...!\n", port); break; case 6: // nonbroadcastly timing sbuf[2] = 6; time(&ts); ptm = localtime(&ts); sbuf[3] = (ptm->tm_year + 1900) / 100; sbuf[4] = (ptm->tm_year + 1900) % 100; sbuf[5] = ptm->tm_mon + 1; sbuf[6] = ptm->tm_mday; sbuf[7] = ptm->tm_hour; sbuf[8] = ptm->tm_min; sbuf[9] = ptm->tm_sec; len = TCPWrite(clientfd, sbuf, 10); if(len <= 0) { perror("Sorry, data have not been sent from client...!\n"); goto Exit; } else printf("timing nonbroadcastly at port %d addr %d successfully...!\n", port, addr); break; case 7: goto Exit; break; } goto next; Exit: TCPConnectionClose(clientfd); return 0; } int main() { uchar choice; /* * UI tips * */ printf("---- Welcome to DA660 Client System Copyright 2014 ----\n"); while(1) { printf("---- 0:Configure DA660, 1:Query real-time data, 2:Send control cmd, 3:Quit ----\n"); printf("please input your choice:"); if(scanf("%d", &choice) != 1) { perror("input argument invalid, Stop to run...!\n"); goto Exit; } /* * to check the user's choice * */ if(choice != 0 && choice != 1 && choice !=2 && choice != 3) { perror("Sorry, you have made wrong choice...!\n"); char ch; printf("Continue or Quit...?Y/N: "); getchar(); ch = getchar(); if(ch == 'Y' || ch == 'y') continue; else goto Exit; } switch(choice) { case 0: configureDA660(); // Testing: It is OK! break; case 1: queryDA660(); // Testing: It is OK! break; case 2: controlDA660(); // Testing: It is OK! break; default: goto Exit; break; } } Exit: printf("Thank you for your using, Good luck, Bye...!\n"); return 0; }
DA660端:
客户端:
十、编程要点
1. DA660有16个通道,采集数据必须写为16个线程,每个通道1个线程。
2. 报文构造尽量长,减少串口读写时间。
一种远程修改sqlite数据库的方法,布布扣,bubuko.com
原文地址:http://blog.csdn.net/wangzhicheng1983/article/details/34116067