A problem about RTAI fifo

sculinux sculinux at gmail.com
Sat Jul 8 10:04:38 CEST 2006


Hello, everyone:
    I have a question about fifo.
    I open and write "dev/rtf0" in user space,while I create
fifo(number is 0) in RTAI kernel and read some data from it.
    When I write data(the data value is from 1 to 11) to "dev/rtf0" in
user space, I
get data from fifo(number is 0) in RTAI kernel, but the data value(about from
-30000 to 30000)I got is different from the data value(from 1 to 11) I wrote in
"dev/rtf0". Why? Can you help me? Thanks a lot.

My code in user space is:
void MyMainWindow::FifoTest()
{

       int CMDF0;
       struct StrRung scan_rung;
       if ((CMDF0 = open("/dev/rtf0", O_WRONLY)) < 0) {
               fprintf(stderr, "Error opening /dev/rtf0\n");
               exit(1);
       }
       int j,k;
               for (j=0;j<RUNG_HEIGHT;j++)
               {
                       for (k=0;k<RUNG_WIDTH;k++)
                       {
scan_rung.Element[k][j].Type=BufRung.Element[k][j].Type=1;
scan_rung.Element[k][j].DynamicInput=BufRung.Element[k][j].DynamicInput;
scan_rung.Element[k][j].DynamicState=BufRung.Element[k][j].DynamicState;
scan_rung.Element[k][j].DynamicOutput=BufRung.Element[k][j].DynamicOutput;
printf("%d ", scan_rung.Element[k][j].Type);
                       }
               }

        if( write(CMDF0,&scan_rung, sizeof (struct StrRung))<0)
       {
        fprintf(stderr, "Can't send code_rst to CMDF0\n");
        exit(1);
        }
        printf("write data to fifo!\n");
}


My code in RTAI kernel is:
static void GetRungsCode(int arg) {
       struct StrRung scan_rung;
       int n,j,k;

       while (1)
       {
n= rtf_get(CMDF0,&scan_rung,sizeof(struct StrRung));

       {
       for (j=0;j<RUNG_HEIGHT;j++)
               {
                       for (k=0;k<RUNG_WIDTH;k++)
                       {
BufRung.Element[k][j].Type=scan_rung.Element[k][j].Type;
BufRung.Element[k][j].DynamicInput=scan_rung.Element[k][j].DynamicInput;
BufRung.Element[k][j].DynamicState=scan_rung.Element[k][j].DynamicState;
BufRung.Element[k][j].DynamicOutput=scan_rung.Element[k][j].DynamicOutput;
                       rt_printk("GeVa=%d ",BufRung.Element[k][j].Type);
                       }
               }
       }

rt_task_wait_period();

       }
return;
}


int init_module(void)
{
 RTIME tick;

 tick = start_rt_timer(nano2count(PERIOD/100));

 rtf_create(CMDF0, 50*sizeof(struct StrRung));
 rtf_create(CMDF2, 50*sizeof(struct StrRung));

 rt_task_init(&getrungsthread, GetRungsCode, 0, 4000, 0, 0, 0);
 rt_task_make_periodic(&getrungsthread, rt_get_time()+tick, tick*100);

 rt_task_init(&rtscanthread, RefreshAllRungs, 0, 4000,  0, 0, 0);
 rt_task_make_periodic(&rtscanthread, rt_get_time()+tick, tick*100);

 rt_task_init(&revaluetouserthread, ReturnValueToUser, 0, 4000,  0, 0, 0);
 rt_task_make_periodic(&revaluetouserthread, rt_get_time()+tick, tick*100);

 return 0;
}

void cleanup_module(void)
{
 stop_rt_timer();
 rt_task_delete(&getrungsthread);
 rt_task_delete(&rtscanthread);
 rt_task_delete(&revaluetouserthread);
 rtf_destroy(CMDF0);
 rtf_destroy(CMDF2);

}

And struct StrRung is:
typedef struct StrElement
{
   short int Type;
   char ConnectedWithTop;
   int VarType;
   int VarNum;

   char DynamicInput;
   char DynamicState;
   char DynamicVarBak;
   char DynamicOutput;
   int var0;
   int var1;
}StrElement;

#define LGT_LABEL 10
#define LGT_COMMENT 30

typedef struct StrRung
{
   int Used;
   int PrevRung;
   int NextRung;
   char Label[LGT_LABEL];
   char Comment[LGT_COMMENT];
   StrElement Element[RUNG_WIDTH][RUNG_HEIGHT];
}StrRung;


ScuLinux




More information about the Rtai mailing list