Closed harryberlin closed 10 months ago
Hi,
i'm trying to send data from for loop, but during the loop the app crashes. by using Thread i get the same result.
i did lot of variants with Thread or Runnable, but evertime bad result. have no idea to solve this.
If i don't use Thread, i think the Serial Buffer overflows.
void btnClick() { new Thread(new Runnable() { public void run() { for (int i = 0; i < 60; i++) { sendHex((byte) 0xFB, (byte) 0xFA, new byte[]{ 0x08, //command (byte) i //adress }); Thread.sleep(100); } } }).start(); }
private void sendHex(byte src, byte dst, byte[] data) { String sData = ""; int sDataColor = R.color.colorSendText; byte[] ibus_msg = new byte[(data.length+4)]; ibus_msg[0] = src; //SRC ibus_msg[1] = (byte) (data.length+2); //LEN ibus_msg[2] = dst; //DST for (int i = 0; i < data.length; i++) { ibus_msg[i+3] = data[i]; //DATA } ibus_msg[ibus_msg.length-1] = ibus_msg[0]; //CK for (int i = 1; i < ibus_msg.length-1; i++) { ibus_msg[ibus_msg.length-1] ^= ibus_msg[i]; } try { if(connected != Connected.True) { Toast.makeText(getActivity(), "not connected", Toast.LENGTH_SHORT).show(); throw new Exception("not connected"); } service.write(ibus_msg); } catch (Exception e) { onSerialIoError(e); sDataColor = R.color.colorErrorText; } for (int i=0; i < ibus_msg.length; i++) { if (i!=0) sData += " "; sData += String.format("%02X", ibus_msg[i]); } _addText(sData, sDataColor); }
public void _addText(String text, int colorId) { SpannableStringBuilder spn = new SpannableStringBuilder(text + "\n"); spn.setSpan(new ForegroundColorSpan(getResources().getColor(colorId)), 0, spn.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); receiveText.append(spn); }
got solved the issue. needed to use a handler.
Hi,
i'm trying to send data from for loop, but during the loop the app crashes. by using Thread i get the same result.
i did lot of variants with Thread or Runnable, but evertime bad result. have no idea to solve this.
If i don't use Thread, i think the Serial Buffer overflows.