|
@@ -59,6 +59,7 @@ import java.io.OutputStream;
|
|
|
import java.net.ServerSocket;
|
|
|
import java.net.Socket;
|
|
|
import java.nio.charset.Charset;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
//===============================================================//
|
|
|
//=================================================================//
|
|
@@ -80,7 +81,7 @@ public class GT9MainActivity extends AppCompatActivity
|
|
|
private UsbEndpoint mUsbEndpointIn;
|
|
|
private boolean mToggle = true;
|
|
|
private boolean isDetached = false;
|
|
|
- private byte[] mBytes = new byte[1024];
|
|
|
+ private byte[] mBytes = new byte[1024 * 8 + 32];
|
|
|
private boolean isReceiverMessage = true;
|
|
|
private boolean isNeedFindDevice = true;
|
|
|
|
|
@@ -110,6 +111,8 @@ public class GT9MainActivity extends AppCompatActivity
|
|
|
private OutputStream outputStream = null;
|
|
|
private InputStream inputStream = null;
|
|
|
|
|
|
+ private byte[] SyncCmd = new byte[] {0x03, 0x00, 0x13, (byte)0x81, 0x4E, 0x00, 0x01};
|
|
|
+
|
|
|
Handler mHandler = new Handler() {
|
|
|
@Override
|
|
|
public void handleMessage(Message msg) {
|
|
@@ -426,8 +429,9 @@ public class GT9MainActivity extends AppCompatActivity
|
|
|
socket = server.accept();
|
|
|
// 建立好连接后,从socket中获取输入流,并建立缓冲区进行读取
|
|
|
inputStream = socket.getInputStream();
|
|
|
+ outputStream = socket.getOutputStream();
|
|
|
byte[] headbytes = new byte[8];
|
|
|
- byte[] databytes = new byte[512];
|
|
|
+ byte[] databytes = new byte[1024*6];
|
|
|
int len;
|
|
|
int datalen = 0;
|
|
|
int framelen = 0;
|
|
@@ -440,7 +444,7 @@ public class GT9MainActivity extends AppCompatActivity
|
|
|
//}
|
|
|
|
|
|
//只有当客户端关闭它的输出流的时候,服务端才能取得结尾的-1
|
|
|
- while ((len = inputStream.read(headbytes)) != -1) {
|
|
|
+ while ((len = inputStream.read(headbytes, 0, 8)) != -1) {
|
|
|
|
|
|
//先判断头部字节是否正确
|
|
|
if ( headbytes[0] == (byte)0x8d
|
|
@@ -448,34 +452,46 @@ public class GT9MainActivity extends AppCompatActivity
|
|
|
&& headbytes[2] == 0x6b
|
|
|
&& headbytes[3] == 0x5a )
|
|
|
{
|
|
|
- framelen = headbytes[4] + (int)(headbytes[5] * 256); //低位在前
|
|
|
- frameid = headbytes[6] + (int)(headbytes[7] * 256);
|
|
|
-
|
|
|
- byte[] bytes = new byte[framelen];
|
|
|
|
|
|
- len = inputStream.read(bytes); //根据头部数据读取对应长度的数据
|
|
|
- datalen = bytes[5]+ (int)(bytes[4] * 256); //高位在前
|
|
|
+ framelen = (headbytes[4] & 0xff) + (int) ((headbytes[5] & 0xff) * 256); //低位在前
|
|
|
+ frameid = (headbytes[6] & 0xff) + (int) ((headbytes[7] & 0xff) * 256);
|
|
|
|
|
|
- GetUSBData(bytes[1],bytes[2],bytes[3],databytes,datalen); //根据命令去获取USB数据
|
|
|
|
|
|
- //bytes[4] = 24;
|
|
|
- //bytes[5] = 0;
|
|
|
+ int offset = 0;
|
|
|
|
|
|
-
|
|
|
- for(int i = 0; i < 8 + datalen; i++)
|
|
|
- {
|
|
|
- if (i < 8) //把前面8个字节的头部补上
|
|
|
+ do{
|
|
|
+ // byte[] bytes = new byte[framelen];
|
|
|
+ len = inputStream.read(mBytes, offset, framelen - offset);
|
|
|
+ if(len==-1)
|
|
|
{
|
|
|
- mBytes[i] = headbytes[i];
|
|
|
+ break;
|
|
|
}
|
|
|
- else{
|
|
|
+ offset+=len;
|
|
|
+ } while(offset < framelen);
|
|
|
+
|
|
|
+ if(offset!=framelen)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ datalen = (mBytes[5] & 0xFF) + (int)(mBytes[4] * 256); //高位在前
|
|
|
|
|
|
- mBytes[i] = databytes[i];
|
|
|
+ if(mBytes[0] == 1){
|
|
|
+ SetUSBData(mBytes, datalen);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ if(GetUSBData(mBytes,databytes,datalen)) {//根据命令去获取USB数据
|
|
|
+ databytes[0] = (byte)0x8d;
|
|
|
+ databytes[1] = 0x7c;
|
|
|
+ databytes[2] = 0x6b;
|
|
|
+ databytes[3] = 0x5a;
|
|
|
+ databytes[4] = (byte)datalen;
|
|
|
+ databytes[5] = (byte)(datalen>>8);
|
|
|
+ databytes[6] = (byte)frameid;
|
|
|
+ databytes[7] = (byte)(frameid>>8);
|
|
|
+ outputStream.write(databytes,0, datalen + 8);
|
|
|
}
|
|
|
}
|
|
|
- outputStream = socket.getOutputStream();
|
|
|
- //outputStream.write("Hello Client,I get the message.".getBytes("UTF-8"));
|
|
|
- outputStream.write(mBytes);
|
|
|
}
|
|
|
// 注意指定编码格式,发送方和接收方一定要统一,建议使用UTF-
|
|
|
/*
|
|
@@ -578,7 +594,7 @@ public class GT9MainActivity extends AppCompatActivity
|
|
|
|
|
|
registerReceiver(mOpenDevicesReceiver, intentFilter);
|
|
|
|
|
|
- while(true)
|
|
|
+ // while(true)
|
|
|
{
|
|
|
isNeedFindDevice = true;
|
|
|
|
|
@@ -609,9 +625,7 @@ public class GT9MainActivity extends AppCompatActivity
|
|
|
break;
|
|
|
|
|
|
} // if (productId == 0x0818 && vendorId == 0x27C0)
|
|
|
- else{
|
|
|
|
|
|
- }
|
|
|
} // for (UsbDevice usbDevice : deviceList.values())
|
|
|
|
|
|
if(isS818Device==false){
|
|
@@ -805,78 +819,117 @@ public class GT9MainActivity extends AppCompatActivity
|
|
|
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
|
|
|
|
|
|
};
|
|
|
- //发送命令,使得S818可以通过IIC向GT9的固定地址读取任意bytes的数据
|
|
|
- private boolean GetUSBData(byte cmd,byte addressHigh,byte addressLow,byte[] wBuffer,int length) {
|
|
|
|
|
|
- int i;
|
|
|
+ private boolean SetUSBData(byte[]wBuffer, int length) {
|
|
|
+ int address = (wBuffer[3] & 0xff) + (int)((wBuffer[2] & 0xff) * 256);//(wBuffer[3]<<8) + (wBuffer[4]&0xff);
|
|
|
|
|
|
- iicByteData[1] = 0x00; //
|
|
|
- iicByteData[2] = cmd; //get report id
|
|
|
- iicByteData[3] = addressHigh;
|
|
|
- iicByteData[4] = addressLow;
|
|
|
- iicByteData[5] = (byte)(length >> 8); //lengthHigh
|
|
|
- iicByteData[6] = (byte)(length & 0x00FF); //lengthLow
|
|
|
+ int sendTimes = length/57 + 1; //预计是向下取整,所以+1
|
|
|
|
|
|
- for (int j = 7; j < 64; j++) {
|
|
|
- iicByteData[j] = 0x00;
|
|
|
- }
|
|
|
-
|
|
|
- //int address1 = ((addressHigh&0xff)<<8);
|
|
|
- //int address2 = addressLow&0xff;
|
|
|
- //int address = address1|address2;
|
|
|
+ int offset = 0;
|
|
|
|
|
|
- int sendTimes = length/63 + 1; //预计是向下取整,所以+1
|
|
|
+ for(int j=1; j <= 4; j++) //命令和首地址
|
|
|
+ {
|
|
|
+ iicWriteData[j] = wBuffer[j-1];
|
|
|
+ }
|
|
|
+ iicWriteData[0] = 0x03;
|
|
|
+ iicWriteData[5] = 0;
|
|
|
|
|
|
for (int k = 0; k < sendTimes; k++) {
|
|
|
- int finalOffset = k*63; //每次只能发57bytes数据
|
|
|
- int offset = k*64;
|
|
|
+ int finalOffset = offset + k*57; //每次只能发57bytes数据
|
|
|
|
|
|
- if(length - (finalOffset ) < 63) {
|
|
|
+ if(length - (finalOffset -offset) < 57) {
|
|
|
|
|
|
- iicByteData[6] = (byte) (length - (finalOffset)); //length
|
|
|
+ iicWriteData[6] = (byte) (length - (finalOffset -offset)); //length
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- iicByteData[6] = 63;
|
|
|
+ iicWriteData[6] = 0x39;
|
|
|
}
|
|
|
|
|
|
- //发送读取命令
|
|
|
- i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointOut, iicByteData, 0, 0x40, 100);
|
|
|
- if (i != 0x40)
|
|
|
- return false; //传输失败
|
|
|
- // SystemClock.sleep(1);
|
|
|
-
|
|
|
- isReceiverMessage = false; //先关闭循环接收信息
|
|
|
-
|
|
|
- //接收数据
|
|
|
- i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointIn, wBuffer, offset, 0x40, 3000);
|
|
|
+ for (int j = 7; j < 64; j++) {
|
|
|
+ iicWriteData[j] = wBuffer[finalOffset + j - 1]; //copy 数据
|
|
|
+ }
|
|
|
+ int i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointOut, iicWriteData, 0, 0x40, 100);
|
|
|
|
|
|
if (i != 0x40) {
|
|
|
- // mError.setText("iicWrite Failed");
|
|
|
+ // mError.setText("iicWrite Failed");
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
- //address = address + 63;
|
|
|
- //iicByteData[3] = (byte)(address>>8);
|
|
|
- //iicByteData[4] = (byte)address;
|
|
|
+ address = address + 57;
|
|
|
+ iicWriteData[3] = (byte)(address>>8);
|
|
|
+ iicWriteData[4] = (byte)address;
|
|
|
}
|
|
|
|
|
|
- //将report id从数组中去除
|
|
|
- //byte[] temperBuffer =new byte[length];
|
|
|
- /*
|
|
|
- if(true) {
|
|
|
- int count1 = 0;
|
|
|
- for (int k = 0; k < length; k++) {
|
|
|
- if (k % 63 == 0) {
|
|
|
- count1++;
|
|
|
+ return true;
|
|
|
+ };
|
|
|
+
|
|
|
+ //发送命令,使得S818可以通过IIC向GT9的固定地址读取任意bytes的数据
|
|
|
+ private boolean GetUSBData(byte[]cmd, byte[] wBuffer,int length) {
|
|
|
+
|
|
|
+ int i;
|
|
|
+
|
|
|
+ do
|
|
|
+ {
|
|
|
+ i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointIn, wBuffer, 0, 512, 3);//清空接收缓冲区
|
|
|
+ }while(i!=-1);
|
|
|
+
|
|
|
+ if(cmd[1] == 13)//同步读取数据
|
|
|
+ {
|
|
|
+ int nTryTimes = 40;
|
|
|
+
|
|
|
+ do{
|
|
|
+ i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointOut, SyncCmd, 0, 7, 100);
|
|
|
+ if (i != 7)
|
|
|
+ return false; //传输失败
|
|
|
+
|
|
|
+ i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointIn, iicByteData, 0, 0x40, 100);
|
|
|
+
|
|
|
+ if (i != 0x40) {
|
|
|
+ return false;
|
|
|
}
|
|
|
+ if(((iicByteData[1]) &0x080)!=0)
|
|
|
+ {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ }while(--nTryTimes > 0);
|
|
|
+ }
|
|
|
|
|
|
- wBuffer[k] = wBuffer[k + count1];
|
|
|
|
|
|
- }
|
|
|
+ for(int j=1; j <= 6; j++)
|
|
|
+ {
|
|
|
+ iicByteData[j] = cmd[j-1];
|
|
|
}
|
|
|
- */
|
|
|
+ iicByteData[0] = 0x03;
|
|
|
+
|
|
|
+ //发送读取命令
|
|
|
+ i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointOut, iicByteData, 0, 0x40, 100);
|
|
|
+ if (i != 0x40)
|
|
|
+ return false; //传输失败
|
|
|
+ // SystemClock.sleep(1);
|
|
|
+
|
|
|
+ int offset = 0;
|
|
|
+
|
|
|
+ do{
|
|
|
+ //接收数据
|
|
|
+ i = mUsbDeviceConnection.bulkTransfer(mUsbEndpointIn, iicByteData, 0, 0x40, 100);
|
|
|
+
|
|
|
+ if (i != 0x40) {
|
|
|
+ // mError.setText("iicWrite Failed");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ for(int j = 1; j < i; j++)
|
|
|
+ {
|
|
|
+ wBuffer[offset + 7 + j] = iicByteData[j];
|
|
|
+ }
|
|
|
+
|
|
|
+ offset += i-1;
|
|
|
+
|
|
|
+ }while(offset < length);
|
|
|
+
|
|
|
return true;
|
|
|
}
|
|
|
}
|