Processing app hangs when waiting for serial input from Arduino
in
Integration and Hardware
•
7 months ago
Hello
I have a serious issue that I haven't been able to work out by myself as of yet. Have tried all kinds of tricks and Googled all I can and nada.
I have an Arduino connected to my PC via the USB serial connection (the usual). On the Arduino is a piece of code that listens to the serial port and every character that it receives is saved to a string. Once the serial event handler sees a newline character (ASCII 10), the Arduino sends 'X' over the serial back to the PC to inform that the string was received and magic was done it. This is a way to let the Processing app to know that it can send the next line to the Arduino.
I tested the Arduino side by opening the serial monitor and when I type text to it, it receives everything and stays quiet until I type a newline to it, at which point I receive X to my serial monitor.
However, when I try the following code on Processing to send an array of strings to the Arduino, it first sends the first line, gets the X and then just hangs there, waiting for the X. For some reason it receives the X from the Arduino, but the code doesn't go ahead but instead just says there, waiting for an X that it already received.
I'm running nuts here, so please, if anyone sees the error in my code, please tell me how to correct it or how to do this properly in case I have violated something sacred. Thank you! :)
Processing code:
Running the above code I get this as output to the console:
I have a serious issue that I haven't been able to work out by myself as of yet. Have tried all kinds of tricks and Googled all I can and nada.
I have an Arduino connected to my PC via the USB serial connection (the usual). On the Arduino is a piece of code that listens to the serial port and every character that it receives is saved to a string. Once the serial event handler sees a newline character (ASCII 10), the Arduino sends 'X' over the serial back to the PC to inform that the string was received and magic was done it. This is a way to let the Processing app to know that it can send the next line to the Arduino.
I tested the Arduino side by opening the serial monitor and when I type text to it, it receives everything and stays quiet until I type a newline to it, at which point I receive X to my serial monitor.
However, when I try the following code on Processing to send an array of strings to the Arduino, it first sends the first line, gets the X and then just hangs there, waiting for the X. For some reason it receives the X from the Arduino, but the code doesn't go ahead but instead just says there, waiting for an X that it already received.
I'm running nuts here, so please, if anyone sees the error in my code, please tell me how to correct it or how to do this properly in case I have violated something sacred. Thank you! :)
Processing code:
- import processing.serial.*;
- Serial serialPort;
- String serialPortName = "/dev/ttyACM0";
- boolean waitForOK = true;
- String linesFromFile[];
- String fileName;
- String serialInput;
- void setup()
- {
- size(50, 50);
- serialPort = new Serial(this, serialPortName, 9600);
- serialPort.bufferUntil('X'); // Trigger the serial event handler only after receivig 'X'.
- }
- void draw()
- {
- selectInput("Select a file to send:", "fileSelected");
- send_file_to_arduino();
- noLoop(); // Stop unnecessary looping.
- }
- void fileSelected(File selection)
- {
- if (selection != null)
- {
- fileName = selection.getAbsolutePath(); // Save file name to a string.
- linesFromFile = loadStrings(selection.getAbsolutePath()); // Read file contents line by line.
- }
- }
- void send_file_to_arduino()
- {
- // Sends the selected file to the wire EDM
- // using the serial port connection. Sends
- // one line at a time and waits for an
- // acknowledgement that ends in line feed.
- for (int index = 0; index < linesFromFile.length; index++)
- {
- print("Sending line " + index + "...");
- serialPort.write(linesFromFile[index]); // Send one line to the controller.
- serialPort.write(10); // Send line feed character to inform Arduino it has a sentence.
- while(waitForOK) // Wait until X is received
- {
- }
- waitForOK = true;
- println("got " + serialInput);
- }
- }
- void serialEvent(Serial serialPort)
- {
- serialInput = serialPort.readString();
- if (serialInput.equals("X"))
- {
- println("serialEvent: received X, waitForOk == false");
- waitForOK = false;
- }
- else
- {
- println("serialEvent:" + serialInput + "." + serialInput.length());
- }
- }
Running the above code I get this as output to the console:
- [0] "/dev/ttyACM0"
- [1] "/dev/ttyS0"
- Sending line 0...serialEvent: received X, waitForOk == false
1