luxiaolongloveMin 于 2016.09.08 16:37 提问
- android 串口通信读取数据,处理问题?求大神帮忙
-
public class MainActivity extends Activity {
/****************************************/ String rxIdCode = ""; String tag = "serial test"; private int i = 0; private EditText ET1; private Button RECV; private Button SEND; private Button IBS; private Button IPCS; private Button SBZ; //serial com3 = new serial(); //serial com2 = new serial(); serial com1 = new serial(); //serial com0 = new serial(); /****************************************/ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /********************************************/ ET1 = (EditText)findViewById(R.id.edit1); RECV = (Button)findViewById(R.id.recv1); SEND = (Button)findViewById(R.id.send1);//制动测量 IBS = (Button)findViewById(R.id.button1);//标定状态 IPCS = (Button)findViewById(R.id.button2);//坡度测量 SBZ = (Button)findViewById(R.id.button3);//取零 //com3.Open(3, 115200); //com2.Open(2, 115200); com1.Open(1, 19200); //com0.Open(0, 19200); RECV.setOnClickListener(new manager()); SEND.setOnClickListener(new manager()); IBS.setOnClickListener(new manager()); IPCS.setOnClickListener(new manager()); SBZ.setOnClickListener(new manager()); timer.schedule(task, 1000, 800); // 1s后执行task,经过1s再次执行 /*********************************************/ } Handler handler = new Handler() { public void handleMessage(Message msg) { if (msg.what == 1) { Log.d(tag,"recv start ..."); int[] RX = com1.Read(); if(RX == null) { Log.d(tag,"recv nothing ..."); return; } else { Log.d(tag,RX.toString()); ET1.append(new String(RX, 0, RX.length)); } //ET1.setText(Integer.toString(i++)); } super.handleMessage(msg); }; }; Timer timer = new Timer(); TimerTask task = new TimerTask() { @Override public void run() { // 需要做的事:发送消息 Message message = new Message(); message.what = 1; handler.sendMessage(message); } }; /***********************************************/ class manager implements OnClickListener{ public void onClick(View v) { String rxIdCode = ""; String str; int i; switch (v.getId()) { //recvive 接收
// case R.id.recv1://
//
// Log.d(tag,"recv start ...");
//
// int[] RX = com1.Read();
//
// if(RX == null)
// {
// Log.d(tag,"recv nothing ...");
// return;
// }
// else
// {
// Log.d(tag,RX.toString());
// ET1.append(new String(RX, 0, RX.length));
// }
//
// break;//send case R.id.send1://制动测量 /*Log.d(tag," start ..."); CharSequence tx = ET1.getText(); int[] text = new int[tx.length()]; for (i=0; i<tx.length(); i++) { text[i] = tx.charAt(i); } com1.Write(text, tx.length()); ET1.setText("");*/ Log.d(tag,"into zhidong celiang status"); int[] text = new int[6]; text[0] = 0x2a; text[1] = 0x41; text[2] = 0x44; text[3] = 0x31; text[4] = 0xe0; text[5] = 0x0d; com1.Write(text, text.length); break; case R.id.button1://标定状态 Log.d(tag,"into biaoding status"); int[] text1 = new int[6]; text1[0] = 0x2a; text1[1] = 0x41; text1[2] = 0x44; text1[3] = 0x33; text1[4] = 0xe2; text1[5] = 0x0d; com1.Write(text1, text1.length); break; case R.id.button2://坡度测量 Log.d(tag,"into podu celiang status"); int[] text2 = new int[6]; text2[0] = 0x2a; text2[1] = 0x41; text2[2] = 0x44; text2[3] = 0x32; text2[4] = 0xe1; text2[5] = 0x0d; com1.Write(text2, text2.length); break; case R.id.button3://取零 Log.d(tag,"send biaoding number"); int[] text3 = new int[6]; /* * 0x2a:*号 * 0x47:大写字母G * 0x5a:大写字母Z * 0xcb:校验码 * 0x0d:回车键 */ text3[0] = 0x2a; text3[1] = 0x47; text3[2] = 0x5a; text3[3] = 0xcb; text3[4] = 0x0d; com1.Write(text3, text3.length); break; } } } /***********************************************/ @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } static { System.loadLibrary("serialtest"); }
}
com1.Write(text1, text1.length);读取到的数据类似*F&11.111&22.222&333.333&44.444&55.555&66.666&777.777
怎么把这个数据赋值到:
MFDD:11.111m/s2
制动协调时间:22.222s
行驶距离:333.333m
行驶时间:44.444s
最大加速度:55.555m/s2
平均减速度:66.666m/s2
起始速度:777.777km/h
-
- qq_32929105 2016.09.08 16:42
利用string.spilt('&'),可以讲该字符串转为string[],根据下标取出对应的值
-
- qq_32929105 回复luxiaolongloveMin: 可以在你获取到数据的时候。。。
- 一年多之前 回复
-
- luxiaolongloveMin 那应该在哪里添这句代码呢?
- 一年多之前 回复