/**-------------------------------------------------------------------------------------------
// sends an SDO onto the bus
// description:
//      should send an canopen-SDO onto the can-bus. !!! this function is not implemented
//      correctly and not tested !!! to access the object directory of an other module, use
//      read/write dictionary entries instead!
//------------------------------------------------------------------------------------------*/
void CANFestivalGui::sendSDO( )
{
    int msgd;
    unsigned char n, e, s, t, c;
    unsigned char data[8];

    s_SDO sdo;
    sdo.ID = ui->canopenSDODeviceId->text( ).toInt( );
    // + 1 wegen dem Command Specifier...
    sdo.len = getLength( canopenSDODataList ) + 1;
    if( ui->canopenSDOReadWrite->isChecked( ) == true ) {
        msgd = Rx;
    } else {
        msgd = Tx;
    }

    n = 8 - (sdo.len-1);
    ui->canopenSDONormal->isChecked( ) ? e=0: e=1;
    s = 0; // !!!!!!!!!!! was isch das ueberhaupt?!
    t = 0; // !!!!!!!!!!! fix this...
    c = 1; // should also be changed !!!!!!!!!!!!

    if( ui->canopenSDOCommandSpecifier->currentText( ).compare( "Init Domain Download" ) == 0 ) {
        if( msgd == Tx ) {
            data[0] = IDD_client( n, e, s );
        } else {
            data[0] = IDD_server;
        }
    } else if( ui->canopenSDOCommandSpecifier->currentText( ).compare( "Download Domain Segment" ) == 0 ) {
        if( msgd == Tx ) {
            data[0] = DDS_client( t, n, c );
        } else {
            data[0] = DDS_server;
        }
    } else if( ui->canopenSDOCommandSpecifier->currentText( ).compare( "Init Domain Upload" ) == 0 ) {
        QMessageBox::information( this, "kdsfjsl", "lauft no nued..." );
    } else if( ui->canopenSDOCommandSpecifier->currentText( ).compare( "Upload Domain Segment" ) == 0 ) {
        QMessageBox::information( this, "kdsfjsl", "lauft no nued..." );
    } else if( ui->canopenSDOCommandSpecifier->currentText( ).compare( "Abort Domain Transfer" ) == 0 ) {
        data[0] = ADT_server;
    }
    for( int i=1; i<sdo.len-1; i++ ) {
        if( ui->canopenSDOHexFormat->isChecked( ) ) {
            data[i] = strtol( canopenSDODataList.at( i-1 )->text( ).toStdString().c_str(), NULL, 16 );
        } else if( ui->canopenSDOBinFormat->isChecked( ) ) {
            data[i] = strtol( canopenSDODataList.at( i-1 )->text( ).toStdString().c_str(), NULL, 2 );
        } else {
            data[i] = canopenSDODataList.at( i-1 )->text( ).toInt( );
        }
    }

    memcpy(&(sdo.body), &(data[0]), sdo.len);
    Send_SDO(0, sdo, msgd);
}
Esempio n. 2
0
UNS8 SDOmGR(UNS8 bus_id, UNS8 line) //Flux Manager
{
	UNS8 res;
	UNS8 i;
	UNS8 line_transfers;	
	UNS8 t, n, c, e, s;
	s_SDO sdo;

	MSG_WAR(0x3A11, "SDOmGR ", 0);
	res = 0xFF;
	sdo.nodeId = transfers[bus_id][line].nodeId ;

	if(TS_HAVE_TO_DO(transfers[bus_id][line].state)) 
	{
        switch(TS_ACTIVITY(transfers[bus_id][line].state)) 
		{   
			// Initiate a Domain (up/down)load ( first frame )   
		case TS_ACTIVATED:	
			MSG_WAR(0x3A12, "Initiate Domain ", 0);
   
			//memcpy(&sdo.body.data[0], &transfers[bus_id][line].index, 2);
			// This Memcpy depends on packing structure. Avoid
			
			sdo.body.data[0] = transfers[bus_id][line].index & 0xFF;        // LSB
			sdo.body.data[1] = (transfers[bus_id][line].index >> 8) & 0xFF; // MSB of index (16 b)
			sdo.body.data[2] = transfers[bus_id][line].subindex;
      
			if(TS_IS_DOWNLOAD(transfers[bus_id][line].state))
			{
				// Number of bytes to transfer < 5 -> expedited tranfer
				MSG_WAR(0x3A13, "Download ", 0);
				if(transfers[bus_id][line].count < 5)
				{
					n = 4 - transfers[bus_id][line].count;
					e = 1;
					s = 1;
					sdo.len = 8 - n;
					for(i=0; i<4-n; i++)
						sdo.body.data[i+3] = transfers[bus_id][line].data[i];
					// Next call will finish.
					transfers[bus_id][line].offset = transfers[bus_id][line].count; 
				} else 
				{ // Normal transfer
					n = 0;
					e = 0;
					s = 1;
					//the first byte of D containts the LSB of number of data to be 
					//download and the last byte of D contains the MSB
					sdo.body.data[3] = transfers[bus_id][line].count;	
					sdo.body.data[6] = 0;
					sdo.body.data[4] = 0;
					sdo.body.data[5] = 0;
					sdo.len = 8;
					transfers[bus_id][line].offset = 0;
				}
				sdo.body.SCS = IDD_client(n,e,s);
			} else 
			{	// initiate upload, expedited transfer
				MSG_WAR(0x3A14, "Upload ", 0);
				sdo.len = 4;
				sdo.body.SCS = IDU_client;
				transfers[bus_id][line].offset = 0;
			}
			res = sendSDO(bus_id, sdo);
			TS_SET_ACTIVITY(transfers[bus_id][line].state,TS_WORKING | TS_WAIT_SERVER);
			break;
			// Follow Domain (up/down)load 
			// ( following frames, if more that one is needed )	
		case TS_WORKING:	
			MSG_WAR(0x3A15, "Domain Segment ", 0);
      
			getSDOlineOnUse( bus_id,transfers[bus_id][line].nodeId, &line_transfers );
			line = line_transfers;

			if(TS_IS_DOWNLOAD(transfers[bus_id][line].state)) 
			{		       
				MSG_WAR(0x3A16, "Download ", 0);
				i = transfers[bus_id][line].count - transfers[bus_id][line].offset;    
        
				if(i <= 0)
				{ // Download Finished
					res = 0;
					transfers[bus_id][line].state = TS_FREE;
					resetSDOline( bus_id, line );
					break;					
				} 
				else 
				{ // Follow segmented transfer
					if(i>7) 
					{
						n = 0;		// There is no unused byte
						c = 0;		// It is not the last message		
					} 
					else 
					{
						n = 7 - i;	        // There could have unused bytes
						c = 1;	        // This is the last message
					}
					sdo.len = 8 - n;
					for(i=0; i<7-n; i++)
						sdo.body.data[i] =transfers[bus_id][line].data[transfers[bus_id][line].offset++];	
					// take the toggle bit	
					t = (transfers[bus_id][line].state & TS_TOGGLE) >> 4;    
					sdo.body.SCS = DDS_client(t,n,c);
					// toggle afterward
					transfers[bus_id][line].state ^= TS_TOGGLE;   
					if (c)
						resetSDOline( bus_id, line );
				}				
			} 
			else 
			{    // Upload			
				MSG_WAR(0x3A17, "Upload ", 0);
				
				//memcpy(&sdo.body.data[0], &transfers[bus_id][line].index, 2);
				// This Memcpy depends on packing structure. Avoid
        
				sdo.body.data[0] = transfers[bus_id][line].index & 0xFF;        // LSB
				sdo.body.data[1] = (transfers[bus_id][line].index >> 8) & 0xFF; // MSB of index (16 b)
				sdo.body.data[2] = transfers[bus_id][line].subindex;		
				sdo.len = 4;
				// take toggle bit
				t = (transfers[bus_id][line].state & TS_TOGGLE) >> 4;
				sdo.body.SCS = UDS_client(t);
				// toggle afterward
				transfers[bus_id][line].state ^= TS_TOGGLE;
			}  
      
			res = sendSDO(bus_id, sdo);
			TS_SET_ACTIVITY(transfers[bus_id][line].state,TS_WORKING | TS_WAIT_SERVER);
			break;
		default:	// Transfer not in use or transfer error. Blub blub...  
			resetSDOline( bus_id, line );
		break;
		}