mdl.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "plt.h"
  2. #include "mdl.h"
  3. //uint8_t DO_status[8] = {0};
  4. void mdl_sim()
  5. {
  6. int i = 0;
  7. for(i = 1; i <= 8; i++){
  8. //DO_status[i - 1] = 0;
  9. chan_write_gpio(i,MDL.DO[i - 1]);
  10. }
  11. for(i = 1; i <= 4; i++){
  12. chan_read_gpio(i + 8,&MDL.DI[i - 1]);
  13. }
  14. }
  15. void mdl_prepare()
  16. {
  17. int i = 0;
  18. chan_init();
  19. for(i = 1; i <= 8; i++){
  20. MDL.DO[i - 1] = 0;
  21. chan_write_gpio(i,0);
  22. }
  23. for(i = 1; i <= 4; i++){
  24. chan_read_gpio(i + 8,&MDL.DI[i - 1]);
  25. }
  26. }
  27. void from_modbus_to_dev()
  28. {
  29. //return 0;
  30. }
  31. int from_dev_to_modbus()
  32. {
  33. // uab = usHoldingRegisters[0x6020];
  34. return 0;
  35. }
  36. void update_states()
  37. {
  38. }
  39. static void *mdl_thrd_main(void *param)
  40. {
  41. mdl_prepare();
  42. from_dev_to_modbus();
  43. while (1)
  44. {
  45. from_modbus_to_dev();
  46. mdl_sim();
  47. from_dev_to_modbus();
  48. usleep(10000); /* 10ms */
  49. }
  50. return NULL;
  51. }
  52. int mdl_init()
  53. {
  54. log_dbg("%s, ++", __func__);
  55. int ret = 0;
  56. pthread_t xthrd;
  57. if (pthread_create(&xthrd, NULL, mdl_thrd_main, NULL) != 0)
  58. {
  59. log_dbg("%s, pthread_create fail", __func__);
  60. ret = -1;
  61. }
  62. log_dbg("%s, --, ret: %d", __func__, ret);
  63. return ret;
  64. }