CPU卡的大小 8K,资源大小不能大于 4095字节
800E000000【删除MF】
80E02001072800FFF00EFF80【建立基本信息文件】4095
00A4000002200100【选择建立基本信息文件】
全局变量 // 线程池
ExecutorService exec = Executors.newCachedThreadPool();
// 只能1个线程同时访问
final Semaphore semp = new Semaphore(1);
case R.id.cpu_write:
String pathgg=Environment.getExternalStorageDirectory()+"/KHDZ_Device/photo/P000002_image.jpg";long fileLength = 0;//文件长度
File flie = new File(pathgg);
if (!flie.isDirectory() && flie.exists()) {
fileLength = flie.length();
} else {//file not exists()
fileLength = 0;
}
byte[] imgArray=Utils.readData(pathgg,fileLength);
Log.e("获取"+"=="+imgArray.length, "=="+"");
long len= imgArray.length;
double pi =len/200;//
String nn=new DecimalFormat("0000").format(pi);
long ff= Long.parseLong(nn);//一次写200字节,写图片的长度数据,算出要 写的次数
long yu=len%200;
// Log.e(nn+"获取图片模板"+ff+"==", "=="+"");
synchronized(this){
//循环写入
for(int a=1;a<=(ff+1);a++){//ff+1写求余数据
MyRunnable myTest = new MyRunnable(a,ff,yu,imgArray);
Thread thread = new Thread(myTest);
thread.start();
exec.execute(myTest);
}
}
// 退出线程池
// exec.shutdown();
break;
public class MyRunnable implements Runnable {
byte[] imgArray ;
int a=0;
long ff=0;//一次写200字节,写图片的长度数据,算出要 写的次数
long yu=0;
public MyRunnable(int a,long ff, long yu,byte[] imgArray) {
this.a=a;
this.ff=ff;
this.yu=yu;
this.imgArray=imgArray;
}
@Override
public void run() {
try {
// 获取许可
semp.acquire();
Intent sendToservice = new Intent(Ridi.this,DeviceService.class); //用于发送指令
int offset=(200*(a-1));
String offsetHexString=Integer.toHexString(offset);//偏移量
String cmdHead="";//根据偏移量改变P1P2命令头
//Log.e("偏移量"+offsetHexString+"==", offsetHexString+"=="+"");
if(offsetHexString.equals("")|| offsetHexString==null){
cmdHead="00D60000C8";
}
if(offsetHexString.length()==1){
cmdHead="00D6000"+offsetHexString+"C8";
}else if(offsetHexString.length()==2){
cmdHead="00D600"+offsetHexString+"C8";
}else if(offsetHexString.length()==3){
cmdHead="00D60"+offsetHexString+"C8";
}else{
cmdHead="00D6"+offsetHexString+"C8";
}
// Log.e("偏移量44=="+cmdHead+"==", "=="+"");
//拷贝命令+数据到数组中。写出图片长度除以200的整数次数。
byte[] cmdArray=null;
if(a!=(ff+1)){
// byte[] ridits =new byte[200] ;
// System.arraycopy(imgArray, 200*(a-1), ridits,0, 200);//每次获取200
cmdArray=Utils.copyArray(cmdHead,a,imgArray);
//写出
// synchronized (this) {
cmd_flag = 42;
byte[] mycmd = psam.ucpu_send_cmd(cmdArray);
sendToservice.putExtra("cmd", mycmd); //封装指令
Ridi.this.startService(sendToservice); //发送指令到服务
Log.e("发送Accessing: " +a , "=="+Tools.Bytes2HexString(cmdArray, cmdArray.length));
Thread.sleep( 6000);//
// }
}else{//ff+1写求余数据
String lastDataLength=Integer.toHexString((int)yu);//最后一次要写的数据长度
if(lastDataLength.length()==1){
lastDataLength="0"+lastDataLength;
}
cmdHead=cmdHead.replaceAll("C8", lastDataLength);
cmdArray=Utils.copyArrayYu(cmdHead.length(),a,yu,imgArray);
byte[] testArray = Utils.zuArrayYu(cmdHead,cmdArray);
Log.e( Tools.Bytes2HexString(testArray, testArray.length)+"ff+1拷贝"+cmdHead, yu+"=="+""+cmdArray.length);
//写出
// synchronized (this) {
cmd_flag = 42;
byte[] mycmd = psam.ucpu_send_cmd(testArray);
sendToservice.putExtra("cmd", mycmd); //封装指令
Ridi.this.startService(sendToservice); //发送指令到服务
Log.e(a+"==发送Accessing: " +testArray.length +"=="+ Tools.Bytes2HexString(testArray, testArray.length), "=="+"");
Thread.sleep( 6000);
}
try {
exec.shutdownNow();// 调用这个方法后,立即停止执行者的运行。返回待执行的Task。
} catch (RejectedExecutionException e) {
e.printStackTrace();
}
//}
// cmd_flag = 42;
// byte[] mycmd = psam.ucpu_send_cmd(cmdArray);
// sendToservice.putExtra("cmd", mycmd); //封装指令
// Ridi.this.startService(sendToservice); //发送指令到服务
// Log.e("33发送Accessing: " +a +"=="+ Tools.Bytes2HexString(cmdArray, cmdArray.length), "=="+"");
// Thread.sleep( 6000);//
// 收到串口返回9000写成功释放
// semp.release();
//availablePermits()指的是当前信号灯库中有多少个可以被使用
// Log.e("-----------------" + semp.availablePermits(), "=="+"");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// 广播接收者,用于接收串口 服务返回的数据,并更新UI
private class MyBroadcast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String receivedata = intent.getStringExtra("result"); // 服务返回的数据
if (receivedata != null) {
byte []receive_buffer = Tools.HexString2Bytes(receivedata);
//Log.e("发送CMD返回数据:"+cmd_flag+"==" + " receivedata", receivedata);
switch (cmd_flag) {
case 42:
byte [] receiver_buffer_DF = Tools.HexString2Bytes(receivedata);
byte []receiver_data_DF = psam.resolveDataFromDevice(receiver_buffer_DF);
if(receiver_data_DF != null){
String response=Tools.Bytes2HexString(receiver_data_DF, receiver_data_DF.length);
if(response.equals("9000")){
semp.release();//重要 使用信号量 定义信号发送,当收到串口返回数据=9000时,释放信号。进行下一次的循环。
}
}
break;
}
}
}
package test;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
public class Utils {
private static byte[] readImage(InputStream input)throws Exception {
byte imagebyte[]=null;
byte buffer[]=new byte[1024];
ByteArrayOutputStream out=new ByteArrayOutputStream();
while(input.read(buffer)!=-1){
out.write(buffer, 0, buffer.length);
}
out.close();
input.close();
imagebyte=out.toByteArray();
return imagebyte;
}
public static byte[] readData(String path,long fileLength){
byte[] imagebyte = null;
File file = new File(path);
if (!file.isDirectory() && file.exists()) {
try {
InputStream in = new FileInputStream(file);
try {
imagebyte=readImage(in);
} catch (Exception e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
return imagebyte;
}
//十六进制转byte
public static byte[] HexString2Bytes(String src) {
int len = src.length() / 2;
byte[] ret = new byte[len];
byte[] tmp = src.getBytes();
for (int i = 0; i < len; i++) {
ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
}
return ret;
}
public static byte uniteBytes(byte src0, byte src1) {
byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
_b0 = (byte)(_b0 << 4);
byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
byte ret = (byte)(_b0 ^ _b1);
return ret;
}
public static byte[] copyArray(String P,int i,byte[] imgArray) {
byte[] targets =HexString2Bytes(P);
// 循环拷贝200字节
byte[] command = new byte[targets.length +200];//
System.arraycopy(targets, 0, command, 0, targets.length);
// System.arraycopy(imgArray, imgArray.length-(200*i), command, targets.length, 200);
//拷贝imgArray 每次开始位置为imgArray.length-(200*i)
System.arraycopy(imgArray, 200*(i-1), command, targets.length, 200);
return command;
}
// System.arraycopy(src, srcPos, dst, dstPos, length);
// src:源数组;
// srcPos:源数组要复制的起始位置;
// dest:目的数组;
// destPos:目的数组放置的起始位置;
// length:复制的长度。
public static byte[] copyArrayYu(long len, int i,long yu, byte[] imgArray) {
// int[] fun ={0,1,2,3,4,5,6};
// System.arraycopy(fun,0,fun,3,3);
// 结果为:{0,1,2,0,1,2,6};
// long len= targets.length;
byte[] yuArray = new byte[(int)yu];
System.arraycopy(imgArray, (int) (imgArray.length-yu), yuArray,0, (int)yu);
//意思是:拷贝imgArray中的数据 ,从第(imgArray.length-yu)开始拷贝,到目标数组yuArray,从标记位0开始,拷贝到xx个长度结束。
return yuArray;
}
public static byte[] zuArrayYu(String P,byte[] randomNumber ) {//External certification
byte[] targets =HexString2Bytes(P);
byte[] externalAuthCommand = new byte[targets.length + randomNumber.length];
System.arraycopy(targets, 0, externalAuthCommand, 0, targets.length);
System.arraycopy(randomNumber, 0, externalAuthCommand, targets.length, randomNumber.length);
return externalAuthCommand;
}
}