qtagger USB통신
This commit is contained in:
@@ -115,7 +115,6 @@ public class RfidFragment extends Fragment {
|
||||
|
||||
@Override
|
||||
public void onReaderReadTag(ATRfidReader atRfidReader, String s, float v, float v1) {
|
||||
//테스트할수있어야함함
|
||||
textViewRfidText.setText(s);
|
||||
}
|
||||
@Override
|
||||
|
||||
@@ -3,28 +3,208 @@ package com.qtagger.Activity;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.ServiceConnection;
|
||||
import android.hardware.usb.UsbDevice;
|
||||
import android.hardware.usb.UsbDeviceConnection;
|
||||
import android.hardware.usb.UsbManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.IBinder;
|
||||
import android.text.Editable;
|
||||
import android.text.Spannable;
|
||||
import android.text.SpannableStringBuilder;
|
||||
import android.text.style.ForegroundColorSpan;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.google.android.material.tabs.TabLayout;
|
||||
import com.hoho.android.usbserial.driver.UsbSerialDriver;
|
||||
import com.hoho.android.usbserial.driver.UsbSerialPort;
|
||||
import com.hoho.android.usbserial.driver.UsbSerialProber;
|
||||
import com.qtagger.Activity.Test.CaptureFragment;
|
||||
import com.qtagger.Activity.Test.FillFragment;
|
||||
import com.qtagger.Activity.Test.InfoFragment;
|
||||
import com.qtagger.Activity.Test.PhotographFragment;
|
||||
import com.qtagger.Activity.Test.WeightFragment;
|
||||
import com.qtagger.Data.CaptureFragmentEnum;
|
||||
import com.qtagger.Data.CustomProber;
|
||||
import com.qtagger.Data.TestEnum;
|
||||
import com.qtagger.Database.ORIGIN_CHRCT;
|
||||
import com.qtagger.R;
|
||||
import com.qtagger.util.CommonHelper;
|
||||
import com.qtagger.util.Constants;
|
||||
import com.qtagger.util.DataManager;
|
||||
import com.qtagger.util.SerialListener;
|
||||
import com.qtagger.util.SerialService;
|
||||
import com.qtagger.util.SerialSocket;
|
||||
import com.qtagger.util.TextUtil;
|
||||
|
||||
public class TestActivity extends AppCompatActivity {
|
||||
private enum Connected { False, Pending, True }
|
||||
private Connected connected = Connected.False;
|
||||
SerialService service;
|
||||
boolean isService = false;
|
||||
private boolean hexEnabled = true;
|
||||
public boolean modeCheck = false;
|
||||
public boolean stopFlag = true;
|
||||
private UsbSerialPort usbSerialPort;
|
||||
public String resultStr = "";
|
||||
|
||||
ServiceConnection conn = new ServiceConnection() {
|
||||
public void onServiceConnected(ComponentName name,
|
||||
IBinder binder) {
|
||||
// 서비스와 연결되었을 때 호출되는 메서드
|
||||
// 서비스 객체를 전역변수로 저장
|
||||
service = ((SerialService.SerialBinder)binder).getService();
|
||||
service.attach(mCallback);
|
||||
//new Thread(()->{connect(null);}).start();
|
||||
TestActivity.this.runOnUiThread(TestActivity.this::connect);
|
||||
// 서비스쪽 객체를 전달받을수 있슴
|
||||
isService = true;
|
||||
}
|
||||
public void onServiceDisconnected(ComponentName name) {
|
||||
// 서비스와 연결이 끊겼을 때 호출되는 메서드
|
||||
isService = false;
|
||||
}
|
||||
};
|
||||
SerialListener mCallback = new SerialListener() {
|
||||
@Override
|
||||
public void onSerialConnect() {
|
||||
Log.d("연결", "연결성공");
|
||||
connected = Connected.True;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSerialConnectError(Exception e) {
|
||||
Log.d("연결", "연결실패");
|
||||
connected = Connected.False;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSerialRead(byte[] data) {
|
||||
receive(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSerialIoError(Exception e) {
|
||||
Log.d("연결", "연결 끊김");
|
||||
connected = Connected.False;
|
||||
}
|
||||
};
|
||||
|
||||
private void connect()
|
||||
{
|
||||
connect(null);
|
||||
}
|
||||
|
||||
|
||||
private void connect(Boolean permissionGranted) {
|
||||
int portNum = 0;
|
||||
UsbDevice device = null;
|
||||
UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
|
||||
UsbSerialProber usbDefaultProber = UsbSerialProber.getDefaultProber();
|
||||
UsbSerialProber usbCustomProber = CustomProber.getCustomProber();
|
||||
for(UsbDevice v : usbManager.getDeviceList().values()) {
|
||||
UsbSerialDriver sDriver = usbDefaultProber.probeDevice(v);
|
||||
if(sDriver == null) {
|
||||
sDriver = usbCustomProber.probeDevice(v);
|
||||
}
|
||||
if(sDriver != null){
|
||||
for(int port = 0; port < sDriver.getPorts().size(); port++)
|
||||
{
|
||||
if (v.getProductName().equals("FT232R USB UART")) {
|
||||
portNum = port;
|
||||
device = v;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if(device == null) {
|
||||
return;
|
||||
}
|
||||
UsbSerialDriver driver = UsbSerialProber.getDefaultProber().probeDevice(device);
|
||||
if(driver == null) {
|
||||
driver = CustomProber.getCustomProber().probeDevice(device);
|
||||
}
|
||||
if(driver == null) {
|
||||
return;
|
||||
}
|
||||
if(driver.getPorts().size() < portNum) {
|
||||
return;
|
||||
}
|
||||
usbSerialPort = driver.getPorts().get(portNum);
|
||||
UsbDeviceConnection usbConnection = usbManager.openDevice(driver.getDevice());
|
||||
if(usbConnection == null && permissionGranted == null && !usbManager.hasPermission(driver.getDevice())) {
|
||||
PendingIntent usbPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(Constants.INTENT_ACTION_GRANT_USB), 0);
|
||||
usbManager.requestPermission(driver.getDevice(), usbPermissionIntent);
|
||||
return;
|
||||
}
|
||||
if(usbConnection == null) {
|
||||
if (!usbManager.hasPermission(driver.getDevice()))
|
||||
Log.d("connect ","connection failed: permission denied");
|
||||
else
|
||||
Log.d("connect ","connection failed: open failed");
|
||||
return;
|
||||
}
|
||||
|
||||
connected = Connected.Pending;
|
||||
try {
|
||||
usbSerialPort.open(usbConnection);
|
||||
usbSerialPort.setParameters(CommonHelper.QTagger_Usb_baudRate, UsbSerialPort.DATABITS_8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE);
|
||||
SerialSocket socket = new SerialSocket(this.getApplicationContext(), usbConnection, usbSerialPort);
|
||||
|
||||
service.connect(socket);
|
||||
|
||||
connected = Connected.True;
|
||||
} catch (Exception e) {
|
||||
Log.d("error",e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void receive(byte[] data) {
|
||||
if(hexEnabled) {
|
||||
if(resultStr.length() > 0) {
|
||||
resultStr += " ";
|
||||
}
|
||||
resultStr += TextUtil.toHexString(data);
|
||||
|
||||
if(modeCheck) {
|
||||
String tempStr = resultStr.replaceAll(" ", "");
|
||||
Log.i("jjhTest : tempStr!!!!!" , tempStr.length() + "");
|
||||
if(tempStr.length() == 72) {
|
||||
String value = tempStr.substring(42, 50);
|
||||
Long i = Long.parseLong(value, 16);
|
||||
Float f = Float.intBitsToFloat(i.intValue());
|
||||
double df = f;
|
||||
df = df / 10;
|
||||
Log.i("jjhTest!!!!!" , String.format("%.5f", df) + " μSv/hr");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
} else {
|
||||
String msg = new String(data);
|
||||
}
|
||||
}
|
||||
|
||||
Fragment surfaceFragment, m1Fragment, weightFragment, fillFragment, pictureFragment, infoFragment;
|
||||
TextView textViewRadiation,textView_TestEnum,textView_TestEnum2;
|
||||
TabLayout tabs;
|
||||
DataManager dataManager = DataManager.getInstance();
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@@ -46,6 +226,7 @@ public class TestActivity extends AppCompatActivity {
|
||||
finish();
|
||||
}
|
||||
});
|
||||
bindService(new Intent(this, SerialService.class), conn, Context.BIND_AUTO_CREATE);
|
||||
|
||||
|
||||
|
||||
@@ -112,6 +293,111 @@ public class TestActivity extends AppCompatActivity {
|
||||
}
|
||||
});
|
||||
|
||||
Thread detectThread = new Thread() {
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
if(stopFlag)
|
||||
{
|
||||
stopFlag = false;
|
||||
modeCheck = false;
|
||||
|
||||
int count = 0;
|
||||
{
|
||||
RunnableExample r = new RunnableExample("AA 00 0E 00 40 FF 00 00 00 02 01 5D FF AA", count++);
|
||||
Thread th = new Thread(r);
|
||||
th.start();
|
||||
}
|
||||
|
||||
{
|
||||
RunnableExample r = new RunnableExample("AA 00 0A 00 40 84 00 00 04 84", count++);
|
||||
Thread th = new Thread(r);
|
||||
th.start();
|
||||
}
|
||||
|
||||
{
|
||||
RunnableExample r = new RunnableExample("AA 00 0E 00 40 84 00 00 00 02 03 5D FB 27", count++);
|
||||
Thread th = new Thread(r);
|
||||
th.start();
|
||||
}
|
||||
|
||||
{
|
||||
RunnableExample r = new RunnableExample("AA 00 0E 00 40 84 00 00 00 02 01 5D FE 26", count++);
|
||||
Thread th = new Thread(r);
|
||||
th.start();
|
||||
}
|
||||
|
||||
{
|
||||
RunnableExample r = new RunnableExample("AA 00 0E 00 40 84 00 00 00 02 42 5D 6A 79", count++);
|
||||
Thread th = new Thread(r);
|
||||
th.start();
|
||||
}
|
||||
|
||||
{
|
||||
RunnableExample r = new RunnableExample("AA 00 0E 00 40 84 00 00 00 02 04 5D D1 50", count++);
|
||||
Thread th = new Thread(r);
|
||||
th.start();
|
||||
}
|
||||
|
||||
{
|
||||
RunnableExample r = new RunnableExample("AA 00 0E 00 40 84 00 00 00 02 01 5D FE 26", count++);
|
||||
Thread th = new Thread(r);
|
||||
th.start();
|
||||
}
|
||||
|
||||
{
|
||||
RunnableExample r = new RunnableExample("AA 00 0E 00 40 84 00 00 00 02 42 5D 6A 79", count++);
|
||||
Thread th = new Thread(r);
|
||||
th.start();
|
||||
}
|
||||
|
||||
{
|
||||
RunnableExample r = new RunnableExample("AA 00 0E 00 40 84 00 00 00 02 04 5D D1 50", count++);
|
||||
Thread th = new Thread(r);
|
||||
th.start();
|
||||
}
|
||||
|
||||
{
|
||||
RunnableExample r = new RunnableExample("AA 00 0E 00 40 84 00 00 00 02 0A 5C 20 FC", count++);
|
||||
Thread th = new Thread(r);
|
||||
th.start();
|
||||
}
|
||||
|
||||
{
|
||||
RunnableExample r = new RunnableExample("AA 00 0E 00 40 84 00 00 16 02 14 4F 00 09", count++);
|
||||
Thread th = new Thread(r);
|
||||
th.start();
|
||||
}
|
||||
|
||||
{
|
||||
RunnableExample r = new RunnableExample("AA 00 0E 00 40 84 00 00 00 02 16 5A 00 12", count++);
|
||||
Thread th = new Thread(r);
|
||||
th.start();
|
||||
}
|
||||
|
||||
int finalCount = count;
|
||||
Thread newThread = new Thread(() -> {
|
||||
try {
|
||||
recursiveCall(finalCount);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
newThread.start();
|
||||
}
|
||||
else
|
||||
{
|
||||
// detect_button.setText("Detect");
|
||||
//value_button.setText("");
|
||||
stopFlag = true;
|
||||
}
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
detectThread.start();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -150,16 +436,134 @@ public class TestActivity extends AppCompatActivity {
|
||||
textView_TestEnum2.setText("예비검사");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if(dataManager.testTabPosition != -1)
|
||||
{
|
||||
tabs.getTabAt(dataManager.testTabPosition).select();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
}
|
||||
|
||||
class RunnableExample implements Runnable
|
||||
{
|
||||
public String code = "";
|
||||
public Integer idx = 0;
|
||||
public RunnableExample(String code, Integer idx)
|
||||
{
|
||||
this.code = code;
|
||||
this.idx = idx;
|
||||
}
|
||||
|
||||
public void run()
|
||||
{
|
||||
try {
|
||||
Thread.sleep(300 * this.idx);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
//run() 추상메서드에 작업내용 구현
|
||||
send(this.code);
|
||||
threadSleep();
|
||||
}
|
||||
}
|
||||
|
||||
class RunnableExample2 implements Runnable
|
||||
{
|
||||
public String code = "";
|
||||
|
||||
public RunnableExample2(String code)
|
||||
{
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public void run()
|
||||
{
|
||||
send(this.code);
|
||||
}
|
||||
}
|
||||
|
||||
private void send(String str) {
|
||||
if(connected != Connected.True) {
|
||||
//Toast.makeText(getContext(), "not connected", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
String msg;
|
||||
byte[] data;
|
||||
if(hexEnabled) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
TextUtil.toHexString(sb, TextUtil.fromHexString(str));
|
||||
//TextUtil.toHexString(sb, newline.getBytes());
|
||||
msg = sb.toString();
|
||||
data = TextUtil.fromHexString(msg);
|
||||
} else {
|
||||
msg = str;
|
||||
data = (str).getBytes();
|
||||
}
|
||||
resultStr = "";
|
||||
|
||||
SpannableStringBuilder spn = new SpannableStringBuilder('\n' + msg+'\n');
|
||||
spn.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorSendText)), 0, spn.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
//receiveText.append(spn);
|
||||
service.write(data);
|
||||
} catch (Exception e) {
|
||||
service.onSerialIoError(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void threadSleep()
|
||||
{
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void recursiveCall(Integer idx) throws InterruptedException {
|
||||
if(idx == 0) {
|
||||
try {
|
||||
Thread.sleep(1000 * 1);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
try {
|
||||
Thread.sleep(300 * (idx + 1));
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
modeCheck = true;
|
||||
|
||||
RunnableExample2 r = new RunnableExample2("AA 00 0A 00 40 84 00 00 09 7F");
|
||||
Thread th = new Thread(r);
|
||||
th.start();
|
||||
|
||||
if(stopFlag == false) {
|
||||
Thread newThread = new Thread(() -> {
|
||||
try {
|
||||
recursiveCall(0);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
newThread.start();
|
||||
}
|
||||
else
|
||||
{
|
||||
modeCheck = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ public final class TextUtil {
|
||||
public final static String newline_crlf = "\r\n";
|
||||
public final static String newline_lf = "\n";
|
||||
|
||||
static byte[] fromHexString(final CharSequence s) {
|
||||
public static byte[] fromHexString(final CharSequence s) {
|
||||
ByteArrayOutputStream buf = new ByteArrayOutputStream();
|
||||
byte b = 0;
|
||||
int nibble = 0;
|
||||
@@ -50,7 +50,7 @@ public final class TextUtil {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
static void toHexString(StringBuilder sb, final byte[] buf) {
|
||||
public static void toHexString(StringBuilder sb, final byte[] buf) {
|
||||
toHexString(sb, buf, 0, buf.length);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,4 +8,5 @@
|
||||
<color name="black">#FF000000</color>
|
||||
<color name="white">#FFFFFFFF</color>
|
||||
<color name="colorPrimary">#d84315</color>
|
||||
<color name="colorSendText">#82CAFF</color>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user