private void rxThread() { uint temp = 0; uint len; while (running) { len = CAN.VCI_Receive(CAN.DEV_USBCAN2, 0, 0, receive, 500, 10); if (len == 0xFFFFFFFF) //Return value of -1 indicates failure { len = 0; } if (len > 20) { temp++; } for (int pkt = 0; pkt < len; pkt++) { rxBuffer[rxBufferWPos] = receive[pkt]; rxBufferWPos = (rxBufferWPos + 1) % RX_BUF_SIZE; } System.Threading.Thread.Sleep(1); } }
public bool balBusTx(UInt32 CanID, byte[] data) { 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 = 3; send[0].Data = new byte[8]; send[0].Data[0] = (byte)CANCMD.CAN_CMD_BALBUS_TX; send[0].Data[1] = 0; for (int i = 0; i < data.Length; i++) { send[0].Data[2] = data[i]; retval = CAN.VCI_Transmit(CAN.DEV_USBCAN2, 0, 0, send, 1); if (retval != CAN.STATUS_OK) { return(false); } System.Threading.Thread.Sleep(10); } return(true); }
//Get the calibration structure from the master unit public bool setCalibration(UInt32 CanID, SlaveConfig sc) { UInt32 retval; CAN.VCI_CAN_OBJ[] send = ctl.allocateCanFrameBuffer(1 + Marshal.SizeOf(typeof(SlaveConfig)) / 8); CAN.VCI_CAN_OBJ[] receive; 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[0] = (byte)CANCMD.CAN_CMD_WRITE_CAL; retval = CAN.VCI_Transmit(CAN.DEV_USBCAN2, 0, 0, send, 1); if (retval != CAN.STATUS_OK) { return(false); } ctl.sendData(StructTools.struct2byteArray <SlaveConfig>(sc), CanID, false, true); ctl.getCanFrames(out receive, 1); if (receive[0].Data[0] == (byte)CANCMD.CAN_CMD_WRITE_CAL) { return(true); } else { return(false); } }
//Get the calibration structure from the master unit public SlaveConfig getCalibration(UInt32 CanID) { SlaveConfig sc = new SlaveConfig(); UInt32 retval; CAN.VCI_CAN_OBJ[] send = new CAN.VCI_CAN_OBJ[1]; CAN.VCI_CAN_OBJ[] receive; 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_READ_CAL; retval = CAN.VCI_Transmit(CAN.DEV_USBCAN2, 0, 0, send, 1); if (retval != CAN.STATUS_OK) { return(sc); } ctl.getCanFrames(out receive, 3); byte[] buffer = new byte[Marshal.SizeOf(typeof(SlaveConfig))]; for (int i = 0; i < buffer.Length; i++) { buffer[i] = receive[i / 8].Data[i % 8]; } return(StructTools.byteArray2Struct <SlaveConfig>(buffer)); }
private void cmdConnect_Click(object sender, EventArgs e) { uint retcode; if (CANConnected) { retcode = CAN.VCI_CloseDevice(CAN.DEV_USBCAN2, 0); if (retcode != CAN.STATUS_OK) { MessageBox.Show("Couldn't close CAN device, error " + retcode.ToString()); return; } CANConnected = false; cmdConnect.Text = "Connect"; } else { retcode = CAN.VCI_OpenDevice(CAN.DEV_USBCAN2, 0, 0); if (retcode != CAN.STATUS_OK) { MessageBox.Show("Couldn't open CAN device, error " + retcode.ToString()); return; } CAN.VCI_INIT_CONFIG canCFG; canCFG.AccMask = 0xFFFFFFFF; canCFG.AccCode = 0x0F000000; canCFG.InitFlag = 0; canCFG.Filter = 1; //Receive all frames canCFG.Timing0 = 0; canCFG.Timing1 = 0x14; canCFG.Mode = 0; //Normal mode retcode = CAN.VCI_InitCAN(CAN.DEV_USBCAN2, 0, 0, ref canCFG); if (retcode != CAN.STATUS_OK) { MessageBox.Show("Couldn't Init CAN, error " + retcode.ToString()); return; } retcode = CAN.VCI_StartCAN(CAN.DEV_USBCAN2, 0, 0); if (retcode != CAN.STATUS_OK) { MessageBox.Show("Couldn't start CAN, error " + retcode.ToString()); return; } bc.ctl.open(); CANConnected = true; cmdConnect.Text = "Disconnect"; } }
public bool sendData(byte[] buffer, uint ID, bool remote, bool extended) { //Allocate enough frames for the data buffer length int bufCount = (buffer.Length - 1) / 8 + 1; CAN.VCI_CAN_OBJ[] buf = allocateCanFrameBuffer(bufCount); int txSize = buffer.Length; for (int i = 0; i < bufCount; i++) { buf[i].ID = ID; buf[i].TimeFlag = 0; buf[i].SendType = 0; buf[i].RemoteFlag = remote ? (byte)1 : (byte)0; buf[i].ExternFlag = extended ? (byte)1 : (byte)0; buf[i].DataLen = (byte)(txSize > 8 ? 8 : txSize); //Transmit full packets unless the amount of data calls for a final packet that's smaller //Copy the data for (int j = 0; j < buf[i].DataLen; j++) { buf[i].Data[j] = buffer[i * 8 + j]; } txSize -= 8; } CAN.VCI_CAN_OBJ[] tempBuf = allocateCanFrameBuffer(1); for (int i = 0; i < bufCount; i++) { tempBuf[0] = buf[i]; System.Threading.Thread.Sleep(5); UInt32 retval = CAN.VCI_Transmit(CAN.DEV_USBCAN2, 0, 0, tempBuf, (uint)1 /*bufCount*/); if (retval != CAN.STATUS_OK) { return(false); } } //else return(true); }
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); }
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; } }
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); } }
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); }