注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
cankau的个人空间 https://home.eeworld.com.cn/space-uid-633343.html [收藏] [复制] [分享] [RSS]
日志

如何用智能手机监控CAN总线设备?

已有 775 次阅读2016-7-22 16:27 |个人分类:无线数据采集| 智能手机, 监控, CAN总线

在工业自动化现场我们一直使用笔记本电脑调试设备。随着智能手机的发展,使用智能手机连接CAN总线设备进行调试和监控,成为了可能。

如何实现CAN总线设备与手机的连接呢?

我们使用的智能手机都有WiFi接口,如果可以使用手机通过WiFi连接CAN bus设备,那么我们就可以摆脱笔记本电脑使用手机来调试和监控CAN bus设备。那么,如何能使CAN现场控制总线网络与无线网络转换,由于CAN总线协议数据与TCP/IP协议数据在桢结构上完全不同,所以我们必须使用转换接口对其数据包重新封装。

使用“K9130 CAN转WiFi”或“CAN转WLAN”无线数据采集收发器实现CAN总线设备无线连接Android设备。

K9130是工业级CAN转WiFi无线数据采集收发器。内部集成一路CAN-bus接口,一路WiFi无线接口,及TCP/IP协议栈,符合 IEEE802.11b/g/n标准,具有传输速率高,接收灵敏度高等特点。K9130通过与无线路由器配合使用,设备可以在拥有相同SSID的接入点之间自由漫游,通过无线WiFi把CAN接入Wireless Ethernet,也可以自建热点允许手机,平板,笔记本等设备连接组成无线局域网。用户可以使用K9130轻松完成CAN-bus网络和 Wireless Ethernet的互联互通,进一步拓展CAN-bus网络和WiFi网络的应用范围。使用K9130实现CAN总线设备WiFI无线连接Android手机示意图如图1所示:

手机通过K9130 CAN转WiFi无线数据收发器连接CANbus设备


K9130可以在-25℃~75℃的温度范围内工作。K9130具有接入WiFi网络的能力,CAN口通信波特率最高支持1Mbps,完善支持TCP Server,TCP Client和UDP等多种工作模式。支持AP与Station模式,既可做热点被电脑,平板、手机直接接入,也可做客户端接入路由器。K9130是开放的标准通讯协议,可支持各类开发环境,如VB、VC、C#、Labview、Java、PHP等。支持Windows、Linux、Android、iOS等操作系统。

我们通过使用“K9130 CAN转WiFi”无线数据采集收发器成功实现CAN总线设备无线连接Android设备,方便的实现调试CAN设备,并对CAN设备进行无线数据采集和监控。

以下是Android手机软件收发CAN数据截图:

Android手机软件收发CAN数据截图

Android程序代码:
private void writeFrame(){
boolean result = K9130.write(txbuffer, 0, 16);
if(false == result){
sendFlag = false;
Toast.makeText(TestActivity.this, "连接未建立", Toast.LENGTH_SHORT).show();
K9130.reconect();
return;
}
sendnum++;
Log.i("Write","write hex..."+sendnum);
sendnumText.setText(String.valueOf(sendnum));
if(sendnum >= sendcount2)
{
sendFlag = false;
if(null != txtimer){
txtimer.cancel();
txtimer.purge();
txtimer = null;
}
//Toast.makeText(TestActivity.this, "发送完成", Toast.LENGTH_SHORT).show();
}
StringBuilder str = new StringBuilder();
StringBuilder frame = new StringBuilder();
int frameinfo;
frameinfo = txbuffer[2];
//textView1.append(str.toString());
    Map<String,Object> mMap = null;
for(int i = 0;i < 1;i++){
mMap = new HashMap<String,Object>();  
   mMap.put("img", R.drawable.ic_stat_tx);
   mMap.put("canid", String.format("%02X%02X%02X%02X", txbuffer[3],txbuffer[4],txbuffer[5],txbuffer[6]));
   
   frame.delete(0,frame.length());
   if(0 != (frameinfo&0x80)){
    frame.append("扩展帧");
   }else{
    frame.append("标准帧");
   }
   if(0 != (frameinfo&0x40)){
    frame.append("|远程帧");
   }else{
    frame.append("|数据帧");
   }
   mMap.put("frameinfo",frame.toString()); 
   
   frameinfo &= 0xf;
   for(int j=0;j<frameinfo;j++){
str.append(String.format("%02X ",txbuffer[j+7]));
}        
   mMap.put("candata", str.toString());  
   mMap.put("cantime", datetimeFormat.format(java.util.Calendar.getInstance().getTime())+"@"+sendnum);
   mList.add(0,mMap);
}
mAdapter.notifyDataSetChanged();
}




package com.cankau.k9130;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketAddress;

public class K9130 {
public static boolean reconect(){
try {
socket = new Socket();
socket.connect(addr, time);
in = socket.getInputStream();
out = socket.getOutputStream();
connect = true;
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public static boolean connect(SocketAddress remoteAddr,int timeout){
addr = remoteAddr;
time = timeout;
return reconect();
}
public static boolean dicconnect(){
try {
in.close();
out.close();
socket.close();
in = null;
out = null;
socket = null;
connect = false;
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public static boolean isConnect(){
return connect;
}
public static boolean write(byte[] buffer,int offset,int count){
if(false == connect){
return false;
}
try {
out.write(buffer, offset, count);
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
connect = false;
return false;
}
}
public static int read(byte[] buffer,int byteOffset,int byteCount){
int len;
if(false == connect){
return -1;
}
try {
len = in.available();
byteCount = (byteCount < len?byteCount:len);
in.read(buffer, byteOffset, byteCount);
return byteCount;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
connect = false;
return -1;
}
}
private static SocketAddress addr;
private static int time;
private static Socket socket;
private static InputStream in;
private static OutputStream out;
private static boolean connect = false;;
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/bkcolor"
    android:padding="10dip" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
   <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
   android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content">
   <TableRow
       android:id="@+id/tableRow1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" >
       <TextView
           android:id="@+id/frame_ex_title"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="2"
           android:text="@string/frame_ex_title_string"
           android:textAppearance="?android:attr/textAppearanceSmall" />
       <Spinner
           android:id="@+id/ex"
           android:layout_width="match_parent"
           android:layout_height="wrap_content" 
           android:layout_weight="1"/>
   </TableRow>
   <TableRow
       android:id="@+id/tableRow2"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" >
       <TextView
           android:id="@+id/frame_rtr_title"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="2"
           android:text="@string/frame_rtr_title_string"
           android:textAppearance="?android:attr/textAppearanceSmall" />
       <Spinner
           android:id="@+id/rtr"
           android:layout_width="match_parent"
           android:layout_height="wrap_content" 
           android:layout_weight="1"/>
   </TableRow>
   <TableRow
       android:id="@+id/tableRow3"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" >
       <TextView
           android:id="@+id/frame_id_title"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="2"
           android:text="@string/frame_id_title_string"
           android:textAppearance="?android:attr/textAppearanceSmall" />
       
       <EditText
           android:id="@+id/frame_id"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:ems="10"
           android:inputType="text" />
   </TableRow>
   <TableRow
       android:id="@+id/tableRow4"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" >
       <TextView
           android:id="@+id/frame_datalen_title"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="2"
           android:text="@string/frame_datalen_title_string"
           android:textAppearance="?android:attr/textAppearanceSmall" />
       <EditText
           android:id="@+id/frame_datalen"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:ems="10"
           android:inputType="number" >
           <requestFocus />
       </EditText>
   </TableRow>
   
   <TableRow
       android:id="@+id/tableRow5"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" >
       <TextView
           android:id="@+id/frame_data_title"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="2"
           android:text="@string/frame_data_title_string"
           android:textAppearance="?android:attr/textAppearanceSmall" />
       <EditText
           android:id="@+id/frame_data"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:ems="10"
           android:inputType="text" >
           <requestFocus />
       </EditText>
   </TableRow>
   <TableRow
       android:id="@+id/tableRow6"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" 
       android:stretchColumns="*">
       <TextView
           android:id="@+id/send_count_title"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="2"
           android:text="@string/send_count_title_string"
           android:textAppearance="?android:attr/textAppearanceSmall" />
       <EditText
           android:id="@+id/send_count"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:ems="4"
           android:inputType="number" >
       </EditText>
   </TableRow>
   <TableRow
       android:id="@+id/tableRow7"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:stretchColumns="*" >
       
       <TextView
           android:id="@+id/send_interval_title"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="2"
           android:text="@string/send_interval_title_string"
           android:textAppearance="?android:attr/textAppearanceSmall" />
       <EditText
           android:id="@+id/send_interval"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:ems="4"
           android:inputType="number" >
       </EditText>
   </TableRow>
</TableLayout>
<LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content" >
   
       <Button
           android:id="@+id/save"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:text="@string/save_string" />
       <ToggleButton
           android:id="@+id/hide"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:textOn="@string/hide_on_string"
           android:textOff="@string/hide_off_string"/>
       <ToggleButton
           android:id="@+id/pause"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:textOn="@string/pause_on_string" 
           android:textOff="@string/pause_off_string" />  
      
</LinearLayout>
   <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content" >
   <Button
       android:id="@+id/autosend"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_weight="1"
       android:text="@string/autosend_string" />
   <Button
       android:id="@+id/stopsend"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_weight="1"
       android:text="@string/stopsend_string" />
   <Button
       android:id="@+id/clear"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_weight="1"
       android:text="@string/clear_string"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
   <TextView
           android:id="@+id/send_title"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:text="@string/send_title_string"
           android:textAppearance="?android:attr/textAppearanceSmall" />
       <EditText
           android:id="@+id/send_num"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:ems="10" 
           android:enabled="false"
           android:inputType="number" >
       </EditText>
   <TextView
           android:id="@+id/recv_title"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:text="@string/recv_title_string"
           android:textAppearance="?android:attr/textAppearanceSmall" />
       <EditText
           android:id="@+id/recv_num"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:ems="10"
           android:enabled="false"
           android:inputType="number" >
       </EditText>
       
</LinearLayout>
</LinearLayout> 
  
    </ScrollView>
    
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
   
</LinearLayout>

在很多场合我们不必背着笔记本前往现场采集那么几组数据,完全可以掏出随身携带的手机实现对CAN总线设备的调试和监控。
评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章