コード例 #1
0
        public void WriteCan(short Ch, __CanMsg Data, bool FDFlag = false, bool Extended = false)
        {
#if PROGRAM_RUNNING
            //TextBox txtbCurrentTextBox;
            TPCANStatus stsResult;

            // We create a TPCANMsg message structure
            //

            try
            {
                try
                {
                    if (FDFlag == false)
                    {
                        TPCANMsg wCANMsg = new TPCANMsg();
                        wCANMsg.DATA = new byte[8];

                        // We configurate the Message.  The ID (max 0x1FF),
                        // Length of the Data, Message Type (Standard in
                        // this example) and die data
                        //

                        wCANMsg.ID      = (uint)Data.ID;
                        wCANMsg.LEN     = (byte)Data.Length;
                        wCANMsg.MSGTYPE = (Extended == true) ? TPCANMessageType.PCAN_MESSAGE_EXTENDED : TPCANMessageType.PCAN_MESSAGE_STANDARD;
                        //wCANMsg.MSGTYPE = TPCANMessageType.PCAN_MESSAGE_STANDARD;
                        // If a remote frame will be sent, the data bytes are not important.
                        //
                        //if (chbRemote.Checked)
                        //    CANMsg.MSGTYPE |= TPCANMessageType.PCAN_MESSAGE_RTR;
                        //else
                        //{
                        // We get so much data as the Len of the message
                        //

                        for (int i = 0; i < wCANMsg.LEN; i++)
                        {
                            wCANMsg.DATA[i] = Data.DATA[i];
                        }
                        //}

                        // The message is sent to the configured hardware
                        //
                        //stsResult = PCANBasic.GetStatus(m_PcanHandle);

                        //if (stsResult == TPCANStatus.PCAN_ERROR_OK)
                        //{
                        stsResult = PCANBasic.Write(m_PcanHandle[Ch], ref wCANMsg);

                        // The message was successfully sent
                        //
                        if (stsResult == TPCANStatus.PCAN_ERROR_OK)
                        {
                            //WriteErrorCount = 0;
                            return;
                        }
                        else
                        {
                            /*
                             * WriteErrorCount++;
                             *
                             * if (WriteErrorCount < 5)
                             * {
                             *  PCANBasic.Uninitialize(m_PcanHandle);
                             *  OpenCan(sid, sSpeed);
                             * }
                             * else
                             * {
                             *  MessageBox.Show(GetFormatedError(stsResult));// An error occurred.  We show the error.
                             * }
                             */
                        }

                        //}
                        //else
                        //{
                        //    return;
                        //}
                    }
                    else
                    {
                        TPCANMsgFD CANMsg;
                        //TextBox txtbCurrentTextBox;
                        int iLength;

                        // We create a TPCANMsgFD message structure
                        //
                        CANMsg      = new TPCANMsgFD();
                        CANMsg.DATA = new byte[64];

                        // We configurate the Message.  The ID,
                        // Length of the Data, Message Type
                        // and the data
                        //
                        CANMsg.ID       = (uint)Data.ID;
                        CANMsg.DLC      = Convert.ToByte(Data.Length);
                        CANMsg.MSGTYPE  = (Extended == true) ? TPCANMessageType.PCAN_MESSAGE_EXTENDED : TPCANMessageType.PCAN_MESSAGE_STANDARD;
                        CANMsg.MSGTYPE |= TPCANMessageType.PCAN_MESSAGE_FD;
                        //CANMsg.MSGTYPE |= (chbBRS.Checked) ? TPCANMessageType.PCAN_MESSAGE_BRS : TPCANMessageType.PCAN_MESSAGE_STANDARD;

                        // If a remote frame will be sent, the data bytes are not important.
                        //
                        //if (chbRemote.Checked)
                        //    CANMsg.MSGTYPE |= TPCANMessageType.PCAN_MESSAGE_RTR;
                        //else
                        //{
                        // We get so much data as the Len of the message
                        //
                        iLength = GetLengthFromDLC(CANMsg.DLC, (CANMsg.MSGTYPE & TPCANMessageType.PCAN_MESSAGE_FD) == 0);

                        //}

                        // The message is sent to the configured hardware
                        //
                        PCANBasic.WriteFD(m_PcanHandle[Ch], ref CANMsg);
                    }
                }
                catch (Exception Msg)
                {
                    //MessageBox.Show(Msg.Message + "\n" + Msg.StackTrace);
                    uMessageBox.Show(title: "경고", promptText: Msg.Message + "\n" + Msg.StackTrace); //MessageBox.Show(GetFormatedError(stsResult));
                }
            }
            finally
            {
            }
#endif
            return;
        }
コード例 #2
0
        public __CanMsg ReadCan(short Ch, bool FDFlag = false)
        {
            __CanMsg    Msg = new __CanMsg();
            TPCANStatus stsResult;

            // We execute the "Read" function of the PCANBasic
            //
            Msg.DATA   = new byte[8];
            Msg.ID     = -1;
            Msg.Length = 0;

#if PROGRAM_RUNNING
            try
            {
                try
                {
                    for (int i = 0; i < 8; i++)
                    {
                        Msg.DATA[i] = 0x00;
                    }


                    if (FDFlag == false)
                    {
                        TPCANMsg       CANMsg;
                        TPCANTimestamp CANTimeStamp;

                        stsResult = PCANBasic.Read(m_PcanHandle[Ch], out CANMsg, out CANTimeStamp);
                        if (stsResult == TPCANStatus.PCAN_ERROR_OK)
                        {
                            Msg.ID     = (int)CANMsg.ID;
                            Msg.Length = CANMsg.LEN;

                            for (int i = 0; i < CANMsg.LEN; i++)
                            {
                                Msg.DATA[i] = CANMsg.DATA[i];
                            }

                            if (NgCountCheckFlag == true)
                            {
                                if (OkCount < 100)
                                {
                                    OkCount++;
                                }
                            }
                        }

                        if (mControl.PublicFunction != null)
                        {
                            Last = mControl.PublicFunction.timeGetTimems();
                            if (1000 <= (Last - First))
                            {
                                CanReceiveReset(Ch);
                                First = mControl.PublicFunction.timeGetTimems();
                            }
                        }
                    }
                    else
                    {
                        TPCANMsgFD       CANMsg;
                        TPCANTimestampFD CANTimeStamp;


                        // We execute the "Read" function of the PCANBasic
                        //
                        stsResult = PCANBasic.ReadFD(m_PcanHandle[Ch], out CANMsg, out CANTimeStamp);
                        if (stsResult == TPCANStatus.PCAN_ERROR_OK)
                        {
                            Msg.ID     = (int)CANMsg.ID;
                            Msg.Length = CANMsg.DLC;

                            for (int i = 0; i < CANMsg.DLC; i++)
                            {
                                Msg.DATA[i] = CANMsg.DATA[i];
                            }

                            if (NgCountCheckFlag == true)
                            {
                                if (OkCount < 100)
                                {
                                    OkCount++;
                                }
                            }
                        }
                    }
                }
                catch (Exception fMsg)
                {
                    //MessageBox.Show(fMsg.Message + "\n" + fMsg.StackTrace);
                    uMessageBox.Show(title: "경고", promptText: fMsg.Message + "\n" + fMsg.StackTrace); //MessageBox.Show(GetFormatedError(stsResult));
                }
            }
            finally
            {
            }
#endif
            return(Msg);
        }