public CanOpenStatus setIO(int index, bool value) { byte ioByte = output_mirror_values[index / 8]; byte bit = 0x1; int shifts = index % 8; ioByte = (byte)(bit << (byte)shifts); if (value == true) { output_mirror_values[index / 8] |= ioByte; } else { byte test = (byte)~ioByte; output_mirror_values[index / 8] &= (byte)~ioByte; } transmit_pdo.setup((uint)0x200 + this.node_id, this.output_mirror_values, (byte)(this.digital_outputs / 8)); return(transmit_pdo.transmit()); }
static void Main(string[] args) { Console.WriteLine("******************************************************"); Console.WriteLine("* CONNECT TWO CAN-ADAPTERS TO A SILENT CANBUS *"); Console.WriteLine("* AND THE TWO ADAPTERS WILL DO CANOPEN COMMUNICATION *"); Console.WriteLine("* HIT RETURN 1 TIME WHEN DONE! *"); Console.WriteLine("******************************************************"); Console.ReadLine(); CANopenLibraryConfiguration.SetAdapter(0); // qqq can't make CanAdapter enum visible here :( byte[] test; uint uint_test; uint uint_temp_val; ushort ushort_temp_val; SyncProducer_NET sync_producer = new SyncProducer_NET(); if (sync_producer.canHardwareConnect(0, 500000) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("Could not connect to CAN-adapter 0"); Console.ReadLine(); } if (sync_producer.setSyncCOBID(0x80) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("Could not setSyncCOBID"); Console.ReadLine(); } if (sync_producer.setTransmissionPeriodTime(1000) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("Could not setTransmissionPeriodTime"); Console.ReadLine(); } if (sync_producer.startPeriodicTransmission(true) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("Could not setTransmissionPeriodTime"); Console.ReadLine(); } Thread.Sleep(10000); if (sync_producer.canHardwareDisconnect() != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("Could not setTransmissionPeriodTime"); Console.ReadLine(); } sync_producer.Dispose(); sync_producer = null; LSS_Slave_NET lss_slave = new LSS_Slave_NET(); LSS_Master_NET lss_master = new LSS_Master_NET(); if (lss_slave.canHardwareConnect(0, 250000) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("Could not connect to CAN-adapter 1"); Console.ReadLine(); } if (lss_master.canHardwareConnect(1, 250000) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("Could not connect to CAN-adapter 2"); Console.ReadLine(); } lss_slave.setDeviceParameters(0x1234, 0x2345, 0x3456, 0x4567); if (lss_master.switchModeGlobal(0) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("switchModeGlobal failed"); Console.ReadLine(); } if (lss_master.switchModeSelectiveVendorId(0x1234) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("switchModeGlobal failed"); Console.ReadLine(); } if (lss_master.switchModeSelectiveProductCode(0x2345) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("switchModeSelectiveProductCode failed"); Console.ReadLine(); } if (lss_master.switchModeSelectiveRevisionNumber(0x3456) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("switchModeSelectiveRevisionNumber failed"); Console.ReadLine(); } if (lss_master.switchModeSelectiveSerialNumber(0x4567) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("switchModeSelectiveSerialNumber failed"); Console.ReadLine(); } byte errorCode = 0xff; byte specError = 0xff; if (lss_master.configureNodeId(5, out errorCode, out specError) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("switchModeSelectiveSerialNumber failed"); Console.ReadLine(); } if (lss_master.configureBitTimingParamteres(5, 7, out errorCode, out specError) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("configureBitTimingParamteres failed"); Console.ReadLine(); } if (lss_master.activateBitTimingParameters(25) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("activateBitTimingParameters failed"); Console.ReadLine(); } Thread.Sleep(25 + 25); // The activate call does not wait, leaving devices 25ms to stop and 25ms time to start with new rate if (lss_master.storeConfiguration(out errorCode, out specError) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("storeConfiguration failed"); Console.ReadLine(); } CanOpenStatus stat; test = new byte[10]; CanMonitor_NET can_Monitor; can_Monitor = new CanMonitor_NET(); Console.WriteLine("**************************************************"); Console.WriteLine("* Press RETURN to see some NMT protocol commands *"); Console.WriteLine("**************************************************"); Console.ReadLine(); ServerSDO_NET server_SDO; server_SDO = new ServerSDO_NET(); ClientSDO_NET client_SDO; client_SDO = new ClientSDO_NET(); NMT_Master_NET nmt_Master; nmt_Master = new NMT_Master_NET(); NMT_Slave_NET nmt_Slave1; nmt_Slave1 = new NMT_Slave_NET(); NMT_Slave_NET nmt_Slave2; nmt_Slave2 = new NMT_Slave_NET(); NMT_Slave_NET nmt_Slave3; nmt_Slave3 = new NMT_Slave_NET(); can_Monitor.registerCanReceiveCallback((Object)can_Monitor, canMonitorCallback); if (can_Monitor.canHardwareConnect(1, 250000) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("Could not connect to CAN-adapter 1"); Console.ReadLine(); } nmt_Slave1.nodeSetId(3); nmt_Slave1.canHardwareConnect(1, 250000); nmt_Slave2.nodeSetId(5); nmt_Slave2.canHardwareConnect(1, 250000); nmt_Slave3.nodeSetId(66); nmt_Slave3.canHardwareConnect(1, 250000); if (nmt_Master.canHardwareConnect(0, 250000) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("Could not connect to CAN-adapter 0"); Console.ReadLine(); } nmt_Master.registerNodeStateCallback(new NMTOperationalStateDelegate(node_state_callback), (Object)nmt_Master); nmt_Master.nodeGuardPollStart(3, 3000); nmt_Master.nodeGuardPollStart(5, 3000); nmt_Master.nodeGuardPollStart(66, 3000); Thread.Sleep(2000); nmt_Master.nodeGuardPollStop(3); // Stopping the nodeguarding service to avoid too much Console-prints. nmt_Master.nodeGuardPollStop(5); nmt_Master.nodeGuardPollStop(66); Thread.Sleep(2000); Console.WriteLine("************************************************"); Console.WriteLine("* Press return do EMERGENCY RX/TX *"); Console.WriteLine("************************************************"); Console.ReadLine(); EmcyClient_NET emcyClient = new EmcyClient_NET(); emcyClient.canHardwareConnect(0, 250000); EmcyServer_NET emcyServer = new EmcyServer_NET(); emcyServer.canHardwareConnect(1, 250000); /* * public delegate CanOpenStatus EmcyServerDelegate( * object obj, byte nodeId, ushort emcyErrorCode, byte errorRegister, * byte[] manufacturerSpecificErrorField) */ emcyServer.registerEmcyServerMessageCallBack((object)emcyServer, emcy_server_callback); emcyClient.nodeSetId(5); emcyClient.sendEmcyMessage(0x1234, 0x10, new byte[] { 1, 2, 3, 4, 5 }); Thread.Sleep(1000); /* * public delegate CanOpenStatus ReceivePdoDelegate(object obj, uint id, byte[] data, byte len) */ Thread.Sleep(2000); Console.WriteLine("************************************************"); Console.WriteLine("* Press return do PDO RX/TX *"); Console.WriteLine("************************************************"); Console.ReadLine(); ReceivePDO_NET rPdo = new ReceivePDO_NET(); rPdo.canHardwareConnect(1, 250000); rPdo.setCobid(0x123); rPdo.registerReceivePdoMessageCallBack((object)rPdo, receive_pdo_callback); TransmitPDO_NET tPdo = new TransmitPDO_NET(); tPdo.canHardwareConnect(1, 250000); tPdo.setup(0x123, new byte[] { 1, 2, 3, 4, 5 }, 5); tPdo.periodicTransmission(true); Thread.Sleep(5000); tPdo.periodicTransmission(false); client_SDO.setReadObjectTimeout(10000); client_SDO.setWriteObjectTimeout(40000); client_SDO.setNodeResponseTimeout(10000); stat = client_SDO.canHardwareConnect(0, 250000); stat = client_SDO.connect(3); stat = server_SDO.canHardwareConnect(1, 250000); stat = server_SDO.nodeSetId(3); /*** * Configure the callbacks. ***/ server_SDO.registerObjectReadCallback(new SrvReadDelegate(canopenReadCallback), (Object)server_SDO); server_SDO.registerObjectWriteCallback(new SrvWriteDelegate(canopenWriteCallback), (Object)server_SDO); server_SDO.registerObjectGetAttributesCallback(new SrvGetAttrDelegate(getAttributesCallback), (Object)server_SDO); Thread.Sleep(2000); Console.WriteLine("************************************************"); Console.WriteLine("* Press return do EXPEITED READ (test 1/2)! *"); Console.WriteLine("************************************************"); Console.ReadLine(); client_SDO.setReadObjectTimeout(500000); client_SDO.setNodeResponseTimeout(10000); uint bytesWrittenByRemoteNode; stat = client_SDO.objectRead(0x0A, 0x0A, out uint_temp_val, out bytesWrittenByRemoteNode); if (stat == CanOpenStatus.CANOPEN_OK) { if (uint_temp_val != 0x40302010 && bytesWrittenByRemoteNode != 4) { Console.WriteLine("ERROR!"); Console.ReadLine(); return; } } Thread.Sleep(2000); Console.WriteLine("************************************************"); Console.WriteLine("* Press return do EXPEITED READ (test 2/2)! *"); Console.WriteLine("************************************************"); Console.ReadLine(); client_SDO.setReadObjectTimeout(2000); // 2 seconds. stat = client_SDO.objectRead(0xB0, 0xB0, out ushort_temp_val, out uint_test); if (stat != CanOpenStatus.CANOPEN_BUFFER_TOO_SMALL) { Console.WriteLine("ERROR!"); Console.ReadLine(); } else { Console.WriteLine("Yes, this is correct, we try to read 40 bytes into 4 bytes!"); Console.WriteLine("Let's try with a bigger receive-buffer!"); } Console.WriteLine("Waiting for SDO server to time out..."); Thread.Sleep(7000); Console.WriteLine("************************************************"); Console.WriteLine("* Press return do SEGEMNETED READ (40 bytes)! *"); Console.WriteLine("************************************************"); Console.ReadLine(); uint responseErrorCode; byte[] rx_buffer = new byte[2000]; stat = client_SDO.objectRead(0xb0, 0xb0, rx_buffer, out bytesWrittenByRemoteNode, out responseErrorCode); if (stat != CanOpenStatus.CANOPEN_OK && bytesWrittenByRemoteNode != 40) { Console.WriteLine("ERROR!"); Console.ReadLine(); return; } else { string received_data = string.Empty; Console.WriteLine(String.Format("Just received {0} bytes of data from remote node!", bytesWrittenByRemoteNode)); for (int i = 0; i < bytesWrittenByRemoteNode; i++) { received_data += String.Format("{0:X2} ", rx_buffer[i]); } Console.WriteLine(received_data); } Thread.Sleep(2000); Console.WriteLine("************************************************"); Console.WriteLine("* Press return do SEGEMNETED READ (1024 bytes)! *"); Console.WriteLine("************************************************"); Console.ReadLine(); stat = client_SDO.objectRead(0xc0, 0xc0, rx_buffer, out bytesWrittenByRemoteNode, out responseErrorCode); if (stat != CanOpenStatus.CANOPEN_OK && bytesWrittenByRemoteNode != 1024) { Console.WriteLine("ERROR!"); Console.ReadLine(); return; } else { string received_data = string.Empty; Console.WriteLine(String.Format("Just received {0} bytes of data from remote node!", bytesWrittenByRemoteNode)); for (int i = 0; i < bytesWrittenByRemoteNode; i++) { received_data += String.Format("{0:X2} ", rx_buffer[i]); } Console.WriteLine(received_data); } Console.WriteLine("**********************************************************************"); Console.WriteLine("* Press return do EXPEDITED WRITE TEST WITH EXPECTED ERROR RECEIVED! *"); Console.WriteLine("**********************************************************************"); Console.ReadLine(); responseErrorCode = 0; if ((client_SDO.objectWrite(0xD0, 0xD0, (byte)0x55, out responseErrorCode)) == CanOpenStatus.CANOPEN_REMOTE_NODE_ABORT) { Console.WriteLine(String.Format("Expected remote errorcode received, no write access to that object! {0:X4}", responseErrorCode)); } Console.WriteLine("***************************************************"); Console.WriteLine("* Press return do EXPEDITED WRITE TEST (1 bytes)! *"); Console.WriteLine("***************************************************"); Console.ReadLine(); if ((client_SDO.objectWrite(0xE0, 0x00, (byte)0x55, out responseErrorCode)) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("ERROR!"); Console.ReadLine(); return; } Console.WriteLine("***************************************************"); Console.WriteLine("* Press return do EXPEDITED WRITE TEST (2 bytes)! *"); Console.WriteLine("***************************************************"); Console.ReadLine(); if ((client_SDO.objectWrite(0xE0, 0x00, (ushort)0x1234, out responseErrorCode)) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("ERROR!"); Console.ReadLine(); return; } Console.WriteLine("***************************************************"); Console.WriteLine("* Press return do EXPEDITED WRITE TEST (4 bytes)! *"); Console.WriteLine("***************************************************"); Console.ReadLine(); if ((client_SDO.objectWrite(0xE0, 0x00, (uint)0x12345678, out responseErrorCode)) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("ERROR!"); Console.ReadLine(); return; } Console.WriteLine("*****************************************************"); Console.WriteLine("* Press return do SEGMENTED WRITE TEST (100 bytes)! *"); Console.WriteLine("*****************************************************"); Console.ReadLine(); byte[] tx_buffer = new byte[2000]; for (int i = 0; i < tx_buffer.Length; i++) { tx_buffer[i] = (byte)i; } if ((stat = client_SDO.objectWrite(0xE0, 0x00, tx_buffer, (ushort)100, out responseErrorCode)) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("ERROR!"); Console.ReadLine(); return; } Console.WriteLine("******************************************************"); Console.WriteLine("* Press return do SEGMENTED WRITE TEST (10000 bytes)! *"); Console.WriteLine("******************************************************"); Console.ReadLine(); tx_buffer = new byte[10000]; for (int i = 0; i < tx_buffer.Length; i++) { tx_buffer[i] = (byte)i; } if ((stat = client_SDO.objectWrite(0xE0, 0x00, tx_buffer, (ushort)10000, out responseErrorCode)) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("ERROR!"); Console.ReadLine(); return; } Console.WriteLine("******************************************************"); Console.WriteLine("* Press return do BLOCK WRITE TEST (10000 bytes)! *"); Console.WriteLine("******************************************************"); Console.ReadLine(); tx_buffer = new byte[10000]; for (int i = 0; i < tx_buffer.Length; i++) { tx_buffer[i] = (byte)i; } if ((stat = client_SDO.objectWriteBlock(0xE0, 0x00, 0, tx_buffer, (ushort)10000, out responseErrorCode)) != CanOpenStatus.CANOPEN_OK) { Console.WriteLine("ERROR!"); Console.ReadLine(); return; } }