cascade_freemodbus.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "cascade_freemodbus.h"
  2. #include "paras.h"
  3. #include "mb_f.h"
  4. #include "pthread.h"
  5. #include "stdio.h"
  6. #include <rtthread.h>
  7. #include "stdio.h"
  8. #include "string.h"
  9. #include <unistd.h>
  10. #include "thread.h"
  11. #define AC_DC_START 4000
  12. #define AC_3_START 6000
  13. enum {
  14. FREE_MB_MODE_MASTER=0,
  15. FREE_MB_MODE_SLAVE,
  16. };
  17. static cascade_freemodbus_t cascade={0};
  18. static void mb_poll(void *para)
  19. {
  20. thread_handle_t *th=(thread_handle_t*)para;
  21. cascade_freemodbus_t *cas=(cascade_freemodbus_t*)th->attr->arg;
  22. if(cas->model == FREE_MB_MODE_SLAVE)
  23. {
  24. eMBInit(MB_RTU, cas->saddr, 2, cas->baund, MB_PAR_NONE);
  25. eMBEnable();
  26. }
  27. printf("start poll_thead\n");
  28. while(1)
  29. {
  30. eMBPoll();
  31. rt_thread_mdelay(10);
  32. }
  33. pthread_exit(NULL);
  34. }
  35. static void thread_entry(void *arg)
  36. {
  37. while(1)
  38. {
  39. printf("start thread_entry\n");
  40. cascade.cache->north.data.ac_dc.dev_cnt = (1 << 8) + 8;
  41. sleep(1);
  42. }
  43. pthread_exit(NULL);
  44. }
  45. static void mb_scan()
  46. {
  47. while(1)
  48. {
  49. sleep(1);
  50. }
  51. pthread_exit(NULL);
  52. }
  53. void cascade_freemodbus_init(void)
  54. {
  55. paras_data_t* p_d = paras_get();
  56. if(p_d->cas.mode == FREE_MB_MODE_MASTER)
  57. {
  58. }else if(p_d->cas.mode == FREE_MB_MODE_SLAVE)
  59. {
  60. cascade.lock = lock_init();
  61. if(p_d->prod.type == PDU_AC_I1O1 || PDU_DC_I1O1 == p_d->prod.type)
  62. {
  63. cascade.north_reg_start = AC_DC_START;
  64. cascade.north_reg_max = 2000;
  65. }
  66. else
  67. {
  68. cascade.north_reg_start = AC_3_START;
  69. cascade.north_reg_max = 3000;
  70. }
  71. cascade.model = FREE_MB_MODE_SLAVE;
  72. cascade.baund = p_d->cas.baud;
  73. cascade.cascade_max = 10;
  74. cascade.cascade_start = 10;
  75. cascade.saddr = p_d->cas.addr;
  76. cascade.cache = (cascade_data_cache *)malloc(sizeof(cascade_data_cache));
  77. if(cascade.cache == NULL)
  78. return;
  79. thread_start(THREAD_ID_CAS_FREE_POLL,mb_poll,&cascade);
  80. thread_start(THREAD_ID_CAS_FREE_ENTRY,thread_entry,NULL);
  81. }else
  82. {
  83. return;
  84. }
  85. }
  86. cascade_freemodbus_t * get_cascade_freemodbus(void)
  87. {
  88. return &cascade;
  89. }