예제 #1
0
파일: LinDriver.cs 프로젝트: m-sobcz/Jogger
        public string Receive()
        {
            string dataFromDriver = "";

            for (int i = 0; i < 8; i++)
            {
                ReceivedData[i] = 0;
            }
            XLClass.xl_event     receivedEvent = new XLClass.xl_event();
            XLDefine.XL_Status   xlStatus      = XLDefine.XL_Status.XL_SUCCESS;
            XLDefine.WaitResults waitResult    = (XLDefine.WaitResults)WaitForSingleObject(eventHandle, 1000);
            if (waitResult != XLDefine.WaitResults.WAIT_TIMEOUT)
            {
                dataFromDriver += $"[{DateTime.Now.Hour:D2} : {DateTime.Now.Minute:D2} : {DateTime.Now.Second:D2} : {DateTime.Now.Millisecond:D3}]   ";
                while (xlStatus != XLDefine.XL_Status.XL_ERR_QUEUE_IS_EMPTY)
                {
                    xlStatus = driver.XL_Receive(portHandle, ref receivedEvent);
                    switch (receivedEvent.tag)
                    {
                    case XLDefine.XL_EventTags.XL_LIN_MSG:
                        string dir = "RX".PadRight(3);
                        if ((receivedEvent.tagData.linMsgApi.linMsg.flags & XLDefine.XL_MessageFlags.XL_LIN_MSGFLAG_TX)
                            == XLDefine.XL_MessageFlags.XL_LIN_MSGFLAG_TX)
                        {
                            dir = "TX".PadRight(3);
                        }
                        else
                        {
                            for (int i = 0; i < 8; i++)
                            {
                                ReceivedData[i] = receivedEvent.tagData.linMsgApi.linMsg.data[i];
                            }
                        }

                        dataFromDriver += ("XL_LIN_MSG, " + dir + " id: " +
                                           receivedEvent.tagData.linMsgApi.linMsg.id.ToString("X2").PadRight(3) + ", data: " +
                                           receivedEvent.tagData.linMsgApi.linMsg.data[0].ToString("X2") + " " +
                                           receivedEvent.tagData.linMsgApi.linMsg.data[1].ToString("X2") + " " +
                                           receivedEvent.tagData.linMsgApi.linMsg.data[2].ToString("X2") + " " +
                                           receivedEvent.tagData.linMsgApi.linMsg.data[3].ToString("X2") + " " +
                                           receivedEvent.tagData.linMsgApi.linMsg.data[4].ToString("X2") + " " +
                                           receivedEvent.tagData.linMsgApi.linMsg.data[5].ToString("X2") + " " +
                                           receivedEvent.tagData.linMsgApi.linMsg.data[6].ToString("X2") + " " +
                                           receivedEvent.tagData.linMsgApi.linMsg.data[7].ToString("X2")) + " (cha-1) " +
                                          receivedEvent.chanIndex.ToString();
                        break;

                    case XLDefine.XL_EventTags.XL_LIN_ERRMSG:
                        dataFromDriver += ("XL_LIN_ERRMSG");
                        break;

                    case XLDefine.XL_EventTags.XL_LIN_SYNCERR:
                        dataFromDriver += ("XL_LIN_SYNCERR");
                        break;

                    case XLDefine.XL_EventTags.XL_LIN_NOANS:
                        dataFromDriver += ("XL_LIN_NOANS") + " (cha-1) " + receivedEvent.chanIndex.ToString();
                        break;

                    case XLDefine.XL_EventTags.XL_LIN_WAKEUP:
                        dataFromDriver += ("XL_LIN_WAKEUP");
                        break;

                    case XLDefine.XL_EventTags.XL_LIN_SLEEP:
                        dataFromDriver += ("XL_LIN_SLEEP");
                        break;

                    case XLDefine.XL_EventTags.XL_LIN_CRCINFO:
                        dataFromDriver += ("XL_LIN_CRCINFO");
                        break;
                    }
                }
            }
            else
            {
                dataFromDriver += "Timeout";
            }
            //Trace.WriteLine(dataFromDriver);
            driver.XL_FlushReceiveQueue(portHandle);
            return(dataFromDriver);
        }