Exemple #1
0
        public bool getVoltages(UInt32 CanID)
        {
            CAN.VCI_CAN_OBJ [] rx1, rx2 = {}, rx;
            byte[]             txBuf    = { (byte)CANCMD.CAN_CMD_SEND_VOLTAGES };
            ctl.sendData(txBuf, 0xA, false, true);

            //Get the first can frame and determine the length
            ctl.getCanFrames(out rx1, 1);
            UInt16 cmd          = (ushort)(rx1[0].Data[0] | rx1[0].Data[1] << 8);
            UInt16 voltageCount = (ushort)(rx1[0].Data[2] | rx1[0].Data[3] << 8);

            if (cmd != (byte)CANCMD.CAN_CMD_SEND_VOLTAGES)
            {
                return(false);
            }

            //Receive any more frames required
            int rxLen = (voltageCount - 2 + 3) / 4;

            if (rxLen > 0)
            {
                ctl.getCanFrames(out rx2, rxLen);
            }

            //Copy both receive arrays into one
            rx = new CAN.VCI_CAN_OBJ[rx1.Length + rx2.Length];
            rx1.CopyTo(rx, 0);
            rx2.CopyTo(rx, rx1.Length);

            UInt16 [] voltages = new UInt16 [voltageCount];

            //Get ushort data out of receive array
            for (int i = 0; i < voltageCount; i++)
            {
                voltages[i] = (ushort)(rx[2 * (i + 2) / 8].Data[2 * (i + 2) % 8] | rx[2 * (i + 2) / 8].Data[2 * (i + 2) % 8 + 1] << 8);
            }

            packData.voltages = new float[voltageCount];
            //Convert to float
            for (int i = 0; i < voltageCount; i++)
            {
                packData.voltages[i] = (float)voltages[i] * VOLTS_PER_COUNT;
            }

            if (voltages.Length != config.cellCount)
            {
                return(false);
            }

            return(true);
        }
Exemple #2
0
 public bool getCanFrame(out CAN.VCI_CAN_OBJ frame)
 {
     if (rxFrameCount() > 0)
     {
         frame        = rxBuffer[rxBufferRPos];
         rxBufferRPos = (rxBufferRPos + 1) % RX_BUF_SIZE;
         return(true);
     }
     else
     {
         frame = new CAN.VCI_CAN_OBJ();
         return(false);
     }
 }
Exemple #3
0
        public bool getLoadEn(UInt32 CanID)
        {
            CAN.VCI_CAN_OBJ[] rx1, rx2 = { }, rx;
            byte[]            txBuf    = { (byte)CANCMD.CAN_CMD_SEND_LOAD_EN };
            ctl.sendData(txBuf, 0xA, false, true);

            //Get the first can frame and determine the length
            ctl.getCanFrames(out rx1, 1);
            UInt16 cmd       = (ushort)(rx1[0].Data[0] | rx1[0].Data[1] << 8);
            UInt16 cellCount = (ushort)(rx1[0].Data[2] | rx1[0].Data[3] << 8);

            if (cmd != (byte)CANCMD.CAN_CMD_SEND_LOAD_EN)
            {
                return(false);
            }

            UInt16 bytes = (ushort)((cellCount + 7) / 8);               //Number of bytes requred to hold cellCount bits

            //Receive any more frames required
            int rxLen = (bytes - 4 + 7) / 8;

            if (rxLen > 0)
            {
                ctl.getCanFrames(out rx2, rxLen);
            }

            //Copy both receive arrays into one
            rx = new CAN.VCI_CAN_OBJ[rx1.Length + rx2.Length];
            rx1.CopyTo(rx, 0);
            rx2.CopyTo(rx, rx1.Length);

            packData.loadEn = new bool[cellCount];

            //Extract the load enable bool array from the packed binary data received from the BMS, taking into account 4 byte header
            for (int i = 0; i < cellCount; i++)
            {
                packData.loadEn[i] = (rx[(i / 8 + 4) / 8].Data[(i / 8 + 4) % 8] & (1 << (i % 8))) != 0;
            }

            if (packData.loadEn.Length != config.cellCount)
            {
                return(false);
            }

            return(true);
        }
Exemple #4
0
        public ushort[] getLocalVoltages(UInt32 CanID)
        {
            UInt32 retval;

            CAN.VCI_CAN_OBJ[] send = new CAN.VCI_CAN_OBJ[1];
            CAN.VCI_CAN_OBJ[] receive;

            VoltagesStr vStr;

            send[0].ID         = CanID;
            send[0].TimeFlag   = 0;
            send[0].SendType   = 0;
            send[0].RemoteFlag = 0;
            send[0].ExternFlag = 1;
            send[0].DataLen    = 1;
            send[0].Data       = new byte[8];

            send[0].Data[0] = (byte)CANCMD.CAN_CMD_GET_LOCAL_VOLTAGES;

            retval = CAN.VCI_Transmit(CAN.DEV_USBCAN2, 0, 0, send, 1);
            if (retval != CAN.STATUS_OK)
            {
                return(new ushort[0]);
            }

            ctl.getCanFrames(out receive, 2);

            byte[] buffer = new byte[Marshal.SizeOf(typeof(VoltagesStr))];

            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = receive[i / 8].Data[i % 8];
            }

            vStr = StructTools.byteArray2Struct <VoltagesStr>(buffer);

            return(vStr.voltages);
        }
Exemple #5
0
        private void cmdTx_Click(object sender, EventArgs e)
        {
            UInt32 retval;

            if (CANConnected)
            {
                CAN.VCI_CAN_OBJ[] send    = new CAN.VCI_CAN_OBJ[1];
                CAN.VCI_CAN_OBJ[] receive = new CAN.VCI_CAN_OBJ[3];

                send[0].ID         = 0xA;
                send[0].TimeFlag   = 0;
                send[0].SendType   = 0;
                send[0].RemoteFlag = 0;
                send[0].ExternFlag = 1;
                send[0].DataLen    = 1;
                send[0].Data       = new byte[8];

                send[0].Data[0] = 0;

                retval = CAN.VCI_Transmit(CAN.DEV_USBCAN2, 0, 0, send, 1);

                receive[0].Data = new byte[8];
                receive[1].Data = new byte[8];
                receive[2].Data = new byte[8];

                receive[0].Reserved = new byte[3];
                receive[1].Reserved = new byte[3];
                receive[2].Reserved = new byte[3];

                retval = CAN.VCI_Receive(CAN.DEV_USBCAN2, 0, 0, receive, 50, 1000);
                //retval = CAN.VCI_Receive(CAN.DEV_USBCAN2, 0, 0, receive, 1, 1000);
                //retval = CAN.VCI_Receive(CAN.DEV_USBCAN2, 0, 0, receive, 1, 1000);

                int a;
                a = 0;
            }
        }
Exemple #6
0
        public bool saveCalibration(UInt32 CanID)
        {
            UInt32 retval;

            CAN.VCI_CAN_OBJ[] send = new CAN.VCI_CAN_OBJ[1];
            CAN.VCI_CAN_OBJ[] receive;

            VoltagesStr vStr;

            send[0].ID         = CanID;
            send[0].TimeFlag   = 0;
            send[0].SendType   = 0;
            send[0].RemoteFlag = 0;
            send[0].ExternFlag = 1;
            send[0].DataLen    = 1;
            send[0].Data       = new byte[8];

            send[0].Data[0] = (byte)CANCMD.CAN_CMD_SAVE_CAL;

            retval = CAN.VCI_Transmit(CAN.DEV_USBCAN2, 0, 0, send, 1);
            if (retval != CAN.STATUS_OK)
            {
                return(false);
            }

            ctl.getCanFrames(out receive, 1);

            if (receive[0].Data[0] == (byte)CANCMD.CAN_CMD_SAVE_CAL)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #7
0
        public bool setPassthrough(UInt32 CanID, bool passthrough)
        {
            UInt32 retval;

            CAN.VCI_CAN_OBJ[] send = new CAN.VCI_CAN_OBJ[1];

            send[0].ID         = CanID;
            send[0].TimeFlag   = 0;
            send[0].SendType   = 0;
            send[0].RemoteFlag = 0;
            send[0].ExternFlag = 1;
            send[0].DataLen    = 1;
            send[0].Data       = new byte[8];

            send[0].Data[0] = (byte)(passthrough ? CANCMD.CAN_CMD_SET_BALBUS_PASSTHROUGH : CANCMD.CAN_CMD_CLEAR_BALBUS_PASSTHROUGH);

            retval = CAN.VCI_Transmit(CAN.DEV_USBCAN2, 0, 0, send, 1);
            if (retval != CAN.STATUS_OK)
            {
                return(false);
            }

            return(true);
        }