『Cプログラミング診断室』目次次(第1章 普通の初心者 何だ、このプログラムは!!)

第1章 普通の初心者

プログラムの紹介


では、さっそく読者から送られてきたプログラムを紹介しましょう。実際はいくつものファイル に分かれていて、全体では約2000行のプログラム中の一つのファイルを紹介します。オリジナ ルは736行のソースファイル(リスト1−1)です。

まず、リストを見て、あなたはどのように感じますか?「変だ!?」と思うこと、気になること を、以下を読む前に、項目をあげて紙に書き並べてください。

通信エミュレータのプログラムで、なおかつ今回はリストの前半しか掲載していないため、十分 に内容を把握するのは不可能でしょう。でも、そんなことは気にしないで、プログラミングスタイ ルなどについて列挙してください。

リスト1−1 オリジナルプログラム

     1  /******************** TYTLE **********************
     2  
     3                   "MLINK3.C"
     4  
     5  ********************** USE ***********************
     6  
     7      MELSEC <-> PC9801 LINK Experiment
     8      USING TEX_0,1,2 (one SEND and CATCH)
     9      SPECIAL EDITION
    10      (MELSEC DEBUG SPECIAL)
    11  
    12  *********************** BY ***********************
    13  
    14  
    15  ********************* OPTION *********************
    16  
    17      USING MRS .obj (my rs232-c source)
    18            MIT1.obj (my tool-1  source)
    19            MEL.obj  (MELSEC UTILITY FUNCTION source)
    20      make  MLINK3.MAK
    21  
    22  ******************** HISTORY *********************
    23  
    24  1990.4.18            MADE(DD1.C)
    25       5.15            tex_* タイオウ
    26  
    27  
    28  **************************************************/
    29  
    30  
    31  
    32  
    33  #include <dos.h>
    34  #include <stdio.h>
    35  #include <memory.h>
    36  #include <time.h>
    37  #include <mrs.h>
    38  #include <mit1.h>
    39  #include <mel.h>
    40  #include <process.h>
    41  #include <string.h>
    42  
    43  
    44  #define WAIT 1                                  /* ケイサンキ ガワ マチジカン */
    45  #define START_RENBAN 0                          /* レンバン ショキチ */
    46  #define FILENAME "meldata.txt"                  /* データ(text2)ファイル */
    47  #define MENU_N 7                                /* メニユー ノ カズ */
    48  
    49  static unsigned char dwfname[15];               /* データ ファイル(イレコ) */
    50  
    51  static unsigned char dwtext2[12*4+1];               /* データ ファイル(イレコ) */
    52  
    53  
    54  int tex_1_len = 17;                             /* text1 チョウ */
    55  
    56  int tex_2_len = 13;                             /* text2 チョウ */
    57  
    58  int iren = 0;                                   /* レンゾク オクリヨウ */
    59  
    60  int irenban = START_RENBAN;                     /* デンブンレンバン */
    61  
    62  
    63  
    64  
    65  
    66  int call_melsec(style,ccc)
    67  int style;                                      /* テンソウ スタイル   */
    68  unsigned char ccc[200];                         /* キャラクター   */
    69  {
    70  
    71      unsigned char send_d[250];          /* ソウシンホンタイ */
    72  
    73  /*********   カキコミ   ***********/
    74      static unsigned char ww[7] = "00FFWW";      /* ワード タンイ */
    75      static unsigned char bw[7] = "00FFBW";      /* ビット タンイ */
    76  
    77  
    78  /*********   ヨミダシ  ***********/
    79      static unsigned char wr[7] = "00FFWR";      /* ワード タンイ               */
    80      static unsigned char br[7] = "00FFBR";      /* ビット タンイ               */
    81  
    82  
    83      static unsigned char sum[3];                /* サムチェック                 */
    84      static unsigned char r_sum[3];              /* ヘンシン リターン サムチェック       */
    85      static unsigned char r_type[200];           /* リターン コード ナイヨウ     */
    86  
    87      unsigned char c;
    88      unsigned char r_code;                       /* リターン コード              */
    89      
    90      
    91      int scr = 0;                                /* サム チェック カンスウ リターン      */
    92      int result = 0;                             /* ケッカ (1=ACK,2=NAK,3=ERR)*/
    93      int s_style = 0;                            /* サム チェック ヒカク ケッカ        */
    94      int i = 0;
    95      int num = 0;
    96      int y_len = 0;
    97      int ikk = 0;
    98      int idm;
    99  
   100  /*************   PROGRAM   ****************/
   101  
   102      cls_t(3);
   103      
   104      strset(send_d,0x00);
   105  
   106      rs_melsec();
   107  
   108  
   109      if(iren == 0){
   110  
   111          printf("\n\n ESC キーで送信開始します \n");
   112      
   113          c = 0;
   114          while(1){
   115                  c = keyin();
   116                  if(c == 0x1b) break;
   117                  c = 0x00;
   118          }
   119      }
   120  
   121      else{
   122          printf("\n\n 送信開始します \n");
   123          idm = 0;
   124          while(idm >= 500){
   125               idm++;
   126          }
   127      }
   128  
   129  
   130  
   131  
   132      
   133      switch(style){
   134  
   135          case 1: strcpy(send_d,bw);
   136                  break;
   137          
   138          case 2: strcpy(send_d,ww);
   139                  break;
   140          
   141          case 4: strcpy(send_d,br);
   142                  break;
   143          
   144          case 5: strcpy(send_d,wr);
   145                  break;
   146          
   147      }
   148  
   149      strcat(send_d,ccc);
   150      scr = check_sum(send_d,sum);
   151  
   152      
   153      strcat(send_d,sum);
   154      strcat(send_d,0x00);
   155  
   156      rs_out1(0x05);
   157  
   158      i = 0;
   159      while(send_d[i] != 0x00){
   160          rs_out1(send_d[i]);
   161          i++;
   162      }
   163  
   164      printf("\n 送信完了、文字数  =  %d\n",i);
   165  
   166      wait_t(WAIT);
   167  
   168  
   169  
   170  /************************  MELSEC deta カキコミ  ************************/
   171  
   172      if((style == 1) || (style == 2)){
   173  
   174          num = rs_inn(1,&r_code);
   175  
   176              if(r_code == 0x06){     /*    catch ACK   */
   177              printf("\n ACK(正常返信)を受け取りました。");
   178              num = rs_inn(4,r_type);
   179              result = 1;
   180          }
   181      
   182          if(r_code == 0x15){     /*    catch NAK   */
   183              printf("\n NAK(異常返信)を受け取りました。");
   184              num = rs_inn(6,r_type);
   185              result = 2;
   186          }
   187          else if((r_code != 0x06) && (r_code != 0x15)){
   188              printf("\n 変なコードを受け取りました。コード= %c",r_code);
   189              wait_key(1);
   190              rs_close();
   191              return(3);
   192              
   193          }
   194      
   195      
   196          printf(" \nリターンコード= %c",r_code);
   197          for(i=0; i <= num-1 ;i++){
   198      
   199              printf(" \n %d 番目のリターンコード= %c",i,r_type[i]);
   200          }
   201  
   202      if(result != 1) {
   203          wait_key(1);
   204      }
   205  
   206      else{
   207          wait_t(1);
   208      }
   209  
   210      rs_close();
   211      return(result);
   212      }
   213  
   214  
   215  
   216  /************************  MELSEC deta ヨミコミ  ************************/
   217  
   218      if((style == 4) || (style == 5)){
   219  
   220          r_code = (unsigned char)rs_in1();
   221  
   222          if(r_code == 0x02){                         /*    catch STX   */
   223              printf("\n 正常返信を受け取りました ");
   224              result = 1;
   225          }
   226          else {
   227              printf("\n 変なコードを受け取りました。コード= %c",r_code);
   228              printf("\n NAKコードをおくります");
   229              send_return(0);                         /* return NAK */
   230              wait_key(1);
   231              rs_close();
   232              return(3);
   233          }
   234  
   235  
   236          num = rs_insd(0x03,r_type);
   237  
   238          scr = check_sum(r_type,sum);
   239  
   240          for(i=0; i<2; i++){
   241              r_sum[i] = rs_in1();
   242          }
   243          r_sum[2] = 0x00;
   244  
   245          if((s_style = (strcmpi(sum,r_sum))) != 0){
   246              printf("\n チェックサムが違います \n");
   247              printf("\n NAKコードをおくります");
   248              send_return(0);                         /* return NAK */
   249              wait_key(1);
   250              rs_close();
   251              return(3);
   252          }
   253          
   254          printf(" \n リターンコード= %c",r_code);
   255          printf(" \n 局番          = %c%c",r_type[0],r_type[1]);
   256          printf(" \n PC番号      = %c%c\n\n",r_type[2],r_type[3]);
   257  
   258          wait_t(1);
   259          i = 4;
   260          y_len = 0;
   261          ikk = 1;
   262          cls_t(3);
   263          locate(0,0);
   264          if(style == 4){
   265              while(1){
   266                  if(r_type[i+1] == 0x03){
   267                      printf(" \n %d 番目リターン= %c",i,r_type[i]);
   268                      break;
   269                  }
   270                  printf(" \n %d 番目リターン= %c  |  %d 番目リターン= %c"
   271                              ,i,r_type[i],i+1,r_type[i+1]);
   272                  i = i + 2;
   273                  if(r_type[i] == 0x03) break;
   274                  y_len++;
   275                  if(y_len >= 18){
   276                      y_len = 0;
   277                      wait_key(1);
   278                      cls_t(3);
   279                      locate(0,0);
   280                  }
   281              }
   282          
   283          }
   284  
   285  
   286          else if(style == 5){
   287              while(1){
   288                  printf("\n   リターン %d.....%c%c%c%c",
   289                                              ikk,r_type[i],r_type[i++],
   290                                              r_type[i++],r_type[i++]);
   291                  i++;
   292                  ikk++;
   293                  if(r_type[i] == 0x03) break;
   294                  y_len++;
   295                  if(y_len >= 18){
   296                      y_len = 0;
   297                      wait_key(1);
   298                      cls_t(3);
   299                      locate(0,0);
   300                  }
   301              }
   302          
   303          }
   304  
   305              
   306  
   307  
   308  
   309  
   310          printf("\n ACKコードをおくります");
   311          send_return(1);                             /* return ACK */
   312          wait_key(1);
   313          
   314      rs_close();
   315      return(result);
   316      }
   317  
   318  }
   319  
   320  
   321  
   322  void status_mel()                   /* ステータス ヨミダシ */
   323  {
   324      int status = 0;
   325      int i;
   326      int bit[8];
   327  
   328      rs_melsec();
   329  
   330  
   331      wait_t(WAIT);
   332  
   333      status = rs_status();
   334      
   335      for(i=0;i<=7;i++){
   336          bit[i] = bit_out(status,i);
   337      }
   338      
   339      locate(13,15);
   340      printf("\n       送信レディー       = %x   受信レディー        = %x "
   341                              ,bit[0],bit[1]);
   342      printf("\n       送信データ         = %x   パリティエラー      = %x "
   343                              ,bit[2],bit[3]);
   344      printf("\n       オーバーランエラー = %x   フレーミングエラー  = %x "
   345                              ,bit[4],bit[5]);
   346      printf("\n       ブレーク信号       = %x   データセットレディー= %x "
   347                              ,bit[6],bit[7]);
   348  
   349      wait_key(1);
   350      return;
   351  
   352  }
   353  
   354  void watch_menu()
   355  {
   356      int i;
   357  
   358      struct MENU_T{
   359                  int x;
   360                  int y;
   361      } bi[MENU_N+2];
   362  
   363      char *koumoku[MENU_N+2];
   364  
   365  
   366      bi[0].x = 19;   bi[0].y = 2;
   367      bi[1].x = 11;   bi[1].y = 4;
   368      bi[2].x = 11;   bi[2].y = 6;
   369      bi[3].x = 11;   bi[3].y = 8;
   370      bi[4].x = 45;   bi[4].y = 4;
   371      bi[5].x = 45;   bi[5].y = 6;
   372      bi[6].x = 45;   bi[6].y = 8;
   373      bi[7].x = 37;   bi[7].y = 11;
   374  
   375      koumoku[0] = "MELSEC  転送実験プログラム";
   376      koumoku[1] = "1.  ビット単位書き込み";
   377      koumoku[2] = "2.  ワード単位書き込み(tex2)";
   378      koumoku[3] = "3.  通信データステータス読みだし";
   379      koumoku[4] = "4.  ビット単位読み込み";
   380      koumoku[5] = "5.  ワード単位読み込み(tex1)";
   381      koumoku[6] = "6. バッチ実行";
   382      koumoku[7] = "選択して下さい (0 - 6 、0=終了)";
   383  
   384      for(i=0; i<=MENU_N; i++){
   385          locate(bi[i].x,bi[i].y);
   386          printf("%s",koumoku[i]);
   387      }
   388      return;
   389  }
   390  
   391  void input_para(style,code1)
   392  int style;
   393  unsigned char code1[50];
   394  {
   395      int c = 0;
   396      int cn = 0;
   397      int wl = 0;
   398      int i = 0;
   399      int tex_2_style = 0;
   400      int irulen = 0;
   401  
   402      unsigned char fd[10];
   403      unsigned char dd[200];
   404      unsigned char tn[10];
   405      unsigned char cvmp[200];
   406      unsigned char dwp[2];
   407      unsigned char zero_s;
   408      unsigned char dw;
   409      unsigned char *cvm;
   410      unsigned char *tnn;
   411      unsigned char *tnnr;
   412      unsigned char *dwru;
   413      unsigned char *dwrud1;
   414      unsigned char *dwrud2;
   415      
   416  
   417      tnn  = (unsigned char *)malloc(3);
   418      tnnr = (unsigned char *)malloc(3);
   419      dwrud1= (unsigned char *)malloc(5);
   420      dwrud2= (unsigned char *)malloc(5);
   421  
   422      strset(dwru,0x00);
   423  
   424      zero_s = 0x00;
   425      cvm = &cvmp[0];
   426      if(style==3){
   427              status_mel();
   428              return;
   429      }
   430  
   431      while(c != 1) {
   432          tex_2_style = 0;
   433          locate(13,15);
   434          printf("コマンド=");
   435  
   436          locate(25,15);
   437          if (style==1) printf("BW");
   438          if (style==2) printf("WW");
   439          if (style==4) printf("BR");
   440          if (style==5) printf("WR");
   441              dw = 0;
   442              while(1){
   443                  locate(12,18);
   444                  printf("伝文ウエイト= ");
   445                  locate(31,18);
   446                  scanf("%c",&dw);
   447                  if((dw>= 0x30) && (dw<= 0x39)) break;
   448                  if((dw>= 0x41) && (dw<= 0x46)) break;
   449                  if((dw>= 0x61) && (dw<= 0x66)) break;
   450                  rewind(stdin);
   451              }
   452              
   453              dwp[0] = dw;
   454              dwp[1] = 0x00;
   455              
   456              while(1){
   457                  locate(12,20);
   458                  printf("先頭デバイス= ");
   459                  locate(31,20);
   460                  scanf("%s",fd);
   461                  wl = strlen(fd);
   462                  if(wl == 5) break;
   463              }
   464              cn = 0;
   465              while(1){
   466                  locate(12,22);
   467                  printf("デバイス点数= ");
   468                  if(style == 2){
   469                      locate(31,22);
   470                      printf("%d",tex_2_len);
   471                      cn = tex_2_len;
   472                      break;
   473                  }
   474                  if(style == 5){
   475                      locate(31,22);
   476                      printf("%d",tex_1_len);
   477                      cn = tex_1_len;
   478                      break;
   479                  }
   480                  locate(31,22);
   481                  scanf("%d",&cn);
   482                  if((cn>0) && (cn<=255)) break;
   483                  
   484              }
   485              itoa(cn,tnn,16);
   486              if(cn < 16) {
   487                  strcpy(tnnr,"0");
   488                  strcat(tnnr,tnn);
   489                  strcpy(tnn,tnnr);
   490              }
   491              tnn = strupr(tnn);
   492              strcpy(tn,tnn);
   493              rewind(stdin);
   494              strset(dd,0x00);
   495              if(style == 1){
   496                      for(i=1; i<=cn; i++){
   497                          locate(40,20);
   498                          printf("データ %d   = ",i);
   499                          while(1){
   500                              locate(56,20);
   501                              scanf("%c",&dd[i-1]);
   502                              if( (style == 1) &&
   503                                       ((dd[i-1] == 0x30) || 
   504                                              (dd[i-1] == 0x31)) ){
   505                                              break;
   506                              }
   507                          }
   508                          locate(56+i-1,22);
   509                          printf("%c",dd[i-1]);
   510                          rewind(stdin);
   511                      }
   512                  dd[cn]=0x00;
   513              }
   514          else if(style == 2){
   515              
   516              strset(dd,0x00);
   517              strset(dwrud1,0x00);
   518              
   519              strcpy(dwrud1,"000");
   520  
   521              itoa(irenban,dwrud2,16);
   522              
   523              strcat(dwrud1,dwrud2);
   524  
   525              strset(dwrud2,0x00);
   526  
   527              irulen = strlen(dwrud1);
   528  
   529              strcake(dwrud1,dwrud2,irulen - 3,irulen);
   530  
   531              strcpy(dd,dwrud2);
   532  
   533              strcat(dd,dwtext2);
   534  
   535              locate(40,20);
   536              printf("連番 = %s",dwrud2);
   537          }
   538          locate(45,24);
   539          printf("よろしいですか(y/n)");
   540  
   541          while(1){
   542              locate(72,24);
   543              c = keyyn(1);
   544              if((c == 1) || (c == 0)) break;
   545          }
   546  
   547      }
   548  
   549      if(style == 2) irenban++;
   550  
   551      strcpy(cvm,dwp);
   552      strcat(cvm,fd);
   553      strcat(cvm,tn);
   554      strcat(cvm,dd);
   555      cvm = strupr(cvm);
   556      strcpy(code1,cvm);
   557  
   558      return;
   559  }
   560  
   561  
   562  void input_batch()
   563  {
   564      FILE *fdd;
   565      unsigned char dwbname[40],
   566                      dwxy[40],
   567                      dwxyr[40],
   568                      *dwsdata,
   569                      dwsdata1[40],
   570                      dwsdata2[40];
   571  
   572  
   573      int rc,
   574          c;
   575          
   576          
   577      dwsdata = &dwsdata2[0];
   578  
   579  
   580      while(1){
   581          locate(13,15);
   582          printf("バッチファイル = ");
   583          locate(30,15);
   584          fscanf(stdin,"%s",dwbname);
   585  
   586          if(dwbname[0] == 0x51 || dwbname[0] == 0x71) break;
   587  
   588          if((fdd=fopen(dwbname,"r"))==NULL) {
   589              locate(13,17);
   590              printf("ファイルが有りません");
   591              strset(dwbname,0x00);
   592              rewind(stdin);
   593              continue;
   594          }
   595  
   596          else{
   597  
   598              locate(13,17);
   599              printf("転送を確認しますか(y/n)");
   600  
   601              while(1){
   602                  locate(37,17);
   603                  c = keyyn(1);
   604                  if((c == 1) || (c == 0)) break;
   605              }
   606              if(c == 0){
   607                  iren = 1;
   608              }
   609  
   610              locate(13,17);
   611              cls_t(6);
   612  
   613              while (fscanf(fdd,"%s",dwxy)!= EOF){
   614  
   615                  if(dwxy[0]==0x40) {
   616                      strset(dwxy,0x00);
   617                      continue;
   618                  }
   619  
   620                  strset(dwsdata,0x00);
   621                  strset(dwxyr,0x00);
   622                  strset(dwsdata1,0x00);
   623                  strncpy(dwxyr,dwxy,5);
   624  
   625                  dwxyr[5]=0x00;
   626  
   627                  strcpy(dwsdata1,"0");
   628                  strcat(dwsdata1,dwxyr);
   629                  strcat(dwsdata1,"01");
   630                  strcat(dwsdata1,&dwxy[6]);
   631                  
   632                  locate(13,17);
   633                  printf("send addr. = %s",dwxyr);
   634                  locate(45,17);
   635                  printf("on / off  %c",dwxy[6]);
   636                  dwsdata = strupr(dwsdata1);
   637                  wait_t(2);
   638                  rc = call_melsec(1,dwsdata);
   639          
   640                  if(rc != 1){
   641                      close(*fdd);
   642                      free(dwsdata);
   643                      return;
   644                  }
   645                  strset(dwxy,0x00);
   646              }
   647              
   648              break;
   649  
   650          }
   651      }
   652  
   653  
   654      free(dwsdata);
   655  
   656  
   657  
   658  }
   659  
   660  
   661  
   662  
   663  
   664  
   665  void main(int argc,char *argv[])
   666  {
   667      int style;
   668      int rc;
   669      int idrnd;
   670      FILE *fd;
   671      
   672      unsigned char cc[200];
   673  
   674      cls_func(1);
   675  
   676      strset(dwfname,0x00);
   677  
   678      strcpy(dwfname,FILENAME);
   679      
   680      if((fd=fopen(dwfname,"r"))==NULL) {
   681          cls_t(3);
   682          printf("\x1b[>1l");
   683          locate(20,8);
   684          printf("TEXT1,2用ファイルが見つかりません");
   685          locate(29,11);
   686          printf("終了します\n\n\n\n\n\n\n");
   687          exit(-1);
   688  
   689      }
   690      else{
   691  
   692          fscanf(fd,"%d",&idrnd);
   693  
   694          idrnd = irenban % idrnd;
   695  
   696          fd = fd + idrnd;
   697  
   698          fscanf(fd,"%s",dwtext2);
   699          
   700          fclose(fd);
   701  
   702          while(1){
   703              cls_t(3);
   704              style = 0;
   705              watch_menu();
   706              while(1){
   707                  locate(55,13);
   708                  style = getchar() - 0x30;
   709                  if((style >= 0) && (style <= 6)) break;
   710              }
   711  
   712              if(style == 0){
   713                  break;
   714              }
   715              else {
   716                  if(style == 6){
   717                      input_batch();
   718                      iren = 0;
   719                  }
   720                  else{
   721                      input_para(style,cc);
   722                      if(style != 3){
   723                          rc = call_melsec(style,cc);
   724                      }
   725                  }
   726              }
   727  
   728          }
   729  
   730      }
   731  
   732      cls_t(3);
   733      printf("\x1b[>1l");
   734      locate(29,11);
   735      printf("終了します\n\n\n\n\n\n\n");
   736  }


Copyright1996 Hirofumi Fujiwara. No reproduction or republication without written permission
『Cプログラミング診断室』目次次(第1章 普通の初心者 何だ、このプログラムは!!)