private void initLoop()
        {
            //ActiveCanDriver = Static_Utils.ActiveCANDriver;
            //if (ActiveCanDriver==Common.Consts.CAN_DRIVER_KVASER) {
            //    if (runningKvaser) return;
            //    runningKvaser = true;
            int faultedPackage      = 0;
            int serialPortReadBytes = 0;

            Task.Factory.StartNew(
                action: () => {
                while (true)
                {
                    if (ActiveCanDriver == Common.Consts.CAN_DRIVER_KVASER)
                    {
                        if (hasChangeInNodesList)
                        {
                            lock (LockCanIdsDic) {
                                dic_Loop_CanChanels  = dic_CanChanels.ToDictionary(x => x.Key, x => x.Value);
                                hasChangeInNodesList = false;
                            }
                        }

                        if (_writePackage.Count != 0 && sw.ElapsedMilliseconds >= Globals.sleepForRefresh)
                        {
                            lock (SyncRoot) {
                                if (_writePackage[0] == null)
                                {
                                    Canlib.canFlushReceiveQueue(CANHandle);
                                }
                                else if /*_writepackage[0] reference a nodeid that does't exist*/ (
                                    !dic_Loop_CanChanels.ContainsKey(_writePackage[0].getInt(8)))
                                {
                                    if (faultedPackage > 5)
                                    {
                                        _writePackage.RemoveAt(0);
                                        faultedPackage = 0;
                                        continue;
                                    }
                                    else
                                    {
                                        faultedPackage++;
                                        Thread.Sleep(5);
                                        continue;
                                    }
                                }
                                else
                                {
                                    var pckt = _writePackage[0].Where((s, i) => i < 8).ToArray();
                                    Canlib.canWrite(
                                        handle: dic_Loop_CanChanels[_writePackage[0].getInt(8)],
                                        id: FrequencyManager.MAGIC_NUMBER,
                                        msg: pckt,
                                        dlc: 8,
                                        /*size of array*/
                                        flag: 2);
                                }
                                sw = Stopwatch.StartNew();
                                _writePackage.RemoveAt(0);
                            }
                        }

                        foreach (var nodeId_x_handle in dic_Loop_CanChanels)
                        {
                            byte[] msgReceive  = new byte[16];
                            int msgReceiveSize = 8;
                            int flag           = 2;

                            int _id   = 0;   //(idArr[0] << 8) + (idArr[1]);
                            long time = 100;
                            Canlib.canStatus err;
                            //err = Canlib.canRead(CANHandle, out _id, msgReceive, out msgReceiveSize, out flag, out time);
                            err = Canlib.canRead(
                                nodeId_x_handle.Value,
                                out _id,
                                msgReceive,
                                out msgReceiveSize,
                                out flag,
                                out time);
                            if (err != Canlib.canStatus.canOK)
                            {
                                if (_writePackage.Count == 0)
                                {
                                    Thread.Sleep(10);
                                }
                                continue;
                            }
                            /*let everyone know wich nodeId sent this message*/
                            msgReceive.putInt(12, nodeId_x_handle.Key);
                            innerTranmit(msgReceive);
                            //Debug.WriteLine(msgReceive.ToArray());
                        }
                    }
                    else if (ActiveCanDriver == Common.Consts.INTERFACE_RS232)
                    {
                        if (_writePackage.Count != 0 && sw.ElapsedMilliseconds >= Globals.sleepForRefresh)
                        {
                            lock (SyncRoot) {
                            }
                        }
                    }
                }
            });
            //} else if (ActiveCanDriver == Common.Consts.INTERFACE_RS232) { }
        }
Example #2
0
        public void CANmsgSend(string datastr)
        {
            int collection = 0;

            byte[] sendmsg = strToToHexByte(datastr);
            if (datastr.Length == 0)
            {
                return;
            }
            decimal a = sendmsg.Length;
            decimal b = 7;
            decimal c = (a - 6) / b;

            if (a > 6)
            {
                collection = (int)Math.Ceiling(c);
            }
            else
            {
                collection = 1;
            }
            Canlib.canFlushReceiveQueue(handle);
            // Create an event collection with 2 messages (events)
            if (collection == 1)
            {
                byte[] Send = new byte[8];

                for (int i = 0; i < 7; i++)
                {
                    if (i + 1 > sendmsg.Length)
                    {
                        Send[i + 1] = 0xFF;
                    }
                    else
                    {
                        Send[i + 1] = sendmsg[i];
                    }
                }
                Send[0] = (byte)sendmsg.Length;
                // event 1
                send(DigSendID, 8, Send, 0);
            }
            else if (collection > 1)
            {
                int    length = sendmsg.Length;
                byte[] Send1  = new byte[8];
                Send1[0] = 0x10;
                Send1[1] = (byte)sendmsg.Length;
                for (int i = 0; i < 6; i++)
                {
                    Send1[i + 2] = sendmsg[i];
                }
                send(DigSendID, 8, Send1, 0);
                Thread.Sleep(20);


                length = length - 6;

                for (int i = 1; i < collection + 1; i++)
                {
                    byte[] Send = new byte[8];
                    length  = length - 7;
                    Send[0] = (byte)(0x20 + i);
                    if (length >= 7)
                    {
                        for (int j = 0; j < 7; j++)
                        {
                            Send[j + 1] = sendmsg[(i - 1) * 7 + j + 6];
                        }
                    }
                    else
                    {
                        for (int j = 0; j < 7; j++)
                        {
                            if (((i - 1) * 7 + j + 6) < sendmsg.Length)
                            {
                                Send[j + 1] = sendmsg[(i - 1) * 7 + j + 6];
                            }
                            else
                            {
                                Send[j + 1] = 0xFF;
                            }
                        }
                    }
                    // event 1
                    send(DigSendID, 8, Send, 0);
                    Thread.Sleep(10);
                }
                // Transmit events
            }
        }
 protected void initForKvaser()
 {
     CmdClearBuffer = new RelayCommand(e => Canlib.canFlushReceiveQueue(CANHandle));
 }
 public static void ClearBuffer()
 {
     //todo which buffer?
     Canlib.canFlushReceiveQueue(Instance.CANHandle);
     Instance._writePackage.Add(null);
 }