3819 views|5 replies

654

Posts

26

Resources
The OP
 

12 "Wanli" Raspberry Pi car - socket learning (Android sending and receiving) [Copy link]

 

Previously, we introduced the code for sending using the UDP protocol on Android, but implementing the receiving function is relatively complicated. Here, we can directly use the library written by predecessors to achieve twice the result with half the effort.

Find a UDP encapsulation on GitHub, see the source code at the end of the article for details.

APP building steps

Create a new blank project and add network usage permissions in AndroidManifest.xml, just like in the previous article.

Copy the UdpClient.java file to the same folder as MainActivity.java. Do not copy it directly into the folder. If you copy it on Android Studio, the package name will be automatically changed.

Modify the layout file "activity_main.xml". The layout is relatively complex. You can see the final effect picture.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="连接状态:"/>
<TextView
android:id="@+id/tv_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="未连接"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="服务器IP地址:"/>
<EditText
android:id="@+id/et_ip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="192.168.31.34"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="服务器端口号:"/>
<EditText
android:id="@+id/et_port"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="1234"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="连接"
android:onClick="onClick"
android:layout_weight="1"/>
<Button
android:id="@+id/btn_disconnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="断开"
android:onClick="onClick"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送内容:"/>
<EditText
android:id="@+id/et_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="lb8820265"/>
</LinearLayout>
<Button
android:id="@+id/btn_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="发送"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="接收内容:"/>
<Button
android:id="@+id/btn_clear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="清空接收"/>
</LinearLayout>
<ScrollView
android:id="@+id/sv_receive"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/et_receive"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/black"
android:background="@null"/>
</ScrollView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Write the MainActivity.java function, which mainly initializes the listener and writes the corresponding functions of each control.

package com.example.lb_socket_android;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.util.Xml;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
private EditText et_ip;
private EditText et_port;
private EditText et_send;
private EditText et_receive;
private TextView tv_state;
private ScrollView sv_receive;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_ip= (EditText) findViewById (R.id.et_ip);
et_port= (EditText) findViewById (R.id.et_port);
et_send=(EditText) findViewById (R.id.et_send);
tv_state= (TextView) findViewById (R.id.tv_state);
et_receive= (EditText) findViewById (R.id.et_receive);
sv_receive= (ScrollView) findViewById (R.id.sv_receive);
UdpClient.getInstance().setOnDataReceiveListener(dataReceiveListener);
}
public void onClick(View view){
switch (view.getId()) {
case R.id.btn_clear:
et_receive.setText("");
break;
case R.id.btn_send:
if (UdpClient.getInstance().isConnect()) {
byte[] data=et_send.getText().toString().getBytes();
String str = new String(data);
Log.i("TAG_log",str);
UdpClient.getInstance().sendByteCmd(data,1001);
} else {
Toast.makeText(MainActivity.this,"尚未连接,请连接Socket",Toast.LENGTH_SHORT).show();
}
break;
case R.id.btn_connect:
String ip = et_ip.getText().toString();
String port = et_port.getText().toString();

if(TextUtils.isEmpty(ip)){
Toast.makeText(MainActivity.this,"IP地址为空",Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(port)){
Toast.makeText(MainActivity.this,"端口号为空",Toast.LENGTH_SHORT).show();
return;
}
UdpClient.getInstance().connect(ip, Integer.parseInt(port));
break;
case R.id.btn_disconnect:
UdpClient.getInstance().disconnect();
tv_state.setText("未连接");
break;
default:
break;
}
}
private UdpClient.OnDataReceiveListener dataReceiveListener = new UdpClient.OnDataReceiveListener() {
@Override
public void onConnectSuccess() {
Log.i("TAG_log","onDataReceive connect success");
tv_state.setText("已连接");
}
@Override
public void onConnectFail() {
Log.e("TAG_log","onDataReceive connect fail");
tv_state.setText("未连接");
}
@Override
public void onDataReceive(byte[] buffer, int size, int requestCode) {
//获取有效长度的数据
byte[] data = new byte[size];
System.arraycopy(buffer, 0, data, 0, size);
String oxValue = new String(data);
Log.i("TAG_log","onDataReceive requestCode = "+requestCode + ", content = "+oxValue);
et_receive.append(oxValue + "\n");
sv_receive.fullScroll(View.FOCUS_DOWN);
}
};
@Override
protected void onDestroy() {
UdpClient.getInstance().disconnect();
super.onDestroy();
}
}

How to run

  1. Connect the mobile phone and PC to the same router, obtain the IP address of the PC, and modify the IP address in the APP code.
  2. Compile and run the socket_UDP_win_server.cpp written previously on Windows.
  3. Compile and run the app on a real device, first click the "Connect" button, then click the "Send" button.

Operation effect

question

Now that the communication problem has been solved, the next step is programming the car, such as how to control the motor, how to control the speed, etc.

Source code

GitHub:

链接已隐藏,如需查看请登录或者注册

Gitee:

链接已隐藏,如需查看请登录或者注册

This post is from Innovation Lab

Latest reply

Summary of "Wanli" Raspberry Pi car: lb8820265's "Wanli" Raspberry Pi car open source sharing - DIY/Open Source Hardware Zone - Electronic Engineering World - Forum (eeworld.com.cn) Table of contents: "Wanli" Raspberry Pi car launched 1. “Wanli” Raspberry Pi car - Establishing a project warehouse 2. "Wanli" Raspberry Pi car - Python learning (using Thonny) 3. "Wanli" Raspberry Pi car - Python learning (timing task) 4. "Wanli" Raspberry Pi car - C++ learning (compile and run, use geany) 5. "Ten Thousand Miles" Raspberry Pi Car - WiringPi Learning (Delay and Thread Simulation Timer) 6. "Wanli" Raspberry Pi car - wiringPi learning (PWM and external interrupt simulation timer) 7. "Ten Thousand Miles" Raspberry Pi Car——RPi.GPIO Learning (PWM and External Interrupt Simulation Timer) 8. "Wanli" Raspberry Pi car - socket learning (local communication) 9. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (TCP Two-Machine Communication) 10. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (UDP Two-Machine Communication) 11. "Wanli" Raspberry Pi car - socket learning (sent from Android) 12 "Wanli" Raspberry Pi car - socket learning (Android sending and receiving) 13. "Wanli" Raspberry Pi Car - Accessories Preparation 14 "Wanli" Raspberry Pi car - motor drive learning 15 "Wanli" Raspberry Pi car - photoelectric encoder learning (forward and reverse judgment) 16. "Wanli" Raspberry Pi car - photoelectric encoder learning (obtaining speed) 17 "Ten Thousand Miles" Raspberry Pi Car——VSCode Learning (Compiling and Debugging) 18. "Ten Thousand Miles" Raspberry Pi Car——Makefile Learning 19 "Ten Thousand Miles" Raspberry Pi Car——VSCode Learning (Multiple C File Link Debugging) 20 "Million Miles" Raspberry Pi Car - Motor Control Learning (Control Speed) 21. "Wanli" Raspberry Pi car - motor control learning (4-wheel speed control) 22. "Wanli" Raspberry Pi car - mobile phone remote control motor rotation 23 "Wanli" Raspberry Pi car - connected to Raspberry Pi without screen 24 "Millions" Raspberry Pi Car - Bullseye Benchmark Test of Raspberry Pi 64-bit System 25 "Million Miles" Raspberry Pi Car - Nam Wheel Control 26 "Wanli" Raspberry Pi car - program startup 27 "Ten Thousand Miles" Raspberry Pi Car - Fix and Get the Raspberry Pi IP Address 28 "Wanli" Raspberry Pi car - car assembly 29 "Wanli" Raspberry Pi car - straight-driving deviation problem and new control mode 30. "Wanli" Raspberry Pi car - Phase 1 completed demonstration (introduction from scratch)   Details Published on 2022-3-21 13:40
Personal signatureQQ:252669569
 
 

1w

Posts

204

Resources
From 2
 

Summary of "Wanli" Raspberry Pi car:

lb8820265's "Wanli" Raspberry Pi car open source sharing - DIY/Open Source Hardware Zone - Electronic Engineering World - Forum (eeworld.com.cn)

Table of contents:

"Wanli" Raspberry Pi car launched

1. “Wanli” Raspberry Pi car - Establishing a project warehouse

2. "Wanli" Raspberry Pi car - Python learning (using Thonny)

3. "Wanli" Raspberry Pi car - Python learning (timing task)

4. "Wanli" Raspberry Pi car - C++ learning (compile and run, use geany)

5. "Ten Thousand Miles" Raspberry Pi Car - WiringPi Learning (Delay and Thread Simulation Timer)

6. "Wanli" Raspberry Pi car - wiringPi learning (PWM and external interrupt simulation timer)

7. "Ten Thousand Miles" Raspberry Pi Car——RPi.GPIO Learning (PWM and External Interrupt Simulation Timer)

8. "Wanli" Raspberry Pi car - socket learning (local communication)

9. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (TCP Two-Machine Communication)

10. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (UDP Two-Machine Communication)

11. "Wanli" Raspberry Pi car - socket learning (sent from Android)

12 "Wanli" Raspberry Pi car - socket learning (Android sending and receiving)

13. "Wanli" Raspberry Pi Car - Accessories Preparation

14 "Wanli" Raspberry Pi car - motor drive learning

15 "Wanli" Raspberry Pi car - photoelectric encoder learning (forward and reverse judgment)

16. "Wanli" Raspberry Pi car - photoelectric encoder learning (obtaining speed)

17 "Ten Thousand Miles" Raspberry Pi Car——VSCode Learning (Compiling and Debugging)

18. "Ten Thousand Miles" Raspberry Pi Car——Makefile Learning

19 "Ten Thousand Miles" Raspberry Pi Car——VSCode Learning (Multiple C File Link Debugging)

20 "Million Miles" Raspberry Pi Car - Motor Control Learning (Control Speed)

21. "Wanli" Raspberry Pi car - motor control learning (4-wheel speed control)
22. "Wanli" Raspberry Pi car - mobile phone remote control motor rotation

23 "Wanli" Raspberry Pi car - connected to Raspberry Pi without screen

24 "Millions" Raspberry Pi Car - Bullseye Benchmark Test of Raspberry Pi 64-bit System

25 "Million Miles" Raspberry Pi Car - Nam Wheel Control

26 "Wanli" Raspberry Pi car - program startup

27 "Ten Thousand Miles" Raspberry Pi Car - Fix and Get the Raspberry Pi IP Address

28 "Wanli" Raspberry Pi car - car assembly

29 "Wanli" Raspberry Pi car - straight-driving deviation problem and new control mode

30. "Wanli" Raspberry Pi car - Phase 1 completed demonstration (introduction from scratch)

This post is from Innovation Lab
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
Personal signature

玩板看这里:

http://en.eeworld.com/bbs/elecplay.html

EEWorld测评频道众多好板等你来玩,还可以来频道许愿树许愿说说你想要玩的板子,我们都在努力为大家实现!

 
 
 

1

Posts

0

Resources
3
 

6666666666666666666666666666666666666666666666666666666666666666666666

This post is from Innovation Lab
 
 
 

2w

Posts

74

Resources
4
 

Come on, great progress.

This post is from Innovation Lab
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
Personal signature

加油!在电子行业默默贡献自己的力量!:)

 
 
 

4

Posts

0

Resources
5
 

Learned

This post is from Innovation Lab
 
 
 

5216

Posts

239

Resources
6
 

Summary (16 articles updated): lb8820265's "Wanli" Raspberry Pi car open source sharing

This post is from Innovation Lab
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list