?? pubfunc.c
字號:
{
int i;
for(i=0;i<size;i++)
{
if((unsigned char)ch1[i]!=(unsigned char)ch2[i])
return -1;
}
return 0;
}
/*************************************************************************
func: ConvertStrToBuf
type: public
desc: 將一個將代表數字的字符串(如"1234")轉成實際數字存入buf中(如buf[0]=1,
buf[1]=2...)
ret : returns the length of the BCD numeric string.
**************************************************************************/
int ConvertStrToBuf(char *str,char *buf,int size)
{
int i;
for(i=0;i<size;i++)
{
buf[i]=str[i]-'0';
}
return i;
}
void WriteDataToFile(char *filename,char *data,long size,char *mode)
{
FILE *fp;
long retval;
fp=fopen(filename,mode);
if ( fp!=NULL)
{
retval = fwrite(data,sizeof(char),size,fp);
fclose(fp);
if(retval != size)
printf("Write data to %s failed!\n",filename);
}
return;
}
/**********************************************************************
* Dec : 多通道設置窗口
* Ret : Modem卡數量
**********************************************************************/
int MultChlSetup(t_MdmArg *out_pmdmarg, int *out_comm_times, char *cs_type)
{
int i,j,ret;
t_MdmArg mdmarg[8];
int mdm_count = 0;
FILE *fp;
//定義組件
newtComponent form;
newtComponent chk_mdmchl[8][4],entry_phone[8][4];
newtComponent label_mdmchl[4],label_phone[4], label_comm_times, label_radiobutton;
newtComponent entry_comm_times;
newtComponent radiobutton[2];
newtComponent button_ok,button_cancel;
newtComponent co;
char res_chk_mdmchl[8][4],*value_phone[8][4], *value_comm_times;
char default_mdmchl[8][4],default_phone[8][4][8],default_comm_times[8],default_cs_type;
int column = 1,row,pai = 3, width_per_column = 17;
char *str_chk_mdmchl[8][4] =
{
"M0C0","M0C1","M0C2","M0C3",
"M1C0","M1C1","M1C2","M1C3",
"M2C0","M2C1","M2C2","M2C3",
"M3C0","M3C1","M3C2","M3C3",
"M4C0","M4C1","M4C2","M4C3",
"M5C0","M5C1","M5C2","M5C3",
"M6C0","M6C1","M6C2","M6C3",
"M7C0","M7C1","M7C2","M7C3"
};
char *str_radiobutton[2] =
{
"Client",
"Server",
};
//初始化
memset(default_mdmchl,' ',sizeof(default_mdmchl));
memset(default_phone,0,sizeof(default_phone));
strcpy(default_comm_times,"1");
default_cs_type = 0;
for (i=0; i<8; i++)
{
mdmarg[i].mdm_no = -1;
memset(mdmarg[i].chl_no,-1,sizeof(mdmarg[i].chl_no));
mdmarg[i].chl_num = 0;
memset(mdmarg[i].phone,0,sizeof(mdmarg[i].phone));
}
if (out_comm_times) *out_comm_times = 1;
//讀配置文件設置參數
fp = fopen("setupdlg.cfg","rb");
if (fp != NULL)
{
ret = fread((char*)mdmarg,sizeof(t_MdmArg),8,fp);
for (i=0; i<ret; i++)
{
for (j=0; j<4; j++)
{
strcpy(default_phone[i][j],mdmarg[i].phone[j]);
if (mdmarg[i].mdm_no>=0 && mdmarg[i].chl_no[j]>=0)
{
default_mdmchl[i][j] = '*';
}
}
}
ret = fread(out_comm_times,sizeof(char),sizeof(int),fp);
//memcpy(out_comm_times,&mdmarg[ret],sizeof(int));
if (ret<=0 || *out_comm_times<0)
{
*out_comm_times = 1;
}
sprintf(default_comm_times,"%d",*out_comm_times);
ret = fread(&default_cs_type,sizeof(char),sizeof(char),fp);
fclose(fp);
}
newtInit();
newtCls();
newtDrawRootText(1,0,"MNJFQ by XieHongwei");
newtDrawRootText(40,0,"ZhongJie Group.CopyRight(R)(2001-2002)");
newtPushHelpLine("Press tab or cursor key to switch...");
//建立窗口
newtCenteredWindow(70, 20, "ZJ-MNJFQ Setup Window");
//newtOpenWindow(4, 3, 70, 18, "ZJ-MNJFQ Setup Window");
//-------------------------------------------
//建立Form
form = newtForm(NULL, NULL, 0);
//label
for (i=0; i<4; i++)
{
label_mdmchl[i] = newtLabel(width_per_column*i+1, 1, "MdmChlNO");
newtFormAddComponent(form, label_mdmchl[i]);
label_phone[i] = newtLabel(width_per_column*i+1+9, 1, "Phone");
newtFormAddComponent(form, label_phone[i]);
}
//button
button_ok=newtButton(15,16," Go ");
newtFormAddComponent(form, button_ok);
button_cancel=newtButton(45,16,"Exit");
newtFormAddComponent(form, button_cancel);
//Modem and phone
for(i=0; i<8; i++)
{
row = pai;
for (j=0; j<4; j++)
{
chk_mdmchl[i][j] = newtCheckbox(column, row, str_chk_mdmchl[i][j],
default_mdmchl[i][j]," *", NULL);
newtFormAddComponent(form, chk_mdmchl[i][j]);
entry_phone[i][j] = newtEntry(column+9,row++,default_phone[i][j],
7,NULL,NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
newtFormAddComponent(form, entry_phone[i][j]);
}
column += width_per_column;
if (column > width_per_column*4)
{
column = 1;
pai += 5;
}
}
//communicate times
label_comm_times = newtLabel(1, pai, "Communicate times:");
newtFormAddComponent(form, label_comm_times);
entry_comm_times = newtEntry(20,pai,default_comm_times,12,NULL,NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
newtFormAddComponent(form, entry_comm_times);
//radio_button
label_radiobutton = newtLabel(37, pai, "C/S type:");
newtFormAddComponent(form, label_radiobutton);
radiobutton[0] = newtRadiobutton(48, pai, str_radiobutton[0], 1-default_cs_type, NULL);
radiobutton[1] = newtRadiobutton(58, pai, str_radiobutton[1], default_cs_type, radiobutton[0]);
for(i=0; i<2; i++)
newtFormAddComponent(form, radiobutton[i]);
pai += 3;
//button
/*
button_ok=newtButton(15,pai," Go ");
newtFormAddComponent(form, button_ok);
button_cancel=newtButton(45,pai,"Exit");
newtFormAddComponent(form, button_cancel);
*/
//--------------------------------------------
//進入循環
newtBell();
co = newtRunForm(form);
newtBell();
//初始化結果緩沖
for (i=0; i<8; i++)
{
mdmarg[i].mdm_no = -1;
memset(mdmarg[i].chl_no,-1,sizeof(mdmarg[i].chl_no));
mdmarg[i].chl_num = 0;
memset(mdmarg[i].phone,0,sizeof(mdmarg[i].phone));
}
//獲取結果
if (co != button_cancel)
{
for(i=0; i<8; i++)
{
for (j=0; j<4; j++)
{
res_chk_mdmchl[i][j] = newtCheckboxGetValue(chk_mdmchl[i][j]);
value_phone[i][j] = strdup(newtEntryGetValue(entry_phone[i][j]));
}
}
value_comm_times = strdup(newtEntryGetValue(entry_comm_times));
*out_comm_times = atoi(value_comm_times);
free(value_comm_times);
co = newtRadioGetCurrent(radiobutton[0]);
for(i=0; i<2; i++)
{
if(co == radiobutton[i])
default_cs_type = i;
}
mdm_count = 0;
for(i=0; i<8; i++)
{
mdmarg[i].chl_num = 0;
for (j=0; j<4; j++)
{
strcpy(mdmarg[i].phone[j],value_phone[i][j]);
if(res_chk_mdmchl[i][j] == '*')
{
mdmarg[i].chl_no[j]=j;
free(value_phone[i][j]);
(mdmarg[i].chl_num)++;
}
}
if (mdmarg[i].chl_num > 0) //該modem卡有channel被選用
{
mdmarg[i].mdm_no = i;
mdm_count++;
}
}
//保存配置
fp = fopen("setupdlg.cfg","wb");
if (fp != NULL)
{
ret = fwrite((char*)mdmarg,sizeof(t_MdmArg),8,fp);
ret = fwrite(out_comm_times,sizeof(int),1,fp);
ret = fwrite(&default_cs_type,sizeof(char),sizeof(char),fp);
fclose(fp);
}
if (mdm_count >= 8)
mdm_count = 8;
if (out_pmdmarg) memcpy(out_pmdmarg,mdmarg,8*sizeof(t_MdmArg));
if (cs_type) *cs_type = default_cs_type;
}
else
{
mdm_count = -1;
}
//關閉Form
newtFormDestroy(form);
//復原
newtFinished();
system("clear");
return mdm_count;
}
int CompBuf(t_TdkMdmChl *p_tdk,char *in_ch1, char *in_ch2 ,int bytes)
{
int i;
char log_str[256];
int NoError = TRUE;
for (i=0; i<bytes; i++)
{
if (in_ch1[i] != in_ch2[i])
{
Log(p_tdk,"Found difference data at %d byte:",i);
Log(p_tdk,"Wrong data : 0x%.2x",(unsigned char)in_ch1[i]);
Log(p_tdk,"Right data : 0x%.2x",(unsigned char)in_ch2[i]);
NoError = FALSE;
}
}
return NoError;
}
/*************************************************************************
func: JudgeTimeOut
type: private
desc: 獲得失敗通信的統計結果
ret : 已經超時返回IS_TIMEOUT,否則返回0
**************************************************************************/
int JudgeTimeOut(time_t start,time_t timeout)
{
time_t now;
time(&now);
if (difftime(now,start)>timeout)
{
return IS_TIMEOUT;
}
else
{
return TRUE;
}
}
/*****************************************
* 將數據寫入文件
*****************************************/
int WriteToFile(char *filename, char *str,int bytes)
{
FILE *fp;
int retval = 0;
fp = fopen(filename,"wb");
if (fp)
{
retval = fwrite(str,sizeof(char),bytes,fp);
fclose(fp);
}
else
{
return -1;
}
return retval;
}
/*************************************************************************
func: PrintBuf
type: public
**************************************************************************/
void PrintBuf(char *prompt, char * in_buf,int size,int mdm_no,int chl_no)
{
int i;
int bytes = 0;
FILE *fp;
char filename[32];
t_TdkMdmChl tdk;
char put_buf[10*KILO];
tdk.mdm_no = (U8)mdm_no;
tdk.chl_no = (U8)chl_no;
sprintf(filename,"RecvErrData_%d.%d",mdm_no,chl_no);
memset(put_buf,0,sizeof(put_buf));
if (prompt != NULL)
{
bytes += sprintf(put_buf+bytes,"%s:\r\n",prompt);
}
if (in_buf == NULL)
{
if (strlen(put_buf)>0) printf("%s\r\n",put_buf);
return;
}
for(i=0;i<size;i++)
{
bytes += sprintf(put_buf+bytes,"%.2x ",(unsigned char)in_buf[i]);
if ((i+1)%25==0)
{
bytes += sprintf(put_buf+bytes,"\r\n");
}
}
Log(&tdk,"%s",put_buf);
fp = fopen(filename,"ab+");
if (fp!=NULL)
{
fwrite(put_buf,sizeof(char),strlen(put_buf),fp);
fclose(fp);
}
else
{
printf("Can't open %s.\r\n", filename);
}
}
/*******************************************
* 毫秒級延時
*******************************************/
void msleep(U32 sleep_msec)
{
struct timeb tp_start;
struct timeb tp_now;
U32 time_pass;
ftime(&tp_start);
while(1)
{
ftime(&tp_now);
time_pass = (tp_now.time - tp_start.time)*1000 + (tp_now.millitm - tp_start.millitm);
if(time_pass>=sleep_msec)
break;
}
return;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -