# Java + Garmin SDK



## Castor_Troy (14. Februar 2008)

Hiho ich verwende Jsr80 als USB-API und würde nun gerne Daten hin/her verschicken zu/vom Garmin Gerät (etrex vista). 

Ich geb mal den Link zu dem C-Code und der Spezifikation - eventuell kann sich jemand das mal kurz ansehn und sagen ob ich alles richtig mache.


Hier währe mein Code - leider bekomme ich bei der Rückgabe vom Gerät nur Nullen  

Bin für jeden Tipp dankbar


```
public void IODevices(UsbDevice device){
		try{
			device.getActiveUsbConfiguration();
			UsbConfiguration config = device.getActiveUsbConfiguration();
			UsbInterface interf = config.getUsbInterface((byte)0);
			interf.claim(new UsbInterfacePolicy() {
				public boolean forceClaim(UsbInterface usbInterface) {
				        return true;
				}
			});
			List totalEndpoints = interf.getUsbEndpoints();
			UsbEndpoint ep = null;
//Endpoints
				UsbEndpoint pipeInEndP = (UsbEndpoint) totalEndpoints.get(0);
				UsbEndpoint pipeOutEndP = (UsbEndpoint) totalEndpoints.get(2);
				
				//Pipes
				UsbPipe pipeIn = pipeInEndP.getUsbPipe();
				pipeIn.open();
				UsbPipe pipeOut = pipeOutEndP.getUsbPipe();
				pipeOut.open();

short mReserved2 = 0;
				short mPacketId = 254;
				int mDataSize = 0;
				byte[] bytesToSend = new byte[13];
				bytesToSend[0] = 20;
				bytesToSend[1] = 0;
				bytesToSend[2] = (byte) (mReserved2 & 0xFF); //Short to Byte
				bytesToSend[3] = (byte) ((mReserved2 & 0xff00 ) >> 8); //Short to Byte
				
				bytesToSend[4] = (byte) (mPacketId & 0xFF); //Short to Byte
				bytesToSend[5] = (byte) ((mPacketId & 0xff00 ) >> 8); //Short to Byte
				
				bytesToSend[6] = (byte) (mReserved2 & 0xFF); //Short to Byte
				bytesToSend[7] = (byte) ((mReserved2 & 0xff00 ) >> 8); //Short to Byte
				
				bytesToSend[8] = (byte) (mDataSize >>24); //unsigned long to Byte
				bytesToSend[9] = (byte) ((mDataSize << 8) >> 24);
				bytesToSend[10] = (byte) ((mDataSize << 16) >> 24);
				bytesToSend[11] = (byte) ((mDataSize << 24) >> 24);
				bytesToSend[12] =' ';
				UsbIrp irpSend = pipeOut.createUsbIrp();
				irpSend.setData(bytesToSend);
				pipeOut.asyncSubmit(irpSend);
				irpSend.waitUntilComplete(1000);
				
				byte[] bytesToRead = new byte[255];
				UsbIrp irpRead = pipeIn.createUsbIrp();
				irpRead.setData(bytesToRead);
				pipeIn.asyncSubmit(irpRead);
				irpRead.waitUntilComplete(1000);
				pipeIn.abortAllSubmissions();
				pipeOut.abortAllSubmissions();
				pipeIn.close();
				pipeOut.close();
				interf.release();
				
				int pid,sv;
				

				pid =(bytesToRead[0] & 0xff);
				sv =(bytesToRead[1] & 0xff);
				
				pid= bytes_to_int(bytesToRead);

				sv= bytes_to_int(bytesToRead, 4);
System.out.println("pid: " + pid + "; sv: " + sv);
				
				System.out.println(bytesToRead);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		

			
	}

int bytes_to_int(byte b[], int start) {
		    /* return the int value whose MSB is in b[start + 3] and whose
		       LSB is in b[start]. */
		   int i = ((b[start + 3] & 0xff) << 24) + ((b[start + 2] & 0xff) << 16)
		     + ((b[start + 1] & 0xff) << 8) + (b[start] & 0xff);
		   return i;
}
		  /** Analagous to bytes_to_short.
		      @see decouto.garble.util.bytes_to_short */
int bytes_to_int(byte b[]) {
		    /* as above, but start == 0 */
		    return bytes_to_int(b, 0);
}
```


----------



## Castor_Troy (14. Februar 2008)

Hier ist der Link für das Garmin SDK : http://www8.garmin.com/support/pdf/IOSDK.zip


----------



## Castor_Troy (14. Februar 2008)

Hier nochmals der COde etwas schöner strukturiert


```
public void IODevices(UsbDevice device){
        try{
                device.getActiveUsbConfiguration();
                UsbConfiguration config = device.getActiveUsbConfiguration();
                UsbInterface interf = config.getUsbInterface((byte)0);
                interf.claim(new UsbInterfacePolicy() {
                        public boolean forceClaim(UsbInterface usbInterface) {
                                return true;
                        }
                });
                List totalEndpoints = interf.getUsbEndpoints();
                UsbEndpoint ep = null;
//Endpoints
                UsbEndpoint pipeInEndP = (UsbEndpoint) totalEndpoints.get(0);
                UsbEndpoint pipeOutEndP = (UsbEndpoint) totalEndpoints.get(2);
                                
                //Pipes
                UsbPipe pipeIn = pipeInEndP.getUsbPipe();
                pipeIn.open();
                UsbPipe pipeOut = pipeOutEndP.getUsbPipe();
                pipeOut.open();

                short mReserved2 = 0;
                short mPacketId = 254;
                int mDataSize = 0;
                byte[] bytesToSend = new byte[12];
                bytesToSend[0] = 20;
                bytesToSend[1] = 0;
                bytesToSend[2] = (byte) (mReserved2 & 0xFF); //Short to Byte
                bytesToSend[3] = (byte) ((mReserved2 & 0xff00 ) >> 8); //Short to Byte
                bytesToSend[4] = (byte) (mPacketId & 0xFF); //Short to Byte
                bytesToSend[5] = (byte) ((mPacketId & 0xff00 ) >> 8); //Short to Byte
                bytesToSend[6] = (byte) (mReserved2 & 0xFF); //Short to Byte
                bytesToSend[7] = (byte) ((mReserved2 & 0xff00 ) >> 8); //Short to Byte
                bytesToSend[8] = (byte) (mDataSize >>24); //unsigned long to Byte
                BytesToSend[9] = (byte) ((mDataSize << 8) >> 24);
                BytesToSend[10] = (byte) ((mDataSize << 16) >> 24);
                bytesToSend[11] = (byte) ((mDataSize << 24) >> 24);
                                
                UsbIrp irpSend = pipeOut.createUsbIrp();
                IrpSend.setData(bytesToSend);
                pipeOut.asyncSubmit(irpSend);
                irpSend.waitUntilComplete(1000);
                                
                byte[] bytesToRead = new byte[255];
                UsbIrp irpRead = pipeIn.createUsbIrp();
                irpRead.setData(bytesToRead);
                pipeIn.asyncSubmit(irpRead);
                irpRead.waitUntilComplete(1000);
                pipeIn.abortAllSubmissions();
                pipeOut.abortAllSubmissions();
                pipeIn.close();
                pipeOut.close();
                interf.release();
                                
                int pid,sv;
                pid =(bytesToRead[0] & 0xff);
                sv =(bytesToRead[1] & 0xff);
                pid= bytes_to_int(bytesToRead);
                sv= bytes_to_int(bytesToRead, 4);
                System.out.println("pid: " + pid + "; sv: " + sv);
                                
                System.out.println(bytesToRead);
        }catch(Exception ex){
                ex.printStackTrace();
        }               
}

int bytes_to_int(byte b[], int start) {
        int i = ((b[start + 3] & 0xff) << 24) + ((b[start + 2] & 0xff) << 16) +
((b[start + 1] & 0xff) << 8) + (b[start] & 0xff);
                   return i;
}
int bytes_to_int(byte b[]) {
                    /* as above, but start == 0 */
                    return bytes_to_int(b, 0);
}
```


----------

