串行端口 ;看不懂我写的东西

时间:2014-02-05 04:51:43

标签: linux serial-port rtai

当我随机生成数据时,我无法读取发送到串口的内容,我想通过COM1发送它,并使用两个不同的实时线程从COM2读取 我使用了RTAI函数,

让我们说;我发送'H''E''L''L''O'一些随机字节和0x01;但是我无法读取'H''E''L''L''O'某个字节和0x01。我读取的值与通过串口发送的值不同。 可能是什么原因? 1-)我在COM1和COM2之间使用零调制解调器电缆 2-)我按顺序发布了作家和读者脚本。 3-)串口发送(写入侧)给出了rt_sperr的错误,很可能是溢出或缓冲器满错误。 4-)我在用户空间中做了这个项目的副本,我遇到了一个RESOURCE TEMPORARY UNAVAILABLE消息,这是否与串口大小有关,如果有的话,你能不能给我一个关于调整串口缓冲区大小的地方。 你建议我采取任何行动吗? 最好

#include <linux/kernel.h>       
#include <linux/module.h>       
#include <linux/version.h>      
#include "rtai.h"              
#include "rtai_sched.h"      
#include "rtai_sem.h"                 

#include "rtai_serial.h"
#ifndef _P1003_1B_VISIBLE
#define MCL_CURRENT 0x0001
#define MCL_FUTURE 0x0002  
#define _P1003_1B_VISIBLE
#endif
#if ! defined(RT_LOWEST_PRIORITY)
#if defined(RT_SCHED_LOWEST_PRIORITY)
#define RT_LOWEST_PRIORITY RT_SCHED_LOWEST_PRIORITY
#else
#error RT_SCHED_LOWEST_PRIORITY not defined
#endif
#endif
#define TASK_PRIORITY 1
#include <linux/errno.h>       
#include <asm/io.h>              
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,0)
MODULE_LICENSE("GPL");
#endif

#define TICK_PERIOD 10000000
#define STACK_SIZE 1024
#define BAUD 115200  
#define NUMBITS 8
#define STOPBITS 1
#define TWOPWR31M1 2147483647   
static RT_TASK simulator_task;  

static RTIME simulator_period_ns = 31857600;
static RTIME now;
static SEM eben; 


static inline long next_rand(long rand)
{
    const long a = 16807;
    const long m = TWOPWR31M1;
    const long q = 127773;
    const long r = 2836;

    long lo, hi;

    hi = rand/q;
    lo = rand - hi*q;
    rand = a*lo - r*hi;
    if (rand <= 0) {
            rand += m;
    }
    return rand;
    }


   static inline long irandu(unsigned long range)
   {
    static long seed = 783637;
    const long m = TWOPWR31M1;

    seed = next_rand(seed);
    return rtai_imuldiv(seed, range, m);
   }


  void generate_rn(int mean, int std, unsigned int *x){

  unsigned int a=0, b=0;
    unsigned long rand_num=0;
       unsigned long rand_max = 100;
    int fp, ret;
         a=mean-2*std;
     b=mean+2*std;

       rand_num = irandu(rand_max);

         *x = a + ((b-a)*rand_num)/rand_max;
             }



    void simulasyon(long int arg)
    {
    unsigned int unit_data=0 ;
    unsigned int size = 367;
    unsigned char mydata[size];  
    int min=0, max=0;
    int rand_num=0;
    int rand_max = 100;
    int std=1;
    int i;
    int ierr;
    mydata[0] = 'A';  
    mydata[1] = 'S';  
    mydata[2] = 'C';  
    mydata[3] = 'I';  
    mydata[size-1] = 0x01;






   while(1){

   for(i = 4 ; i < size-1 ; i ++){

    generate_rn(i, 1, &unit_data);



            mydata[i] = unit_data;

  }

 //ierr= rt_sem_wait(&eben);  

  rt_spclear_tx(COM1);

  ierr= rt_spwrite_timed(COM1,(void *)mydata, size,
   DELAY_FOREVER);  
  //if(ierr=-1){printk("succes\n");}

  rt_task_wait_period();
   }

   return;
   }


  int init_module(void)
  {
  RTIME simulator_period_count; 
  RTIME timer_period_count;     
  int retval;                  
  int res;
  int mutx;

    res=rt_spopen(COM1, BAUD, NUMBITS, STOPBITS,RT_SP_PARITY_NONE,
    RT_SP_NO_HAND_SHAKE, RT_SP_FIFO_DISABLE);
    if(res) {
   printk("Error: rt_spopen\n");

      switch(res) {
   case -ENODEV:
   printk("No device %d\n", res);
   break;
   case -EINVAL:
   printk("Invalid val %d\n", res);
   break;
   case -EADDRINUSE:
   printk("Address in use %d\n", res);
   break;
   default:
   printk("Unknown %d\n", res);
   break;
       }
   do_exit(0);   }



      //rt_typed_sem_init(&eben, 0, BIN_SEM);

     rt_spclear_tx(COM1);


     rt_set_periodic_mode();


       simulator_period_count = nano2count(simulator_period_ns);
       timer_period_count = start_rt_timer(simulator_period_count);



           retval = rt_task_init(&simulator_task,  
             simulasyon, 
             0,            
             1024,         
             RT_LOWEST_PRIORITY, 
             0,            
             0);           
              if (0 != retval) {
if (-EINVAL == retval) {

  printk("periodic task: task structure already in use\n");
} else if (-ENOMEM == retval) {

  printk("periodic task: can't allocate stack\n");
} else {

  printk("periodic task: error initializing task structure\n");
}
          return retval;
             }

          retval = rt_task_make_periodic(&simulator_task, 
                      rt_get_time() + simulator_period_count,
                      simulator_period_count); 
    if (0 != retval) {
if (-EINVAL == retval) {

  printk("periodic task: task structure is invalid\n");
} else {

  printk("periodic task: error starting task\n");
}
        return retval;
       }

         return 0;
           }

      void cleanup_module(void)
         {
        int retval;

      retval = rt_task_delete(&simulator_task);

       if (0 !=  retval) {
    if (-EINVAL == retval) {

      printk("periodic task: task structure is invalid\n");
    } else {
     printk("periodic task: error stopping task\n");
     }
    }
    //rt_stop_timer();
   //rt_busy_sleep(10000000);
    rt_spclose(COM1);
   //rt_sem_delete(&eben);

    return;
    }



    #include <linux/kernel.h>
    #include <linux/module.h>
    #include <linux/version.h>
    #include "rtai.h"
    #include "rtai_serial.h"
    #include "rtai_sched.h"
    #include "rtai_fifos.h"
    #include <rtai_sem.h>

    #define BAUD 115200
    #define NUMBITS 8
    #define STOPBITS 1

    #define TASK_PRIORITY 1
    #define TICK_PERIOD 10000000
    #define STACK_SIZE 1024

    #if ! defined(RT_LOWEST_PRIORITY)
    #if defined(RT_SCHED_LOWEST_PRIORITY)
    #define RT_LOWEST_PRIORITY RT_SCHED_LOWEST_PRIORITY
    #else
    #error RT_SCHED_LOWEST_PRIORITY not defined
    #endif
    #endif  


   #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,0)
   MODULE_LICENSE("GPL");
   #endif
   static RTIME tick_period_ns = 31857600;
   static SEM eben; 
   struct rt_task_struct rt_task1;

   static void  task_read (long int arg)
   {

    int i=0;
unsigned char cha[367];
unsigned char mydata[362];

unsigned int max_data_len=367;
unsigned int max_mydata_len=362;

    int ierr;


while(1){
    rt_spclear_rx(COM2);
   //ierr= rt_sem_wait(&eben);
   rt_spread_timed(COM2, (char *) cha , max_data_len, DELAY_FOREVER);

    for (i=0;i<367;i++){
  //ierr=rt_sem_signal(&eben);
  //printk("ch[%d], %d\n", i, ch[i]);
  }


   if (cha[0]=='A'  && cha[1]=='S' && cha[2]=='C' && cha[3]=='I'  &&              cha[size]==0x01 )
    {
        for (i=0;i<362;i++)
        {
            mydata[i]=cha[i+4];     
        rtf_put(0, (char *)mydata, max_mydata_len);             //PUTTING DATA IN FIFO


    }       
    rt_task_wait_period();


}
return;


   }


  int init_module(void)
 {

 int res;
 int retval;
 RTIME tick_period_get;
 RTIME tick_period_count;

 res=rt_spopen(COM2, BAUD, NUMBITS, STOPBITS, RT_SP_PARITY_ODD, RT_SP_NO_HAND_SHAKE, RT_SP_FIFO_DISABLE);


 if(res) {
  printk("Error: rt_spopen\n");

switch(res) {
   case -ENODEV:
   printk("No device %d\n", res);
   break;
   case -EINVAL:
   printk("Invalid val %d\n", res);
   break;
   case -EADDRINUSE:
   printk("Address in use %d\n", res);
   break;
   default:
   printk("Unknown %d\n", res);
   break;
  }
    do_exit(0);
 }

   rt_spclear_rx(COM2);
   retval = rtf_create(0,4096);
   if (retval) {
   printk("could not create RT-FIFO /dev/rtf0\n");
   return retval;
   }
   rtf_reset(0);
   rt_set_periodic_mode();
   tick_period_get = nano2count(tick_period_ns);
   tick_period_count = start_rt_timer(tick_period_get);

rt_typed_sem_init(&eben, 0, BIN_SEM);
    retval=rt_task_init(&rt_task1, task_read, 0, 1024, RT_LOWEST_PRIORITY,0,0);

if (0 != retval) {
    if (-EINVAL == retval) {
        /* task structure is already in use */
        printk("periodic task: task structure already in use\n");
    } else if (-ENOMEM == retval) {
        /* stack could not be allocated */
        printk("periodic task: can't allocate stack\n");
    } else {
        /* unknown error */
        printk("periodic task: error initializing task structure\n");
    }
    return retval;
}



retval = rt_task_make_periodic(&rt_task1, rt_get_time() + tick_period_get, tick_period_get); 
if (0 != retval) {
    if (-EINVAL == retval) {
        /* task structure is already in use */
        printk("periodic task: task structure is invalid\n");
    } else {

        printk("periodic task: error starting task\n");
    }
    return retval;
}




 return 0;   }

 void cleanup_module(void)
 {

//rt_sem_delete(&eben);
 stop_rt_timer();
 rtf_destroy(0);
 rt_task_delete(&rt_task1);
 rt_spclose(COM2); 

 return;

0 个答案:

没有答案