我目前面临一个串行通信问题,这让我困扰了几天。
该应用程序是一种个人 RFID 标签库存系统。最终目标是扫描 RFID 标签并将它们记录到 arduino FRAM 中,并将标签数据中继到我正在制作的 android 智能手机应用程序。作为一个整体,我是 Java 和 Android 开发的新手,我在读取 arduino 正在写入的 (RX) 串行数据 (TX) 时遇到问题。
我正在使用蓝牙适配器(Bluesmirf,RN42 调制解调器),配对和连接都很好。
我能够从 android 手机传输“刷新库存”切换,并且 Arduino 确实收到了它,因为它跳转到 AndroidRefresh() 函数,如代码中所示。但是,当我尝试编写测试 RFID 标签时,Android 什么也看不到。
我 80% 确定这是我的 Android 代码中的一个缺陷。我在想这可能是一个“时间”问题,因为一旦在 Android 上切换“刷新库存”按钮,它就会发送“标志 int”以将 arduino 发送到 AndroidRefresh(),并立即在两个脚本中, arduino 写,Android 听。我是串行通信的新手,我不确定这些数据是否由于某种原因从缓冲区中消失了?
谢谢你的帮助。这真让我抓狂。
Arduino 片段:
#include <SoftwareSerial.h>
#include <EEPROM.h>
#include < avr/interrupt.h >
#include < avr/io.h >
//setup serial for RFID reader
#define rxPin 3
#define txPin 2
SoftwareSerial rfserial = SoftwareSerial(rxPin, txPin);
//seek command 0x82
byte SeekCard[] = {0xFF,0x00,0x01,0x82,0x83};
byte value;
int k;
char incomingChar;
long convert = 0;
void setup()
{
//set the Serial monitor to preferred baud rate
Serial.begin(9600);
//RFID reader is defaulted to 19200 baud rate
rfserial.begin(19200);
// for (int i =0; i< 7; i++){
// convert = convert + test[i];
//Serial.println(convert, DEC);
// delay(100);
// }
}
void loop()
{
//find a tag
if (rfserial.available() > 0){
SeekTag();
}
if (Serial.available() > 0){
androidRefresh(); // Refresh Inventory
while(Serial.available()>0) Serial.read(); //CLEARS RX BUFFER
}
}
void SeekTag(){
// Do RFID stuff
}
void androidRefresh()
{
//*************receiving a message from Android and printing on Arduino*************************
byte test[] = {0xFF,0x00,0x01,0x82,0x83, 0xFC, 0x64, 0xD0, 0x82,0x83, 0xFF};
//Serial.println("Refreshing Inventory...");
for (int i =0; i< 11; i++){
Serial.write(test[i]);
Serial.println(test[i]);
// Serial.println(test[i], HEX);
}
//while(Serial.available()>0) Serial.read(); //CLEARS RX BUFFER
}
和 Android Java 活动:
public class InventoryActivity extends Activity {
/** Bluetooth Variables **/
private static BluetoothSocket mbtSocket;
private static InputStream mbtInputStream;
private static OutputStream mbtOutputStream;
private static final String TAG = "SmartFridge"; //Debug
private static final boolean D = true; // Debug
OutputStream tmpOut = null;
OutputStream mmOutStream = null;
InputStream tmpIn = null;
InputStream mmInStream = null;
byte[] buffer = new byte[1024];; // buffer store for the stream
int bytes; // bytes returned from read()
int numberofbytes = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inventory);
final ListView listview = (ListView) findViewById(R.id.listview);
String[] values = new String[] { "Item 1 from Arduino",
"Item 2 from Arduino",
"Item 3 from Arduino",
"Item 4 from Arduino",
"Item 5 from Arduino",
"and the beat goes on"};
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
}
// Menu Stuff
// Initiating Menu XML file (menu.xml)
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.inventory_menu, menu);
return true;
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_search:
Toast.makeText(InventoryActivity.this, "Refreshing...", Toast.LENGTH_SHORT).show();
{
// As suggested by http://developer.android.com/guide/topics/connectivity/bluetooth.html
mbtSocket = btWrapper.getSocket();
try {
tmpOut = mbtSocket.getOutputStream();
tmpIn = mbtSocket.getInputStream();
if(D) Log.e(TAG, "Test 1");
} catch (IOException e1) { }
mmOutStream = tmpOut;
mmInStream = tmpIn;
if(D) Log.e(TAG, "Test 2");
try {
mmOutStream.write(600); // Can be anything, only UI request is to send FRAM contents from Arduino once toggled on phone
if(D) Log.e(TAG, "Serial Message Sent to Arduino for Refresh");
}
catch(Exception e) {}
}
/** Read Bluetooth Stuff **/
// Read from the InputStream
try {
numberofbytes = mmInStream.available();
if(D) Log.e(TAG, numberofbytes + " bytes ready to read");
if( mmInStream.available() > 0 )
{
bytes = mmInStream.read(buffer);
if(D) Log.e(TAG, "Received Data from Arduino");
if(D) Log.e(TAG, "Received:" + bytes);
}
} catch (IOException e) {
if(D) Log.e(TAG, "Did not receive data from Arduino");
}
/** End Read Bluetooth Stuff **/
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//End Menu STuff
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
}