Example #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;
        }
Example #2
0
        unsafe public void sendFame(UInt32 len, UInt32 startIndex = 0)
        {
            if (Form_SetCANParam.form_setcan.checkCANFDMode.Checked == false)//send can frame
            {
                TPCANStatus result1;
                TPCANMsg    CANMsg;
                CANMsg         = new TPCANMsg();
                CANMsg.DATA    = new byte[8];
                CANMsg.MSGTYPE = 0x00;
                if (len > 48)
                {
                    return;
                }
                CANMsg.ID       = arrSendBuf[startIndex].ID;
                CANMsg.LEN      = arrSendBuf[startIndex].DataLen;
                CANMsg.MSGTYPE |= (arrSendBuf[startIndex].ExternFlag == 1) ? TPCANMessageType.PCAN_MESSAGE_EXTENDED : TPCANMessageType.PCAN_MESSAGE_STANDARD;
                if (arrSendBuf[startIndex].RemoteFlag == 1)
                {
                    CANMsg.MSGTYPE |= TPCANMessageType.PCAN_MESSAGE_RTR;//0数据帧;1远程帧
                }
                for (int i = 0; i < CANMsg.LEN; i++)
                {
                    CANMsg.DATA[i] = arrSendBuf[startIndex].Data[i];
                }

                result1 = PCANBasic.Write(PCAN_PARA1.PCANIndex, ref CANMsg);

                // int result = VCI_Transmit(0x04, 0x00, CANINDEX, ref arrSendBuf[startIndex], len);

                if (result1 < 0)
                {
                    MessageBox.Show("发送失败");
                }
            }
            else//send can fd frame
            {
                TPCANStatus result1;
                TPCANMsgFD  CANMsg;
                CANMsg      = new TPCANMsgFD();
                CANMsg.DATA = new byte[64];
                // CANMsg.MSGTYPE = 0x00;
                if (len > 48)
                {
                    return;
                }
                CANMsg.ID       = arrSendBuf[startIndex].ID;
                CANMsg.DLC      = arrSendBuf[startIndex].DataLen;
                CANMsg.MSGTYPE |= (arrSendBuf[startIndex].ExternFlag == 1) ? TPCANMessageType.PCAN_MESSAGE_EXTENDED : TPCANMessageType.PCAN_MESSAGE_STANDARD;
                CANMsg.MSGTYPE |= (Form_BasicFunction.form_basic.checkEnableCANFD.Checked) ? TPCANMessageType.PCAN_MESSAGE_FD : TPCANMessageType.PCAN_MESSAGE_STANDARD;
                CANMsg.MSGTYPE |= (Form_BasicFunction.form_basic.EnableCANFD_BRS.Checked) ? TPCANMessageType.PCAN_MESSAGE_BRS : TPCANMessageType.PCAN_MESSAGE_STANDARD;
                if (arrSendBuf[startIndex].RemoteFlag == 1)
                {
                    CANMsg.MSGTYPE |= TPCANMessageType.PCAN_MESSAGE_RTR;//0数据帧;1远程帧
                }
                for (int i = 0; i < CANMsg.DLC; i++)
                {
                    CANMsg.DATA[i] = arrSendBuf[startIndex].Data[i];
                }

                result1 = PCANBasic.WriteFD(PCAN_PARA1.PCANIndex, ref CANMsg);

                // int result = VCI_Transmit(0x04, 0x00, CANINDEX, ref arrSendBuf[startIndex], len);

                if (result1 < 0)
                {
                    MessageBox.Show("发送失败");
                }
            }
        }