Exemplo n.º 1
0
 /*****************************************************************************
 *
 *    acCanOpen
 *
 *    Purpose:
 *    open can port by name
 *
 *
 *    Arguments:
 *        PortName                - port name
 *        synchronization         - true, synchronization ; false, asynchronous
 *        MsgNumberOfReadBuffer   - message number of read intptr
 *        MsgNumberOfWriteBuffer  - message number of write intptr
 *    Returns:
 *        =0 SUCCESS; or <0 failure
 *
 *****************************************************************************/
 private int acCanOpen(string CanPortName, bool synchronization, uint MsgNumberOfReadBuffer, uint MsgNumberOfWriteBuffer)
 {
     CanPortName = "\\\\.\\" + CanPortName;
     if (!synchronization)
     {
         hDevice = CanDriver.CreateFile(CanPortName, CanDriver.GENERIC_READ + CanDriver.GENERIC_WRITE, 0, IntPtr.Zero, CanDriver.OPEN_EXISTING, CanDriver.FILE_ATTRIBUTE_NORMAL + CanDriver.FILE_FLAG_OVERLAPPED, IntPtr.Zero);
     }
     else
     {
         hDevice = CanDriver.CreateFile(CanPortName, CanDriver.GENERIC_READ + CanDriver.GENERIC_WRITE, 0, IntPtr.Zero, CanDriver.OPEN_EXISTING, CanDriver.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
     }
     if (hDevice.ToInt32() == -1)
     {
         hDevice = INVALID_HANDLE_VALUE;
         return(OPERATION_ERROR);
     }
     if (hDevice != INVALID_HANDLE_VALUE)
     {
         MaxReadMsgNumber  = MsgNumberOfReadBuffer;
         MaxWriteMsgNumber = MsgNumberOfWriteBuffer;
         orgReadBuf        = Marshal.AllocHGlobal((int)(CanDriver.CAN_MSG_LENGTH * 10000));
         orgWriteBuf       = Marshal.AllocHGlobal((int)(CanDriver.CAN_MSG_LENGTH * 10000));
         return(SUCCESS);
     }
     else
     {
         return(OPERATION_ERROR);
     }
 }
Exemplo n.º 2
0
        public ServiceToolCtrl(ref CanDriver CAN)
        {
            this.CAN = CAN;

            ServiceToolWorker = new Thread(ServiceToolBackgroundWorker);
            ServiceToolWorker.IsBackground = true;
            ServiceToolWorker.Start();
        }
Exemplo n.º 3
0
 /*****************************************************************************
 *
 *    acSetCommMask
 *
 *    Purpose:
 *        Execute SetCommMask of AdvCan.
 *
 *
 *    Arguments:
 *        EvtMask    - event type
 *
 *
 *    Returns:
 *        true SUCCESS; or false failure
 *
 *****************************************************************************/
 private bool acSetCommMask(uint EvtMask)
 {
     if (!CanDriver.SetCommMask(hDevice, EvtMask))
     {
         int num1 = Marshal.GetLastWin32Error();
         return(false);
     }
     Marshal.WriteInt32(this.events.evPtr, 0);
     return(true);
 }
Exemplo n.º 4
0
        /*****************************************************************************
        *
        *    acGetStatus
        *
        *    Purpose:
        *        Get the current status of the driver and the CAN Controller
        *
        *
        *    Arguments:
        *        Status    - status buffer
        *    Returns:
        *        =0 SUCCESS; or <0 failure
        *
        *****************************************************************************/
        private int acGetStatus(ref Can.CanDriver.CanStatusPar_t Status)
        {
            bool flag = false;

            flag = CanDriver.DeviceIoControl(hDevice, CanDriver.CAN_IOCTL_STATUS, IntPtr.Zero, 0, lpStatusBuffer, CanDriver.CAN_CANSTATUS_LENGTH, ref OutLen, this.ioctlOvr.memPtr);
            if (!flag)
            {
                return(OPERATION_ERROR);
            }
            Status = (CanDriver.CanStatusPar_t)(Marshal.PtrToStructure(lpStatusBuffer, typeof(CanDriver.CanStatusPar_t)));
            return(SUCCESS);
        }
Exemplo n.º 5
0
        /*****************************************************************************
        *
        *    acClearRxFifo
        *
        *    Purpose:
        *        Clear can port receive buffer
        *
        *
        *    Arguments:
        *
        *    Returns:
        *        =0 SUCCESS; or <0 failure
        *
        *****************************************************************************/
        private int acClearRxFifo()
        {
            bool flag = false;

            Command.cmd = CanDriver.CMD_CLEARBUFFERS;
            Marshal.StructureToPtr(Command, lpCommandBuffer, true);
            flag = CanDriver.DeviceIoControl(hDevice, CanDriver.CAN_IOCTL_COMMAND, lpCommandBuffer, CanDriver.CAN_COMMAND_LENGTH, IntPtr.Zero, 0, ref OutLen, this.ioctlOvr.memPtr);
            if (!flag)
            {
                return(OPERATION_ERROR);
            }
            return(SUCCESS);
        }
Exemplo n.º 6
0
        /*****************************************************************************
        *
        *    acEnterWorkMode
        *
        *    Purpose:
        *        Enter work mode
        *
        *
        *    Arguments:
        *
        *    Returns:
        *        =0 SUCCESS; or <0 failure
        *
        *****************************************************************************/
        private int acEnterWorkMode()
        {
            bool flag;

            Command.cmd = CanDriver.CMD_START;
            Marshal.StructureToPtr(Command, lpCommandBuffer, true);
            flag = CanDriver.DeviceIoControl(hDevice, CanDriver.CAN_IOCTL_COMMAND, lpCommandBuffer, CanDriver.CAN_COMMAND_LENGTH, IntPtr.Zero, 0, ref OutLen, this.ioctlOvr.memPtr);
            if (!flag)
            {
                return(OPERATION_ERROR);
            }
            return(SUCCESS);
        }
Exemplo n.º 7
0
        /*****************************************************************************
        *
        *    acCanWrite
        *
        *    Purpose:
        *        Write can msg
        *
        *
        *    Arguments:
        *        msgWrite              - managed buffer for write
        *        nWriteCount           - msg number for write
        *        pulNumberofWritten    - real msgs have written
        *
        *    Returns:
        *        =0 SUCCESS; or <0 failure
        *
        *****************************************************************************/

        private int acCanWrite(Can.CanDriver.canmsg_t[] msgWrite, uint nWriteCount, ref uint pulNumberofWritten)
        {
            bool flag;
            int  nRet;
            uint dwErr;

            if (nWriteCount > MaxWriteMsgNumber)
            {
                nWriteCount = MaxWriteMsgNumber;
            }
            pulNumberofWritten = 0;
            flag = CanDriver.WriteFile(hDevice, msgWrite, nWriteCount, out pulNumberofWritten, this.txOvr.memPtr);
            if (flag)
            {
                if (nWriteCount > pulNumberofWritten)
                {
                    nRet = TIME_OUT;                          //Sending data timeout
                }
                else
                {
                    nRet = SUCCESS;                               //Sending data ok
                }
            }
            else
            {
                dwErr = (uint)Marshal.GetLastWin32Error();
                if (dwErr == CanDriver.ERROR_IO_PENDING)
                {
                    if (CanDriver.GetOverlappedResult(hDevice, this.txOvr.memPtr, out pulNumberofWritten, true))
                    {
                        if (nWriteCount > pulNumberofWritten)
                        {
                            nRet = TIME_OUT;                    //Sending data timeout
                        }
                        else
                        {
                            nRet = SUCCESS;                         //Sending data ok
                        }
                    }
                    else
                    {
                        nRet = OPERATION_ERROR;                         //Sending data error
                    }
                }
                else
                {
                    nRet = OPERATION_ERROR;                            //Sending data error
                }
            }
            return(nRet);
        }
Exemplo n.º 8
0
        /*****************************************************************************
        *
        *    acWaitEvent
        *
        *    Purpose:
        *        Wait can message or error of the CAN Controller.
        *
        *
        *    Arguments:
        *        msgRead           - managed buffer for read
        *        nReadCount        - msg number that unmanaged buffer can preserve
        *        pulNumberofRead   - real msgs have read
        *        ErrorCode         - return error code when the CAN Controller has error
        *
        *    Returns:
        *        =0 SUCCESS; or <0 failure
        *
        *****************************************************************************/
        private int acWaitEvent(Can.CanDriver.canmsg_t[] msgRead, uint nReadCount, ref uint pulNumberofRead, ref uint ErrorCode)
        {
            int nRet = OPERATION_ERROR;

            ErrorCode       = 0;
            pulNumberofRead = 0;
            if (CanDriver.WaitCommEvent(hDevice, this.events.evPtr, this.events.olPtr) == true)
            {
                EventCode = (uint)Marshal.ReadInt32(this.events.evPtr, 0);
                if ((EventCode & CanDriver.EV_RXCHAR) != 0)
                {
                    nRet = acCanRead(msgRead, nReadCount, ref pulNumberofRead);
                }
                if ((EventCode & CanDriver.EV_ERR) != 0)
                {
                    nRet = OPERATION_ERROR;
                    acClearCommError(ref ErrorCode);
                }
            }
            else
            {
                int err = Marshal.GetLastWin32Error();
                if (CanDriver.ERROR_IO_PENDING == err)
                {
                    if (CanDriver.GetOverlappedResult(hDevice, this.eventOvr.memPtr, out pulNumberofRead, true))
                    {
                        EventCode = (uint)Marshal.ReadInt32(this.events.evPtr, 0);
                        if ((EventCode & CanDriver.EV_RXCHAR) != 0)
                        {
                            nRet = acCanRead(msgRead, nReadCount, ref pulNumberofRead);
                        }
                        if ((EventCode & CanDriver.EV_ERR) != 0)
                        {
                            nRet = OPERATION_ERROR;
                            acClearCommError(ref ErrorCode);
                        }
                    }
                    else
                    {
                        nRet = OPERATION_ERROR;
                    }
                }
                else
                {
                    nRet = OPERATION_ERROR;
                }
            }

            return(nRet);
        }
Exemplo n.º 9
0
        /*****************************************************************************
        *
        *    acCanClose
        *
        *    Purpose:
        *        Close can port
        *
        *
        *    Arguments:
        *
        *    Returns:
        *        =0 SUCCESS; or <0 failure
        *
        *****************************************************************************/
        private int acCanClose()
        {
            if (hDevice != INVALID_HANDLE_VALUE)
            {
                CanDriver.CloseHandle(hDevice);
                Thread.Sleep(100);
                Marshal.FreeHGlobal(orgWriteBuf);
                Marshal.FreeHGlobal(orgReadBuf);
                hDevice = INVALID_HANDLE_VALUE;
            }


            return(SUCCESS);
        }
Exemplo n.º 10
0
        /*****************************************************************************
        *
        *    acSetAcceptanceFilterCode
        *
        *    Purpose:
        *        Set acceptance filter code of the CAN Controller
        *
        *
        *    Arguments:
        *        Code        - acceptance filter code
        *    Returns:
        *        =0 SUCCESS; or <0 failure
        *
        *****************************************************************************/
        private int acSetAcceptanceFilterCode(uint Code)
        {
            bool flag = false;

            Config.target = CanDriver.CONF_ACCC;
            Config.val1   = Code;
            Marshal.StructureToPtr(Config, lpConfigBuffer, true);
            flag = CanDriver.DeviceIoControl(hDevice, CanDriver.CAN_IOCTL_CONFIG, lpConfigBuffer, CanDriver.CAN_CONFIG_LENGTH, IntPtr.Zero, 0, ref OutLen, this.ioctlOvr.memPtr);
            if (!flag)
            {
                return(OPERATION_ERROR);
            }
            return(SUCCESS);
        }
Exemplo n.º 11
0
        /*****************************************************************************
        *
        *    acSetBaud
        *
        *    Purpose:
        *	     Set baudrate of the CAN Controller.The two modes of configuring
        *     baud rate are custom mode and standard mode.
        *     -   Custom mode
        *         If Baud Rate value is user defined, driver will write the first 8
        *         bit of low 16 bit in BTR0 of SJA1000.
        *         The lower order 8 bit of low 16 bit will be written in BTR1 of SJA1000.
        *     -   Standard mode
        *         Target value     BTR0      BTR1      Setting value
        *           10K            0x31      0x1c      10
        *           20K            0x18      0x1c      20
        *           50K            0x09      0x1c      50
        *          100K            0x04      0x1c      100
        *          125K            0x03      0x1c      125
        *          250K            0x01      0x1c      250
        *          500K            0x00      0x1c      500
        *          800K            0x00      0x16      800
        *         1000K            0x00      0x14      1000
        *
        *
        *    Arguments:
        *        BaudRateValue     - baudrate will be set
        *    Returns:
        *        =0 SUCCESS; or <0 failure
        *
        *****************************************************************************/
        private int acSetBaud(uint BaudRateValue)
        {
            bool flag;

            Config.target = CanDriver.CONF_TIMING;
            Config.val1   = BaudRateValue;
            Marshal.StructureToPtr(Config, lpConfigBuffer, true);
            flag = CanDriver.DeviceIoControl(hDevice, CanDriver.CAN_IOCTL_CONFIG, lpConfigBuffer, CanDriver.CAN_CONFIG_LENGTH, IntPtr.Zero, 0, ref OutLen, this.ioctlOvr.memPtr);
            if (!flag)
            {
                return(OPERATION_ERROR);
            }
            return(SUCCESS);
        }
Exemplo n.º 12
0
        /*****************************************************************************
        *
        *    acSetListenOnlyMode
        *
        *    Purpose:
        *        Set listen only mode of the CAN Controller
        *
        *
        *    Arguments:
        *        ListenOnly        - true, open only listen mode; false, close only listen mode
        *    Returns:
        *        =0 succeeded; or <0 Failed
        *
        *****************************************************************************/
        private int acSetListenOnlyMode(bool ListenOnly)
        {
            bool flag;

            Config.target = CanDriver.CONF_LISTEN_ONLY_MODE;
            if (ListenOnly)
            {
                Config.val1 = 1;
            }
            else
            {
                Config.val1 = 0;
            }
            Marshal.StructureToPtr(Config, lpConfigBuffer, true);
            flag = CanDriver.DeviceIoControl(hDevice, CanDriver.CAN_IOCTL_CONFIG, lpConfigBuffer, CanDriver.CAN_CONFIG_LENGTH, IntPtr.Zero, 0, ref OutLen, this.ioctlOvr.memPtr);
            if (!flag)
            {
                return(OPERATION_ERROR);
            }
            return(SUCCESS);
        }
Exemplo n.º 13
0
        /*****************************************************************************
        *
        *    acSetSelfReception
        *
        *    Purpose:
        *        Set support for self reception
        *
        *
        *    Arguments:
        *        SelfFlag      - true, open self reception; false, close self reception
        *    Returns:
        *        =0 SUCCESS; or <0 failure
        *
        *****************************************************************************/
        private int acSetSelfReception(bool SelfFlag)
        {
            bool flag;

            Config.target = CanDriver.CONF_SELF_RECEPTION;
            if (SelfFlag)
            {
                Config.val1 = 1;
            }
            else
            {
                Config.val1 = 0;
            }
            Marshal.StructureToPtr(Config, lpConfigBuffer, true);
            flag = CanDriver.DeviceIoControl(hDevice, CanDriver.CAN_IOCTL_CONFIG, lpConfigBuffer, CanDriver.CAN_CONFIG_LENGTH, IntPtr.Zero, 0, ref OutLen, this.ioctlOvr.memPtr);
            if (!flag)
            {
                return(OPERATION_ERROR);
            }
            return(SUCCESS);
        }
Exemplo n.º 14
0
 void Init_Can()
 {
     CAN = new CanDriver();
     CAN.IntCanDriver();
     CAN_SetupFilters();
     CAN.UiEvent += CAN_UiEvent;
     CAN.OpenCanChannel(0);
 }
Exemplo n.º 15
0
 void DeInit_Can()
 {
     if (CAN != null)
     {
         CAN.Kill_Threads();
         CAN = null;
     }
 }
Exemplo n.º 16
0
 /*****************************************************************************
 *
 *    acGetCommMask
 *
 *    Purpose:
 *        Execute GetCommMask of AdvCan.
 *
 *
 *    Arguments:
 *        EvtMask     - event type
 *
 *
 *    Returns:
 *        true SUCCESS; or false failure
 *
 *****************************************************************************/
 private bool acGetCommMask(ref uint EvtMask)
 {
     return(CanDriver.GetCommMask(hDevice, ref EvtMask));
 }
Exemplo n.º 17
0
 /*****************************************************************************
 *
 *    acClearCommError
 *
 *    Purpose:
 *        Execute ClearCommError of AdvCan.
 *
 *
 *    Arguments:
 *        ErrorCode      - error code if the CAN Controller occur error
 *
 *
 *    Returns:
 *        true SUCCESS; or false failure
 *
 *****************************************************************************/
 private bool acClearCommError(ref uint ErrorCode)
 {
     CanDriver.COMSTAT lpState = new CanDriver.COMSTAT();
     return(CanDriver.ClearCommError(hDevice, out ErrorCode, out lpState));
 }
Exemplo n.º 18
0
        /*****************************************************************************
        *
        *    acCanRead
        *
        *    Purpose:
        *        Read can message.
        *
        *
        *    Arguments:
        *        msgRead           - managed buffer for read
        *        nReadCount        - msg number that unmanaged buffer can preserve
        *        pulNumberofRead   - real msgs have read
        *
        *    Returns:
        *        =0 SUCCESS; or <0 failure
        *
        *****************************************************************************/
        private int acCanRead(Can.CanDriver.canmsg_t[] msgRead, uint nReadCount, ref uint pulNumberofRead)
        {
            bool flag;
            int  nRet;
            uint i;

            if (nReadCount > MaxReadMsgNumber)
            {
                nReadCount = MaxReadMsgNumber;
            }
            pulNumberofRead = 0;
            flag            = CanDriver.ReadFile(hDevice, orgReadBuf, nReadCount, out pulNumberofRead, this.rxOvr.memPtr);
            if (flag)
            {
                if (pulNumberofRead == 0)
                {
                    nRet = TIME_OUT;
                }
                else
                {
                    for (i = 0; i < pulNumberofRead; i++)
                    {
                        if (OS_TYPE == 8)
                        {
                            msgRead[i] = (CanDriver.canmsg_t)(Marshal.PtrToStructure(new IntPtr(orgReadBuf.ToInt64() + CanDriver.CAN_MSG_LENGTH * i), typeof(CanDriver.canmsg_t)));
                        }
                        else
                        {
                            msgRead[i] = (CanDriver.canmsg_t)(Marshal.PtrToStructure(new IntPtr(orgReadBuf.ToInt32() + CanDriver.CAN_MSG_LENGTH * i), typeof(CanDriver.canmsg_t)));
                        }
                    }
                    nRet = SUCCESS;
                }
            }
            else
            {
                if (CanDriver.GetOverlappedResult(hDevice, this.rxOvr.memPtr, out pulNumberofRead, true))
                {
                    if (pulNumberofRead == 0)
                    {
                        nRet = TIME_OUT;                               //Package receiving timeout
                    }
                    else
                    {
                        for (i = 0; i < pulNumberofRead; i++)
                        {
                            if (OS_TYPE == 8)
                            {
                                msgRead[i] = (CanDriver.canmsg_t)(Marshal.PtrToStructure(new IntPtr(orgReadBuf.ToInt64() + CanDriver.CAN_MSG_LENGTH * i), typeof(CanDriver.canmsg_t)));
                            }
                            else
                            {
                                msgRead[i] = (CanDriver.canmsg_t)(Marshal.PtrToStructure(new IntPtr(orgReadBuf.ToInt32() + CanDriver.CAN_MSG_LENGTH * i), typeof(CanDriver.canmsg_t)));
                            }
                        }
                        nRet = SUCCESS;
                    }
                }
                else
                {
                    nRet = OPERATION_ERROR;                                    //Package receiving error
                }
            }
            return(nRet);
        }