CanManagerClass.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using CCDCount.DLL.AlarmTools;
  2. using LogClass;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace CCDCount.DLL.CanBus
  9. {
  10. public class CanManagerClass
  11. {
  12. UInt32 m_devtype = 4;//USBCAN2
  13. UInt32 m_devind = 0;
  14. UInt32 m_canind = 0;
  15. int DataLen = 8;
  16. VCI_INIT_CONFIG config = new VCI_INIT_CONFIG();
  17. public void OpenDevice()
  18. {
  19. if (CanLibraryClass.VCI_OpenDevice(m_devtype, m_devind, 0) == 0)
  20. {
  21. Console.WriteLine("打开设备失败,请检查设备类型和设备索引号是否正确");
  22. return;
  23. }
  24. }
  25. public void InitCan()
  26. {
  27. //初始化CAN
  28. config.AccCode = Convert.ToUInt32("0x00000000", 16);
  29. config.AccMask = Convert.ToUInt32("0xFFFFFFFF", 16);
  30. config.Timing0 = Convert.ToByte("0x00", 16);
  31. config.Timing1 = Convert.ToByte("0x14", 16);
  32. config.Filter = (Byte)1;
  33. config.Mode = (Byte)0;
  34. if (CanLibraryClass.VCI_InitCAN(m_devtype, m_devind, m_canind, ref config) == 0)
  35. {
  36. Console.WriteLine("初始化Can失败");
  37. SystemAlarm.AlarmAlert(AlarmMessageList.CAN连接失败,"CAN Connect Error", "CAN连接失败","CanManagerClass-InitCan");
  38. return;
  39. }
  40. if (CanLibraryClass.VCI_StartCAN(m_devtype, m_devind, m_canind) == 0)
  41. {
  42. Console.WriteLine("开启Can失败");
  43. SystemAlarm.AlarmAlert(AlarmMessageList.CAN连接失败, "CAN Connect Error", "CAN连接失败", "CanManagerClass-InitCan");
  44. return;
  45. }
  46. SystemAlarm.AlarmCancel(AlarmMessageList.CAN连接失败);
  47. }
  48. public unsafe bool SenMessage(CanSenMessage senMessage)
  49. {
  50. bool result = false;
  51. VCI_CAN_OBJ sendobj = new VCI_CAN_OBJ();
  52. sendobj.RemoteFlag = (byte)(0);
  53. sendobj.ExternFlag = (byte)(0);
  54. sendobj.ID = Convert.ToUInt32(Convert.ToString(0, 16), 16);
  55. sendobj.DataLen = Convert.ToByte(DataLen);
  56. string str = "";
  57. byte SendMessage0 = new byte();
  58. SendMessage0 = senMessage.SendDate;
  59. sendobj.Data[0] = SendMessage0;
  60. //byte SendMessage1 = new byte();
  61. //SendMessage1 |= (byte)(1 << 0);
  62. //sendobj.Data[1] = SendMessage1;
  63. byte SendMessage6 = new byte();
  64. SendMessage6 = (byte)(1 << 0);
  65. sendobj.Data[6] = SendMessage6;
  66. byte SendMessage7 = new byte();
  67. if (!senMessage.IsOK)
  68. SendMessage7 |= (byte)(1 << senMessage.TaiHao);
  69. sendobj.Data[7] = SendMessage7;
  70. str += "发送数据:";
  71. str += string.Format("byte0:{0},byte6:{1},byte7:{2}",
  72. SendMessage0.ToString("000"),
  73. SendMessage6.ToString("000"), SendMessage7.ToString("000"));
  74. if (CanLibraryClass.VCI_Transmit(m_devtype, m_devind, m_canind, ref sendobj, 1) == 0)
  75. {
  76. Console.WriteLine("发送失败");
  77. return result;
  78. }
  79. LOG.log(str, 6);
  80. result = true;
  81. return result;
  82. }
  83. int previousWriteDone = 0;
  84. public unsafe bool GetTrigger(ref bool isFrist)
  85. {
  86. bool result = false;
  87. UInt32 res = new UInt32();
  88. VCI_CAN_OBJ[] m_recobj = new VCI_CAN_OBJ[1000];
  89. res = CanLibraryClass.VCI_Receive(m_devtype, m_devind, m_canind, ref m_recobj[0], 1000, 100);
  90. if (res == 0xFFFFFFFF) res = 0;
  91. if (res != 0)
  92. {
  93. for (UInt32 i = 0; i < res; i++)
  94. {
  95. if (m_recobj[i].RemoteFlag == 0)
  96. {
  97. int writeDone = -1;
  98. fixed (VCI_CAN_OBJ* m_recobj1 = &m_recobj[i])
  99. {
  100. writeDone = m_recobj1->Data[6] & 0x01;
  101. if(isFrist)
  102. {
  103. previousWriteDone = writeDone == 0 ? 1 : 0;
  104. isFrist = false;
  105. LOG.log(string.Format(
  106. "通讯状态初始化,prevateWriteDone:{0},WriteDone:{1},IsFrist:{2}",
  107. previousWriteDone, writeDone, isFrist), 6);
  108. }
  109. }
  110. if ((writeDone == 1 && previousWriteDone == 0) || (writeDone == 0 && previousWriteDone == 1))
  111. {
  112. result = true;
  113. }
  114. previousWriteDone = writeDone;
  115. }
  116. }
  117. }
  118. return result;
  119. }
  120. public void CloseCan()
  121. {
  122. if(CanLibraryClass.VCI_ResetCAN(m_devtype, m_devind, m_canind)==0)
  123. {
  124. LOG.error("CAN关闭失败");
  125. }
  126. }
  127. }
  128. }