Example #1
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
    *
    *****************************************************************************/

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

        if (nWriteCount > MaxWriteMsgNumber)
        {
            nWriteCount = MaxWriteMsgNumber;
        }
        pulNumberofWritten = 0;
        flag = AdvCan.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 == AdvCan.ERROR_IO_PENDING)
            {
                if (AdvCan.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);
    }
Example #2
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
    *
    *****************************************************************************/
    public int acWaitEvent(AdvCan.canmsg_t[] msgRead, uint nReadCount, ref uint pulNumberofRead, ref uint ErrorCode)
    {
        int nRet = OPERATION_ERROR;

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

        return(nRet);
    }
Example #3
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
    *
    *****************************************************************************/
    public int acCanRead(AdvCan.canmsg_t[] msgRead, uint nReadCount, ref uint pulNumberofRead)
    {
        bool flag;
        int  nRet;
        uint i;

        if (nReadCount > MaxReadMsgNumber)
        {
            nReadCount = MaxReadMsgNumber;
        }
        pulNumberofRead = 0;
        flag            = AdvCan.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] = (AdvCan.canmsg_t)(Marshal.PtrToStructure(new IntPtr(orgReadBuf.ToInt64() + AdvCan.CAN_MSG_LENGTH * i), typeof(AdvCan.canmsg_t)));
                    }
                    else
                    {
                        msgRead[i] = (AdvCan.canmsg_t)(Marshal.PtrToStructure(new IntPtr(orgReadBuf.ToInt32() + AdvCan.CAN_MSG_LENGTH * i), typeof(AdvCan.canmsg_t)));
                    }
                }
                nRet = SUCCESS;
            }
        }
        else
        {
            if (AdvCan.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] = (AdvCan.canmsg_t)(Marshal.PtrToStructure(new IntPtr(orgReadBuf.ToInt64() + AdvCan.CAN_MSG_LENGTH * i), typeof(AdvCan.canmsg_t)));
                        }
                        else
                        {
                            msgRead[i] = (AdvCan.canmsg_t)(Marshal.PtrToStructure(new IntPtr(orgReadBuf.ToInt32() + AdvCan.CAN_MSG_LENGTH * i), typeof(AdvCan.canmsg_t)));
                        }
                    }
                    nRet = SUCCESS;
                }
            }
            else
            {
                nRet = OPERATION_ERROR;                                   //Package receiving error
            }
        }
        return(nRet);
    }