Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Io 如何在处理过程中管理多个串行消息_Io_Arduino_Processing_Messages - Fatal编程技术网

Io 如何在处理过程中管理多个串行消息

Io 如何在处理过程中管理多个串行消息,io,arduino,processing,messages,Io,Arduino,Processing,Messages,我正在读取RFID卡的UID,并将其存储在名为myUID的变量中 之后,我将使用出厂密钥授权卡并读取块号4(之前已写入),并将其存储在字符串readBlock中 在Arduino上,我将变量打印到串行接口上,如下所示 Serial.println(myUID); Serial.println(readBlock); 在客户端,我使用一个读取串行数据的Java程序。我的程序使用 现在我的data.txt应该是 xxx xxx xxx xxx (uid of card) 00 00 00 00 0

我正在读取RFID卡的UID,并将其存储在名为
myUID
的变量中

之后,我将使用出厂密钥授权卡并读取块号4(之前已写入),并将其存储在字符串
readBlock

在Arduino上,我将变量打印到串行接口上,如下所示

Serial.println(myUID);
Serial.println(readBlock);
在客户端,我使用一个读取串行数据的Java程序。我的程序使用

现在我的data.txt应该是

xxx xxx xxx xxx (uid of card)
00 00 00 00 00 00 00 00 ... (read block from card)
但是看起来

237 63 58 1
07
37 37 95
 37 
97 98 50 54 37 5
4 55 102 55 52 
45 98

我尝试过几种方法,比如
readStringUntil('\n')在处理库中,但没有成功。

对于所有感兴趣的人,我自己已经通过几个小时的谷歌搜索解决了这个问题,所以这可能会对将来的人有所帮助:

我可以用以下代码修复它:

import processing.serial.*;
int count = 0;
String input = "";
String fileName = dataPath("SET FILEPATH HERE");

Serial mySerial;

import java.io.*;

void setup() {
  mySerial = new Serial(this, Serial.list()[0], 9600);
  mySerial.bufferUntil('\n');

  File f = new File(fileName);
  if (f.exists()) {
    f.delete();
  }
}

void draw(){}

// listen to serial events happening
void serialEvent(Serial mySerial){
  input = mySerial.readStringUntil('\n'); 
  write(input, count);
  count++;
}

// function for writing the data to the file
void write(String inputString, int counter) {
  // should new data be appended or replace any old text in the file?  
  boolean append = false;

  // just for my purpose, because I have got two lines of serial which need to get written to the file 
  //(Line 1: UID of card, Line 2: Read block of card)  
  if(counter < 2){
    append = true;  
  }
  else{
    count = 0;
  }


  try {
    File file = new File("D:/xampp/htdocs/pizza/src/rfid/data.txt");

    if (!file.exists()) {
      file.createNewFile();
    }

    FileWriter fw = new FileWriter(file, append);
    BufferedWriter bw = new BufferedWriter(fw);
    PrintWriter pw = new PrintWriter(bw);

    pw.write(inputString + '\n');
    pw.close();
  }
  catch(IOException ioe) {
    System.out.println("Exception ");
    ioe.printStackTrace();
  }
}
import processing.serial.*;
整数计数=0;
字符串输入=”;
字符串文件名=数据路径(“在此处设置文件路径”);
串行mySerial;
导入java.io.*;
无效设置(){
mySerial=newserial(这是Serial.list()[0],9600);
mySerial.bufferUntil('\n');
文件f=新文件(文件名);
如果(f.exists()){
f、 删除();
}
}
void draw(){}
//收听发生的连续事件
void serialEvent(Serial mySerial){
input=mySerial.readStringUntil('\n');
写入(输入、计数);
计数++;
}
//用于将数据写入文件的函数
无效写入(字符串输入字符串,整数计数器){
//是否应追加新数据或替换文件中的任何旧文本?
布尔追加=假;
//只是为了我的目的,因为我有两行序列号需要写入文件
//(第1行:卡的UID,第2行:卡的读取块)
如果(计数器<2){
append=true;
}
否则{
计数=0;
}
试一试{
File File=新文件(“D:/xampp/htdocs/pizza/src/rfid/data.txt”);
如果(!file.exists()){
createNewFile();
}
FileWriter fw=新的FileWriter(文件,追加);
BufferedWriter bw=新的BufferedWriter(fw);
PrintWriter pw=新的PrintWriter(bw);
write(inputString+'\n');
关闭();
}
捕获(ioe异常ioe){
System.out.println(“例外”);
ioe.printStackTrace();
}
}

好的,所以我设法让append函数工作,但是文本的格式仍然很奇怪。。。我不知道为什么:(您是否确认
9600
的波特率是正确的?
RFID
卡是否返回ASCII编码的字符或仅二进制数据?我已确认波特率,并且卡返回十六进制数据。
import processing.serial.*;
int count = 0;
String input = "";
String fileName = dataPath("SET FILEPATH HERE");

Serial mySerial;

import java.io.*;

void setup() {
  mySerial = new Serial(this, Serial.list()[0], 9600);
  mySerial.bufferUntil('\n');

  File f = new File(fileName);
  if (f.exists()) {
    f.delete();
  }
}

void draw(){}

// listen to serial events happening
void serialEvent(Serial mySerial){
  input = mySerial.readStringUntil('\n'); 
  write(input, count);
  count++;
}

// function for writing the data to the file
void write(String inputString, int counter) {
  // should new data be appended or replace any old text in the file?  
  boolean append = false;

  // just for my purpose, because I have got two lines of serial which need to get written to the file 
  //(Line 1: UID of card, Line 2: Read block of card)  
  if(counter < 2){
    append = true;  
  }
  else{
    count = 0;
  }


  try {
    File file = new File("D:/xampp/htdocs/pizza/src/rfid/data.txt");

    if (!file.exists()) {
      file.createNewFile();
    }

    FileWriter fw = new FileWriter(file, append);
    BufferedWriter bw = new BufferedWriter(fw);
    PrintWriter pw = new PrintWriter(bw);

    pw.write(inputString + '\n');
    pw.close();
  }
  catch(IOException ioe) {
    System.out.println("Exception ");
    ioe.printStackTrace();
  }
}