Go to Post Think of the build season as kiddie bowling. Mentors should be the bumpers and your mom who lines you up and gives you advice. - Joe Matt [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 07-03-2012, 17:23
ProgrammerMatt ProgrammerMatt is offline
Programmer-Electrical-Mechanical
FRC #0228 (Gus)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2010
Location: Southington
Posts: 138
ProgrammerMatt is just really niceProgrammerMatt is just really niceProgrammerMatt is just really niceProgrammerMatt is just really nice
URGENT Sending data over tcp

Im trying to get my tracking code to work i can send a pwm value over tcp to robot with roborealm but it updates so slow even know on roborealm it is sending it at 50ms on the robot it takes about 500ms to register, here is the code, Roborealm sends it as an int, we have a competition tomorrow so ASAP would be great PLEASE AND THANKYOU

Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Gus.Team228.Final;

import edu.wpi.first.wpilibj.Timer;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.ServerSocketConnection;
import javax.microedition.io.SocketConnection;

/**
 *
 * @author Matt Smith
 */
public class Socket extends Thread {

    private ServerSocketConnection socket;
    private SocketConnection connection;
    DataInputStream instream;
    DataOutputStream outstream;
    int datain;

    public void run() {

        try {
            socket = (ServerSocketConnection) Connector.open("socket://:1234");

            connection = (SocketConnection) socket.acceptAndOpen();

            instream = connection.openDataInputStream();

            outstream = connection.openDataOutputStream();
            
            while (true) {
                datain = instream.read();

                instream.read();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

//Teleop code:
motor.setRaw(socket.datain);
Reply With Quote
  #2   Spotlight this post!  
Unread 09-03-2012, 11:00
derekwhite's Avatar
derekwhite derekwhite is offline
Java Virtual Machine Hacker
no team (FIRST@Oracle)
Team Role: Programmer
 
Join Date: May 2009
Rookie Year: 2009
Location: Burlington, MA
Posts: 127
derekwhite is on a distinguished road
Re: URGENT Sending data over tcp

Here are some things to check out:

1) Is RoboRealm flushing output at every int, or is getting buffered?

2) Why is "instream.read()" called 2x in your loop?

3) The field "Socket.datain" is being used to communicate between threads. For correct portable code this should be declared "volatile", or all reads and writes should be guarded by synchronized statements. This shouldn't actually matter for Java on the cRIO though.

4) There isn't any built-in Java-level input buffering on a Socket's input stream in this version of Java. That shouldn't result in a 10x slowdown though.

To test this you could try reading into a 4-byte array, and constructing an int from that

Code:
byte b[] = new byte[4];

while (true) {
    int len = instream.read(b, 0, b.length); // should check that len is 4, and buffer up until full, but in practice you should see 4 bytes...

    datain = (b[0] << 24) | ((b[1] & 0xFF) << 16) |((b[2] & 0xFF) << 8) | (b[1] & 0xFF);  // or reverse the byte order if necessary.
}
Reply With Quote
  #3   Spotlight this post!  
Unread 10-03-2012, 19:24
ProgrammerMatt ProgrammerMatt is offline
Programmer-Electrical-Mechanical
FRC #0228 (Gus)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2010
Location: Southington
Posts: 138
ProgrammerMatt is just really niceProgrammerMatt is just really niceProgrammerMatt is just really niceProgrammerMatt is just really nice
Re: URGENT Sending data over tcp

Quote:
Originally Posted by derekwhite View Post
Here are some things to check out:

1) Is RoboRealm flushing output at every int, or is getting buffered?

2) Why is "instream.read()" called 2x in your loop?

3) The field "Socket.datain" is being used to communicate between threads. For correct portable code this should be declared "volatile", or all reads and writes should be guarded by synchronized statements. This shouldn't actually matter for Java on the cRIO though.

4) There isn't any built-in Java-level input buffering on a Socket's input stream in this version of Java. That shouldn't result in a 10x slowdown though.

To test this you could try reading into a 4-byte array, and constructing an int from that

Code:
byte b[] = new byte[4];

while (true) {
    int len = instream.read(b, 0, b.length); // should check that len is 4, and buffer up until full, but in practice you should see 4 bytes...

    datain = (b[0] << 24) | ((b[1] & 0xFF) << 16) |((b[2] & 0xFF) << 8) | (b[1] & 0xFF);  // or reverse the byte order if necessary.
}
I will try this thanks, also is UDP a better choice for speed?
Sorry but i dont understand what int len is for?

P.S, Yea i just noticed that there is 2 instream.read();

Last edited by ProgrammerMatt : 10-03-2012 at 19:31.
Reply With Quote
  #4   Spotlight this post!  
Unread 11-03-2012, 14:25
Ginto8's Avatar
Ginto8 Ginto8 is offline
Programming Lead
AKA: Joe Doyle
FRC #2729 (Storm)
Team Role: Programmer
 
Join Date: Oct 2010
Rookie Year: 2010
Location: Marlton, NJ
Posts: 174
Ginto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of lightGinto8 is a glorious beacon of light
Re: URGENT Sending data over tcp

len is the number of bytes read by that specific call. Since there isn't always enough new data available to fill your buffer, read() lets you know exactly how much was there (or, more correctly, how much it copied into your buffer).

UDP is faster, but it's also a good bit riskier. TCP is nice because it guarantees that packets will arrive, that packets will not be corrupted, and that packets will arrive in the order they were sent. UDP guarantees nothing. However, since UDP doesn't have all those checks, it will be faster. Be careful using it, though, because you can no longer rely on any of the guarantees of TCP.
Reply With Quote
  #5   Spotlight this post!  
Unread 11-03-2012, 15:27
ProgrammerMatt ProgrammerMatt is offline
Programmer-Electrical-Mechanical
FRC #0228 (Gus)
Team Role: Mentor
 
Join Date: Jan 2011
Rookie Year: 2010
Location: Southington
Posts: 138
ProgrammerMatt is just really niceProgrammerMatt is just really niceProgrammerMatt is just really niceProgrammerMatt is just really nice
Re: URGENT Sending data over tcp

Quote:
Originally Posted by Ginto8 View Post
len is the number of bytes read by that specific call. Since there isn't always enough new data available to fill your buffer, read() lets you know exactly how much was there (or, more correctly, how much it copied into your buffer).

UDP is faster, but it's also a good bit riskier. TCP is nice because it guarantees that packets will arrive, that packets will not be corrupted, and that packets will arrive in the order they were sent. UDP guarantees nothing. However, since UDP doesn't have all those checks, it will be faster. Be careful using it, though, because you can no longer rely on any of the guarantees of TCP.
Is UDP supported? i dont think it is in this version of java
Reply With Quote
Reply


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:48.

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