Go to Post FIRST is doing a great job, it's me who needs to do the hard work now. - Bomberofdoom [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Spotlight this post!  
Unread 02-09-2012, 00:38
DarthCoder DarthCoder is offline
Registered User
FRC #4309
 
Join Date: Aug 2012
Location: Washington
Posts: 37
DarthCoder is an unknown quantity at this point
TCP communication with cRIO

I've been trying to get TCP connections working with the cRIO. I have an Android app and robot code setup. I've gotten them to connect to each other, but every time I spawn a process it crashes the whole thing. This is my code, is there any options that I'm getting wrong?

App code:
Code:
public class BotSmithsAppActivity extends Activity 
{
	static final int PORT_NUMBER = 1500;
	static final String IP_ADDR = "10.43.9.2";
	
	static final int MOVE_ARM_UP = 0;
	static final int MOVE_ARM_DOWN = 1;
	
	//Inits socket
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //Arm up button
        final Button upButton = (Button)findViewById(R.id.ARM_UP);
        upButton.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v) 
            {
                sendAction(MOVE_ARM_UP);
            }
        });
        
        //Arm down button
        final Button downButton = (Button)findViewById(R.id.ARM_DOWN);
        downButton.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v) 
            {
                sendAction(MOVE_ARM_DOWN);
            }
        });
    }
    
    void sendAction(int actionType)
    {
    	try
        {
			Socket s = new Socket(IP_ADDR, PORT_NUMBER);
			OutputStream out = s.getOutputStream();
			
			out.write(actionType);
			
			s.close();
		} 
        catch (UnknownHostException e) 
		{
			e.printStackTrace();
		} 
        catch (IOException e) 
		{
			e.printStackTrace();
		}
    }
}
Robot code:
Code:
VOID tcpServerWorkTask(int sFd, char* address, u_short port);

#define MOVE_ARM_UP 0
#define MOVE_ARM_DOWN 1

class AppControl;

AppControl* g_appCtrl = NULL;

class AppControl : public IterativeRobot
{
public:
	
	static const unsigned int MAX_CONNECTIONS = 1;
	static const unsigned int SERVER_WORK_PRIORITY = 100;
	static const unsigned int SERVER_STACK_SIZE = 1000;
	
	sockaddr_in m_serverAddr;
	int m_sFd;
	
	char m_workName[16];
	
	std::auto_ptr<Jaguar> m_armMotor;
	
	AppControl(void)
		:m_disabled(false)
	{
		g_appCtrl = this;
		
		m_armMotor.reset(new Jaguar(3));
	}

	void TeleopInit()
	{
		m_disabled = false;
		
		int sockAddrSize = sizeof(struct sockaddr_in);
		
		//Fill out server address
		m_serverAddr.sin_family = AF_INET;
		m_serverAddr.sin_len = sockAddrSize;
		m_serverAddr.sin_port = 1500;
		m_serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
		
		//Init socket.
		if ((m_sFd = socket(AF_INET, SOCK_STREAM, 0)) == ERROR) 
		{ 
			perror("socket"); 
			printf("\n Error connecting to socket.\n\n");
			return;
		}
		printf("Initialized socket.\n");
		
		//Bind to socket
		if (bind(m_sFd, (struct sockaddr*)&m_serverAddr, sockAddrSize) == ERROR) 
		{ 
			perror("bind");
			close(m_sFd);
			printf("\n Error binding to socket.\n\n");
			return;
		}
		printf("Bound socket\n");
		
		//Set socket to listen for connections.
		if (listen(m_sFd, MAX_CONNECTIONS) == ERROR)
		{ 
			perror("listen");
			close(m_sFd);
			printf("\n Error setting socket to listen mode.\n\n");
			return;
		}
		printf("Listening on socket 1500\n");
		
		//Wait for connections
		FOREVER
		{
			if ( m_disabled )
			{
				break;
			}
			
			sockaddr_in clientAddr;
			int newFd = 0;
			
			//Accept connection from client
			if ((newFd = accept (m_sFd, (struct sockaddr*)&clientAddr, &sockAddrSize)) == ERROR) 
			{
				perror("accept");
				close(m_sFd);
				printf("\n Error accepting connection\n\n");
				return;
			}
			
			//Spawn work task
			printf("Spawning work task\n");
			if (taskSpawn(m_workName, SERVER_WORK_PRIORITY, 0, SERVER_STACK_SIZE,
			(FUNCPTR)tcpServerWorkTask, newFd,
			(int)inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port), 
			0, 0, 0, 0, 0, 0, 0) == ERROR)
			{ 
				perror("taskSpawn");
				printf("\nError spawning task closing new file descriptor\n\n");
				close(newFd);
			}
		}
		
		printf("closing server socket.\n");
		close(m_sFd);
	}
	
	void TeleopDisabled()
	{
		m_disabled = true;
	}
	
	bool m_disabled;
	
	friend VOID tcpServerWorkTask(int sFd, char* address, u_short port);
};

VOID tcpServerWorkTask(int sFd, char* address, u_short port)
{
	unsigned int i = 0;
	read(sFd, (char*)&i, sizeof(unsigned int));
	
	std::cout << "Data: " << i << "\n";
	std::cout << "Port: " << (int)port << "\n";
	std::cout << "Address: " << address << "\n";
 	
	if ( g_appCtrl != NULL )
	{
		if ( i == MOVE_ARM_UP )
		{
			printf("Arm up\n");
			//g_appCtrl->m_armMotor->Set(-0.5f);
		}
		else if ( i == MOVE_ARM_DOWN )
		{
			printf("Arm down\n");
			//g_appCtrl->m_armMotor->Set(0.5f);
		}
	}
	
	printf("freeing address\n");
	free(address);
	printf("Closing file descriptor.\n");
	close(sFd);
	printf("Closed file descriptor.\n");
}

START_ROBOT_CLASS(AppControl);
I left out includes and imports. I get some Task Exited error. The error code is 1114118.
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 09:41.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi