禁用COM3 null的serialEvent()时出错

时间:2017-04-05 06:43:58

标签: java arduino processing

在我的项目中,我试图从Arduino读取RFID编号和数量,并使用Processing来更新本地主机上的数据库。它首次提供输出,然后给出错误。

Arduino代码:

int count = 0;                                          // count = 0
                                         // character array of size 12 
char input[12];                                // character array of size 12 
String in;
boolean flag = 0;                                       // flag =0
void setup()
{
   Serial.begin(9600);          // begin serial port with baud rate 9600bps
}
void loop()
{
    if(Serial.available())
    {
        count = 0;
        while(Serial.available() && count<12)
        // Read 12 characters and store them in //input array
        {
            input[count] = Serial.read();
            count++; 
            delay(5);
        } 
        String input1= String(input);
        int value=20;
        String stringOne =  String(value); 
        Serial.print (input1 +String("r")+ stringOne + String( ";") + String("r"));
        //Serial.write(in);   
    }
}

我的处理代码是:

import cc.arduino. * ;
import org.firmata. * ;
import processing.serial. * ;
import de.bezier.data.sql.mapper. * ;
import de.bezier.data.sql. * ;
MySQL dbconnection;
Serial myPort; // The serial port
String inByte = " ";
String inByte1 = " ";
String str2;
int breaker = 0;
String rfid;
int Amount;
int count;
int length;
String cutString;
String rfid12 = "";
int amount = 0;
MySQL akshay;
String UniqueID;
//Sftp sftp;
int rupeescharged;

void setup() {
    // List all the available serial ports
    printArray(Serial.list());
    // Open the port you are using at the rate you want:
    ***myPort = new Serial(this, Serial.list()[0], 9600);    <--- getting error on this line my serialEvent() gets disable for this port 
    myPort.buffer(18);***
    String user = "root";
    String pass = "";
    String database = "akshay";
    // connect to database of server "localhost"
    akshay = new MySQL(this, "localhost", database, user, pass);
    if (breaker == 1) {
        Amountfinal(rfid12, amount);
    }
    //setup();  
    noLoop();

}

void draw() {

}

void serialEvent(Serial myPort) {

    inByte = myPort.readString();
    String cutString = inByte.substring(0, 18);
    String[] list = split(cutString, ';');
    // print(cutString);
    //print(list[0]);
    String input = list[0];
    String[] init = split(input, 'r');
    rfid = init[0];

    String amt = init[1];
    Amount = Integer.parseInt(amt);
    String rfid1 = rfid.substring(0, 12);
    //println(rfid);
    //print(Amount);
    Akshay(rfid1, Amount);

}

void Akshay(String rfid, int amount) {
    String rfid_1 = rfid;
    int amt = amount;
    rfid_1 = rfid_1.replace("\f", "@");
    rfid_1 = rfid_1.replace(".", "@");
    rfid_1 = rfid_1.replace("\b", "@");
    rfid_1 = rfid_1.replace("\n", "@");
    rfid_1 = rfid_1.replace("\t", "@");

    //   println(rfid_1);
    if (rfid_1.charAt(rfid_1.length() - 1) == '@') {
        rfid_1 = rfid_1.substring(0, rfid_1.length() - 1);
    }
    println(rfid_1);
    //print(amt);
    Amountfinal(rfid_1, amt);
    breaker = 1;
}

void Amountfinal(String rfid12, int amount) {
    String rfid = rfid12;
    println(rfid);

    int rupeescharged = amount;
    if (akshay.connect()) {
        // rfid= "123";

        akshay.query("SELECT * FROM userrfid");
        while (akshay.next()) {
            // replace "first_name" and "last_name" with column names from your table
            String s1 = akshay.getString("rfid");

            int s2 = akshay.getInt("pass");
            int s3 = s2 - rupeescharged;
            akshay.execute("UPDATE userrfid SET pass='" + s3 + "' WHERE rfid='" + rfid + "'");
            print(s3);

        }
    }
    //setup();
}

0 个答案:

没有答案