コード例 #1
0
ファイル: UCan.cs プロジェクト: nalale/SevconUtility
        public bool SendMessage(CAN_Message msg)
        {
            string s = "";

            if (!IsConnected)
            {
                return(false);
            }

            // Стандартные сообщения
            if (msg.Ext == false)
            {
                s  = "t";
                s += (msg.id & 0x7FF).ToString("X3");
                s += msg.dlc.ToString("x");
                for (int i = 0; i < msg.dlc; i++)
                {
                    string sub_str = msg.data[i].ToString("X2");
                    s += sub_str;
                }
                s       += "\r";
                loadCnt += 11 + 8 * msg.dlc + loadExtra;
                loadCntTx++;
            }
            // Расширенные сообщения
            else
            {
                s  = "x";
                s += msg.id.ToString("X8");
                s += msg.dlc.ToString("x");
                for (int i = 0; i < msg.dlc; i++)
                {
                    string sub_str = msg.data[i].ToString("X2");
                    s += sub_str;
                }
                s       += "\r";
                s        = s.Replace("X", "");
                loadCnt += 29 + 8 * msg.dlc + loadExtra;
                loadCntTx++;

                /*
                 *              if (msg.id == 0x18DA17F1 || msg.id == 0x18DAF117)
                 *                      Global.OutputAll(msg.ToString(), TextColor.Tx);
                 */
            }

            //ftdiDevice.Write(s, s.Length, ref numBytesWritten);
            lock (txlocker)
            {
                FTDI_send_msg_list.Add(s);
                txWait.Set();
            }

            TxCount++;

            return(true);
        }
コード例 #2
0
ファイル: UCan.cs プロジェクト: nalale/SevconUtility
        public void recieve_thread()
        {
            uint        numBytesAvailable = 0;
            uint        numBytesRead      = 0;
            int         end_mes;
            string      inBuffer;
            CAN_Message msg = new CAN_Message();

            string buf = "";
            string s;

            while (rxThreadAlive)
            {
                rxWait.WaitOne(100);

                ftStatus = ftdiDevice.GetRxBytesAvailable(ref numBytesAvailable);
                if (numBytesAvailable > 1)
                {
                    s = "";
                    ftdiDevice.Read(out s, numBytesAvailable, ref numBytesRead);
                    buf += s;

                    int end = buf.IndexOf("\r");
                    while (end != -1)
                    {
                        inBuffer = buf.Substring(0, end + 1);
                        buf      = buf.Substring(end + 1);

                        // Парсинг стандартных ID
                        if (inBuffer.StartsWith("t"))
                        {
                            inBuffer = inBuffer.Substring(1);
                            end_mes  = inBuffer.IndexOf("\r");
                            if (end_mes >= 4)
                            {
                                msg     = new CAN_Message();
                                msg.id  = uint.Parse(inBuffer.Substring(0, 3), System.Globalization.NumberStyles.HexNumber);
                                msg.dlc = byte.Parse(inBuffer[3].ToString(), System.Globalization.NumberStyles.HexNumber);
                                int str_ptr = 4;
                                for (int byte_cnt = 0; byte_cnt < msg.dlc; byte_cnt++)
                                {
                                    if ((inBuffer.Length) < str_ptr + 2)
                                    {
                                        break;
                                    }

                                    string sub_str = inBuffer.Substring(str_ptr, 2);
                                    msg.data[byte_cnt] = byte.Parse(sub_str, System.Globalization.NumberStyles.HexNumber);
                                    str_ptr           += 2;
                                }
                                lock (rxlocker)
                                {
                                    loadCnt += 11 + 8 * msg.dlc + loadExtra;
                                    loadCntRx++;
                                    OnReadMessage(msg);
                                    RxCount++;
                                }
                                inBuffer = "";
                            }
                        }

                        else if (inBuffer.StartsWith("x"))                          // Парсинг расширенных ID
                        {
                            end_mes = inBuffer.IndexOf("\r");
                            msg.id  = 0x1101;
                            msg.Ext = true;
                            if (end_mes >= 0)
                            {
                                msg     = new CAN_Message();
                                msg.id  = UInt32.Parse(inBuffer.Substring(1, 8), System.Globalization.NumberStyles.HexNumber);
                                msg.Ext = true;
                                msg.dlc = byte.Parse(inBuffer[9].ToString());
                                int str_ptr = 10;
                                for (int byte_cnt = 0; byte_cnt < msg.dlc; byte_cnt++)
                                {
                                    string sub_str = inBuffer.Substring(str_ptr, 2);
                                    msg.data[byte_cnt] = byte.Parse(sub_str, System.Globalization.NumberStyles.HexNumber);
                                    str_ptr           += 2;
                                }

                                lock (rxlocker)
                                {
                                    /*
                                     *                                  if (msg.id == 0x18DA17F1 || msg.id == 0x18DAF117)
                                     *                                          Global.OutputAll(msg.ToString(), TextColor.Rx);
                                     */
                                    loadCnt += 29 + 8 * msg.dlc + loadExtra;
                                    loadCntRx++;
                                    OnReadMessage(msg);
                                    RxCount++;
                                }
                                inBuffer = "";
                            }
                        }
                        else if (inBuffer.StartsWith("I"))
                        {
                            int n = 1;
                            Model       = Int32.Parse(inBuffer.Substring(n, 2), System.Globalization.NumberStyles.HexNumber);
                            n          += 2;
                            HW_Version  = (UInt16)(Int32.Parse(inBuffer.Substring(n, 2), System.Globalization.NumberStyles.HexNumber) << 8);
                            n          += 2;
                            HW_Version += UInt16.Parse(inBuffer.Substring(n, 2), System.Globalization.NumberStyles.HexNumber);
                            n          += 2;
                            SW_Version  = (UInt16)(Int32.Parse(inBuffer.Substring(n, 2), System.Globalization.NumberStyles.HexNumber) << 8);
                            n          += 2;
                            SW_Version += UInt16.Parse(inBuffer.Substring(n, 2), System.Globalization.NumberStyles.HexNumber);
                        }

                        end = buf.IndexOf("\r");
                    }
                }
            }
        }