📄 yt-bak.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
#define YTMAXNUM 16
typedef struct YTCtrlInfo
{
char *name;
char *fileName;
char *codAddr[16];
char *codCtrl[48];
char *codComm[4];
char *codCmd[16];
int CtrlNum; //record how many Ctrl Code
int CmdNum; //record how many Command Code
}YTCTRLINFO, *PYTCTRLINFO;
PYTCTRLINFO TMYTCtrlInfo[YTMAXNUM];
int main(int argc, char **argv)
{
int i, j;
printf("Hello every body!\n");
InitYTInfo();
ReadYTCommand(0, "/hello/AB.COD");
ReadYTCommand(1, "/hello/EE.COD");
ReadYTCommand(2, "/hello/HY.COD");
ReadYTCommand(3, "/hello/PELCO-D.COD");
SendYTCommand(0, 1, "Up_Start");
FreeYTInfo();
//TCVSSendYTCommand(0, 1, "Up_Start", "/hello/AB.COD");
exit (0);
}
//-----------------------------------------------------------------------------
//Name: InitYTInfo()
//Input:
//-----------------------------------------------------------------------------
int InitYTInfo()
{
int i, j;
for(j = 0; j < YTMAXNUM ; j++)
{
TMYTCtrlInfo[j] = malloc(sizeof(YTCTRLINFO));
memset( TMYTCtrlInfo[j], 0, sizeof(YTCTRLINFO) );
//--------------------------------------malloc buffer
TMYTCtrlInfo[j]->name = malloc(64);
TMYTCtrlInfo[j]->fileName = malloc(64);
for(i = 0; i < 16; i++)
{
TMYTCtrlInfo[j]->codAddr[i] = malloc(64);
}
for(i = 0; i < 48; i++)
{
TMYTCtrlInfo[j]->codCtrl[i] = malloc(128);
}
for(i = 0; i < 4; i++)
{
TMYTCtrlInfo[j]->codComm[i] = malloc(32);
}
for(i = 0; i < 16; i++)
{
TMYTCtrlInfo[j]->codCmd[i] = malloc(128);
}
//--------------------------------------
}
return 0;
}
//-----------------------------------------------------------------------------
//Name: InitYTInfo()
//Input:
//-----------------------------------------------------------------------------
int FreeYTInfo()
{
int i, j;
for(j = 0; j < 16 ; j++)
{
//--------------------------------------malloc buffer
free(TMYTCtrlInfo[j]->name);
free(TMYTCtrlInfo[j]->fileName);
for(i = 0; i < 16; i++)
{
free(TMYTCtrlInfo[j]->codAddr[i]);
}
for(i = 0; i < 48; i++)
{
free(TMYTCtrlInfo[j]->codCtrl[i]);
}
for(i = 0; i < 4; i++)
{
free(TMYTCtrlInfo[j]->codComm[i]);
}
for(i = 0; i < 16; i++)
{
free(TMYTCtrlInfo[j]->codCmd[i]);
}
//--------------------------------------
free(TMYTCtrlInfo[j]);
}
return 0;
}
//-----------------------------------------------------------------------------
//Name: ReadYTCommand
//YTName:
// YunTai code filename ex."/user/Cod/AB.COD"
//-----------------------------------------------------------------------------
int ReadYTCommand(int index, char* fileName)
{
FILE *fp;
char *s;
int ret = 0;
int i;
TMYTCtrlInfo[index]->CtrlNum = 0;
TMYTCtrlInfo[index]->CmdNum = 0;
//--------------------------------------malloc buffer
s = malloc(128);
//--------------------------------------
fp = fopen(fileName, "r" );
if(fp == NULL)
{
ret = -1;
goto ReadFreeAll ;
}
//---------------------------Read Address Data
s = fgets(s, 128, fp);
if(strncasecmp(s, "[Address]", 9) != 0)
{
ret = -2;
goto ReadFreeAll ;
}
for(i = 0; i < 16; i++)
{
s = fgets(s, 128, fp);
if(s == NULL)
{
ret = -2;
goto ReadFreeAll ;
}
strcpy(TMYTCtrlInfo[index]->codAddr[i], s);
printf("codAddr[%d] -->%s\n", i, TMYTCtrlInfo[index]->codAddr[i] );
}
//--------------------------- Read Crtl Data "[CtrlCode]"
for(i = 0; i < 100; i++)
{
s = fgets(s, 128, fp);
if(s == NULL)
{
ret = -3;
goto ReadFreeAll ;
}
if(strncasecmp(s, "[CtrlCode]", 10) == 0)
{
break;
}
}
if( i >= 100)
{
ret = -3;
goto ReadFreeAll ;
}
for(i = 0; i < 48; i++)
{
s = fgets(s, 128, fp);
if(s == NULL)
{
ret = -3;
goto ReadFreeAll ;
}
if(strncasecmp(s, "[Comm]", 6) == 0)
{
break;
}
strcpy(TMYTCtrlInfo[index]->codCtrl[i], s);
printf("codCtrl[%d] -->%s\n", i, TMYTCtrlInfo[index]->codCtrl[i] );
}
//--------------------------- Read Comm Data "[Comm]"
if( i >= 48)
{
ret = -4;
goto ReadFreeAll ;
}
TMYTCtrlInfo[index]->CtrlNum = i;
for(i = 0; i < 4; i++)
{
s = fgets(s, 128, fp);
if(s == NULL)
{
ret = -4;
goto ReadFreeAll ;
}
strcpy(TMYTCtrlInfo[index]->codComm[i], s);
printf("codComm[%d] -->%s\n", i, TMYTCtrlInfo[index]->codComm[i] );
}
if (feof(fp) != 0)
{
ret = 0;
goto ReadFreeAll ;
}
//--------------------------- Read Crtl Data "[CmdCode]"
for(i = 0; i < 10; i++)
{
if (feof(fp) != 0)
{
ret = 0;
goto ReadFreeAll ;
}
s = fgets(s, 128, fp);
if(s == NULL)
{
ret = 0;
goto ReadFreeAll ;
}
if(strncasecmp(s, "[CmdCode]", 9) == 0)
{
break;
}
}
if( i >= 10)
{
ret = 0;
goto ReadFreeAll ;
}
for(i = 0; i < 16; i++)
{
if (feof(fp) != 0)
{
ret = 0;
goto ReadCmdEnd; //file end
}
s = fgets(s, 128, fp);
if(s == NULL)
{
ret = 0;
goto ReadCmdEnd ;
}
strcpy(TMYTCtrlInfo[index]->codCmd[i], s);
printf("codCmd[%d] -->%s\n", i, TMYTCtrlInfo[index]->codCmd[i] );
}
ReadCmdEnd:
TMYTCtrlInfo[index]->CmdNum = i;
//--------------------free all
ReadFreeAll:
fclose(fp);
free(s);
//--------------------
printf("ReadYTCommand end -----------------------------------%s, %d, %d, %d !\n", fileName, TMYTCtrlInfo[index]->CtrlNum, TMYTCtrlInfo[index]->CmdNum, ret);
return ret;
}
//-----------------------------------------------------------------------------
//Name: TCVSSendYTCommand
//Input:
// index = 0: yuntai control code
// index = 1: yuntai command
//Address:
// YunTai Address ( 1 --16)
//Command:
// control or command code(see appendix) ex. "Up_Start" "Up_Stop" ......
//YTName:
// YunTai code filename ex."/user/Cod/AB.COD"
//-----------------------------------------------------------------------------
/*
int TCVSSendYTCommand(int Index, int Address, char *Command, char* YTName)
{
int i;
FILE *fp;
char *s;
char *codAddr[16];
char *codCtrl[48];
char *codComm[4];
char *codCmd[16];
char *filename = "/hello/AB.COD"; //
int ret = 0;
printf("Hello world -- 1!\n");
//--------------------------------------malloc buffer
for(i = 0; i < 16; i++)
{
codAddr[i] = malloc(64);
}
for(i = 0; i < 48; i++)
{
codCtrl[i] = malloc(128);
}
for(i = 0; i < 4; i++)
{
codComm[i] = malloc(32);
}
for(i = 0; i < 16; i++)
{
codCmd[i] = malloc(128);
}
s = malloc(128);
//--------------------------------------
//printf("%s ", strtok(s, s1) );
//while( (p = strtok(NULL, s1) ) )
// printf("%s ", p);
//printf("ss\n");
//
fp = fopen(filename, "r" );
if(fp == NULL)
{
printf("fd===NULL\n");
goto FreeAll ;
}
//---------------------------Read Address Data
s = fgets(s, 128, fp);
if(strncasecmp(s, "[Address]", 9) != 0)
{
printf("Code File Error = %s(%s)(%d)\n", filename, s, strncasecmp(s, "[Address]", 9));
}
for(i = 0; i < 16; i++)
{
s = fgets(s, 128, fp);
if(s == NULL)
{
printf("Read File error!");
goto FreeAll ;
}
//ss = strrchr(s, '=');
//if(ss == NULL)
// continue;
//memmove(codAddr[i], ss + 1, strlen(ss) - 1);
strcpy(codAddr[i], s);
printf("codAddr[%d] -->%s\n", i, codAddr[i] );
}
//--------------------------- Read Crtl Data "[CtrlCode]"
for(i = 0; i < 100; i++)
{
s = fgets(s, 128, fp);
if(s == NULL)
{
printf("Read File error!");
goto FreeAll ;
}
if(strncasecmp(s, "[CtrlCode]", 10) == 0)
{
break;
}
}
if( i >= 100)
{
printf("Read File error!");
goto FreeAll ;
}
for(i = 0; i < 48; i++)
{
s = fgets(s, 128, fp);
if(s == NULL)
{
printf("Read File error!");
goto FreeAll ;
}
if(strncasecmp(s, "[Comm]", 6) == 0)
{
break;
}
//ss = strrchr(s, '=');
//if(ss == NULL)
// continue;
//memmove(codCtrl[i], ss + 1, strlen(ss) - 1);
strcpy(codCtrl[i], s);
printf("codCtrl[%d] -->%s\n", i, codCtrl[i] );
}
//--------------------------- Read Comm Data "[Comm]"
if( i >= 48)
{
printf("Read File error!");
goto FreeAll ;
}
for(i = 0; i < 4; i++)
{
s = fgets(s, 128, fp);
if(s == NULL)
{
printf("Read File error!");
goto FreeAll ;
}
//ss = strrchr(s, '=');
//if(ss == NULL)
// continue;
//memmove(codComm[i], ss + 1, strlen(ss) - 1);
strcpy(codComm[i], s);
printf("codComm[%d] -->%s\n", i, codComm[i] );
}
if (feof(fp) != 0)
printf("file end --\n");
//--------------------------- Read Crtl Data "[CmdCode]"
for(i = 0; i < 10; i++)
{
if (feof(fp) != 0)
goto FreeAll; //file end
s = fgets(s, 128, fp);
if(s == NULL)
{
printf("Read File error!");
goto FreeAll ;
}
if(strncasecmp(s, "[CmdCode]", 9) == 0)
{
break;
}
}
if( i >= 10)
{
printf("Read File error!");
goto FreeAll ;
}
for(i = 0; i < 16; i++)
{
if (feof(fp) != 0)
goto FreeAll; //file end
s = fgets(s, 128, fp);
if(s == NULL)
{
printf("Read File End !!!\n!");
goto FreeAll ;
}
//ss = strrchr(s, '=');
//if(ss == NULL)
// continue;
//memmove(codCmd[i], ss + 1, strlen(ss) - 1);
strcpy(codCmd[i], s);
printf("codCmd[%d] -->%s\n", i, codCmd[i] );
}
//--------------------free all
FreeAll:
fclose(fp);
for(i = 0; i < 16; i++)
{
free(codAddr[i]);
}
for(i = 0; i < 48; i++)
{
free(codCtrl[i]);
}
for(i = 0; i < 4; i++)
{
free(codComm[i]);
}
for(i = 0; i < 16; i++)
{
free(codCmd[i]);
}
free(s);
//--------------------
return 0;
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -