Skip to content

Commit bd7d7ac

Browse files
UART: example for 3 UART.
Change-Id: Iba60fb71d70a3be9d85276a1350975ec0f3035da
1 parent 5ad4206 commit bd7d7ac

File tree

3 files changed

+187
-98
lines changed

3 files changed

+187
-98
lines changed

app/src/main/java/com/hardkernel/wiringpi/MainActivity.java

Lines changed: 76 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -174,37 +174,83 @@ public void run() {
174174
private boolean mStopSerial;
175175
private Button mBtn_WriteSerial;
176176
private EditText mET_Write;
177-
private ToggleButton mBtn_ReadSerial;
178-
private EditText mET_Read;
179-
private String mLine;
177+
private TextView mTV1_data;
178+
private TextView mTV2_data;
179+
private TextView mTV3_data;
180+
private long mUART1_timestamp;
181+
private long mUART2_timestamp;
182+
private long mUART3_timestamp;
183+
private TextView mTV1_timestamp;
184+
private TextView mTV2_timestamp;
185+
private TextView mTV3_timestamp;
186+
private String mData1;
187+
private String mData2;
188+
private String mData3;
189+
private final static String TTYS1 = "/dev/ttyS1";
190+
private final static String TTYS2 = "/dev/ttyS2";
191+
private final static String TTYS3 = "/dev/ttyS3";
192+
180193
Runnable mRunnableSerial = new Runnable() {
181194

182195
@Override
183196
public void run() {
184197
// TODO Auto-generated method stub
185198

186-
try {
187-
BufferedReader br = new BufferedReader(new FileReader(TTYSx));
188-
while (!mStopSerial) {
189-
while((mLine = br.readLine()) != null) {
190-
Log.e(TAG, mLine);
199+
while (true) {
200+
try {
201+
Log.e(TAG, "2{");
202+
BufferedReader br = new BufferedReader(new FileReader(TTYS2));
203+
while((mData1 = br.readLine()) != null) {
204+
Log.e(TAG, "data : " + mData1);
205+
mUART1_timestamp = System.currentTimeMillis();
206+
Log.e(TAG, "time : " + mUART1_timestamp);
191207
mHandler.sendEmptyMessage(0);
192208
}
209+
Log.e(TAG, "2}");
210+
211+
Log.e(TAG, "3{");
212+
br = new BufferedReader(new FileReader(TTYS3));
213+
while((mData2 = br.readLine()) != null) {
214+
mUART2_timestamp = System.currentTimeMillis();
215+
mHandler.sendEmptyMessage(1);
216+
}
217+
Log.e(TAG, "3}");
218+
219+
Log.e(TAG, "1{");
220+
br = new BufferedReader(new FileReader(TTYS1));
221+
while((mData3 = br.readLine()) != null) {
222+
mUART3_timestamp = System.currentTimeMillis();
223+
mHandler.sendEmptyMessage(2);
224+
}
225+
Log.e(TAG, "1}");
226+
227+
br.close();
228+
} catch (IOException e) {
229+
e.printStackTrace();
193230
}
194-
br.close();
195-
} catch (IOException e) {
196-
e.printStackTrace();
197231
}
198232
}
199233
};
200-
private final static String TTYSx = "/dev/ttyS1";
234+
201235
private Handler mHandler = new Handler() {
202236

203237
@Override
204238
public void handleMessage(Message msg) {
205239
// TODO Auto-generated method stub
206240
super.handleMessage(msg);
207-
mET_Read.setText(mLine);
241+
if (msg.what == 0) {
242+
Log.e(TAG, "1 : " + mData1);
243+
mTV1_data.setText(mData1);
244+
mTV1_timestamp.setText(String.valueOf(mUART1_timestamp));
245+
} else if (msg.what == 1) {
246+
Log.e(TAG, "2 : " + mData2);
247+
mTV2_data.setText(mData2);
248+
mTV2_timestamp.setText(String.valueOf(mUART2_timestamp));
249+
} else if (msg.what == 2) {
250+
Log.e(TAG, "3 : " + mData3);
251+
mTV3_data.setText(mData3);
252+
mTV3_timestamp.setText(String.valueOf(mUART3_timestamp));
253+
}
208254
}
209255
};
210256
//UART }}}
@@ -259,7 +305,6 @@ public void onTabChanged(String tabId) {
259305
mBtn_PWM.setChecked(false);
260306
mBtn_Weather.setChecked(false);
261307
mBtn_GPIO.setChecked(false);
262-
mBtn_ReadSerial.setChecked(false);
263308
mBtn_1Wire.setChecked(false);
264309
}
265310
});
@@ -498,6 +543,13 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
498543
//I2C }}}
499544

500545
//UART {{{
546+
setBaudRate();
547+
mTV1_data = (TextView) findViewById(R.id.tv1_data);
548+
mTV2_data = (TextView) findViewById(R.id.tv2_data);
549+
mTV3_data = (TextView) findViewById(R.id.tv3_data);
550+
mTV1_timestamp = (TextView) findViewById(R.id.tv1_timestamp);
551+
mTV2_timestamp = (TextView) findViewById(R.id.tv2_timestamp);
552+
mTV3_timestamp = (TextView) findViewById(R.id.tv3_timestamp);
501553
mET_Write = (EditText) findViewById(R.id.et_write);
502554
mET_Write.addTextChangedListener(new TextWatcher() {
503555

@@ -522,33 +574,21 @@ public void afterTextChanged(Editable s) {
522574
}
523575
});
524576

577+
new Thread(mRunnableSerial).start();
578+
525579
mBtn_WriteSerial = (Button) findViewById(R.id.btn_write_serial);
526580
mBtn_WriteSerial.setEnabled(false);
527581
mBtn_WriteSerial.setOnClickListener(new OnClickListener() {
528582

529583
@Override
530584
public void onClick(View v) {
531585
// TODO Auto-generated method stub
532-
writeSerial(mET_Write.getText().toString());
586+
writeSerial(mET_Write.getText().toString() + "\n");
533587
mET_Write.setText("");
588+
mStopSerial = false;
534589
}
535590
});
536591

537-
mET_Read = (EditText) findViewById(R.id.et_read);
538-
mBtn_ReadSerial = (ToggleButton) findViewById(R.id.btn_read_serial);
539-
mBtn_ReadSerial.setOnCheckedChangeListener(new OnCheckedChangeListener() {
540-
541-
@Override
542-
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
543-
// TODO Auto-generated method stub
544-
if (isChecked) {
545-
setBaudRate();
546-
mStopSerial = false;
547-
new Thread(mRunnableSerial).start();
548-
} else
549-
mStopSerial = true;
550-
}
551-
});
552592
//UART }}}
553593

554594
//1-Wire {{{
@@ -625,9 +665,6 @@ protected void onPause() {
625665
//I2C {{{
626666
mBtn_Weather.setChecked(false);
627667
//I2C }}}
628-
//UARD {{{
629-
mBtn_ReadSerial.setChecked(false);
630-
//UART }}}
631668
//1-Wire {{{
632669
mBtn_1Wire.setChecked(false);
633670
//1-Wire }}}
@@ -793,6 +830,10 @@ private void setBaudRate() {
793830
DataOutputStream os = new DataOutputStream(mProcess.getOutputStream());
794831
os.writeBytes("stty -F /dev/ttyS1 115200\n");
795832
os.flush();
833+
os.writeBytes("stty -F /dev/ttyS2 115200\n");
834+
os.flush();
835+
os.writeBytes("stty -F /dev/ttyS3 115200\n");
836+
os.flush();
796837
Thread.sleep(100);
797838
} catch (IOException e1) {
798839
// TODO Auto-generated catch block
@@ -805,7 +846,8 @@ private void setBaudRate() {
805846

806847
private void writeSerial(String data) {
807848
try {
808-
BufferedWriter bw = new BufferedWriter(new FileWriter(TTYSx));
849+
Log.e(TAG, "writeSerial(" + data + ")");
850+
BufferedWriter bw = new BufferedWriter(new FileWriter(TTYS1));
809851
bw.write(data);
810852
bw.close();
811853
} catch (IOException e) {

app/src/main/res/layout/activity_main.xml

Lines changed: 108 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,114 @@
1515
android:layout_width="match_parent"
1616
android:layout_height="match_parent" >
1717

18+
<LinearLayout
19+
android:id="@+id/tab4"
20+
android:layout_width="match_parent"
21+
android:layout_height="match_parent"
22+
android:gravity="center"
23+
android:orientation="vertical">
24+
25+
<Button
26+
android:id="@+id/btn_write_serial"
27+
android:layout_width="wrap_content"
28+
android:layout_height="wrap_content"
29+
android:layout_marginTop="10dp"
30+
android:text="@string/Write" />
31+
32+
<EditText
33+
android:id="@+id/et_write"
34+
android:layout_width="match_parent"
35+
android:layout_height="wrap_content"
36+
android:layout_marginLeft="10dp"
37+
android:layout_marginRight="10dp"
38+
android:layout_marginTop="5dp"
39+
android:ems="10" />
40+
41+
<LinearLayout
42+
android:layout_width="match_parent"
43+
android:layout_height="wrap_content"
44+
android:layout_marginBottom="10dp"
45+
android:orientation="horizontal">
46+
47+
<TextView
48+
android:id="@+id/textView6"
49+
android:layout_width="wrap_content"
50+
android:layout_height="wrap_content"
51+
android:text="@string/uart1" />
52+
53+
<TextView
54+
android:id="@+id/tv1_data"
55+
android:layout_width="wrap_content"
56+
android:layout_height="wrap_content"
57+
android:layout_marginLeft="10dp"
58+
android:layout_weight="1" />
59+
60+
<TextView
61+
android:id="@+id/tv1_timestamp"
62+
android:layout_width="wrap_content"
63+
android:layout_height="wrap_content"
64+
android:layout_marginLeft="5dp"
65+
android:layout_marginRight="10dp"
66+
android:layout_weight="0" />
67+
</LinearLayout>
68+
69+
<LinearLayout
70+
android:layout_width="match_parent"
71+
android:layout_height="wrap_content"
72+
android:layout_marginBottom="10dp"
73+
android:orientation="horizontal">
74+
75+
<TextView
76+
android:id="@+id/textView6"
77+
android:layout_width="wrap_content"
78+
android:layout_height="wrap_content"
79+
android:text="@string/uart2" />
80+
81+
<TextView
82+
android:id="@+id/tv2_data"
83+
android:layout_width="wrap_content"
84+
android:layout_height="wrap_content"
85+
android:layout_marginLeft="10dp"
86+
android:layout_weight="1" />
87+
88+
<TextView
89+
android:id="@+id/tv2_timestamp"
90+
android:layout_width="wrap_content"
91+
android:layout_height="wrap_content"
92+
android:layout_marginLeft="5dp"
93+
android:layout_marginRight="10dp"
94+
android:layout_weight="0" />
95+
</LinearLayout>
96+
97+
<LinearLayout
98+
android:layout_width="match_parent"
99+
android:layout_height="wrap_content"
100+
android:orientation="horizontal">
101+
102+
<TextView
103+
android:id="@+id/textView6"
104+
android:layout_width="wrap_content"
105+
android:layout_height="wrap_content"
106+
android:text="@string/uart3" />
107+
108+
<TextView
109+
android:id="@+id/tv3_data"
110+
android:layout_width="wrap_content"
111+
android:layout_height="wrap_content"
112+
android:layout_marginLeft="10dp"
113+
android:layout_weight="1" />
114+
115+
<TextView
116+
android:id="@+id/tv3_timestamp"
117+
android:layout_width="wrap_content"
118+
android:layout_height="wrap_content"
119+
android:layout_marginLeft="5dp"
120+
android:layout_marginRight="10dp"
121+
android:layout_weight="0" />
122+
</LinearLayout>
123+
124+
</LinearLayout>
125+
18126
<LinearLayout
19127
android:id="@+id/tab1"
20128
android:layout_width="match_parent"
@@ -255,70 +363,6 @@
255363
</LinearLayout>
256364
</LinearLayout>
257365

258-
<LinearLayout
259-
android:id="@+id/tab4"
260-
android:layout_width="match_parent"
261-
android:layout_height="match_parent"
262-
android:gravity="center"
263-
android:orientation="vertical" >
264-
265-
<TextView
266-
android:id="@+id/textView6"
267-
android:layout_width="wrap_content"
268-
android:layout_height="wrap_content"
269-
android:layout_marginBottom="10dp"
270-
android:text="@string/uart_interface"
271-
android:textAppearance="?android:attr/textAppearanceLarge" />
272-
273-
<TextView
274-
android:id="@+id/tv_UART"
275-
android:layout_width="wrap_content"
276-
android:layout_height="wrap_content"
277-
android:autoLink="web"
278-
android:clickable="true"
279-
android:text="@string/uart_interface_link"
280-
android:textAppearance="?android:attr/textAppearanceLarge" />
281-
282-
<Button
283-
android:id="@+id/btn_write_serial"
284-
android:layout_width="wrap_content"
285-
android:layout_height="wrap_content"
286-
android:layout_marginTop="10dp"
287-
android:text="@string/Write" />
288-
289-
<EditText
290-
android:id="@+id/et_write"
291-
android:layout_width="match_parent"
292-
android:layout_height="wrap_content"
293-
android:layout_marginLeft="10dp"
294-
android:layout_marginRight="10dp"
295-
android:layout_marginTop="5dp"
296-
android:ems="10" />
297-
298-
<ToggleButton
299-
android:id="@+id/btn_read_serial"
300-
android:layout_width="wrap_content"
301-
android:layout_height="wrap_content"
302-
android:layout_marginTop="20dp" />
303-
304-
<TextView
305-
android:id="@+id/textView4"
306-
android:layout_width="wrap_content"
307-
android:layout_height="wrap_content"
308-
android:layout_marginTop="5dp"
309-
android:text="@string/Read"
310-
android:textAppearance="?android:attr/textAppearanceLarge" />
311-
312-
<EditText
313-
android:id="@+id/et_read"
314-
android:layout_width="match_parent"
315-
android:layout_height="wrap_content"
316-
android:layout_marginLeft="10dp"
317-
android:layout_marginRight="10dp"
318-
android:layout_marginTop="5dp"
319-
android:ems="10" />
320-
</LinearLayout>
321-
322366
<LinearLayout
323367
android:id="@+id/tab3"
324368
android:layout_width="match_parent"

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,8 @@
3434
<string name="uart_interface">UART interface using the C2 Tinkering Kit</string>
3535
<string name="uart_interface_link">http://odroid.com/dokuwiki/doku.php?id=en:c2_hardware_uart</string>
3636
<string name="altitude">Altitude</string>
37+
<string name="uart1">UART1 : </string>
38+
<string name="uart2">UART2 : </string>
39+
<string name="uart3">UART3 : </string>
3740

3841
</resources>

0 commit comments

Comments
 (0)