Problem with Recieving serial Strings ending with LF or CR's.
in
Core Library Questions
•
2 years ago
i have a device that outputs serial data like a string, and ends with 0D then 0A(ASCII:Linefeed then Carriage Return). When using the code below, it seems to be ignoring them and never prints a line or outputs. Hyper-terminal Reads the data fine.
PLEASE HELP!
ASCII DATA BEING SENT TO PROCESSING( one line):
00000000: 30 37 2f 30 36 2f 31 31 | 20 20 31 32 3a 33 33 3a 07/06/11 12:33:
00000010: 32 33 20 20 20 31 30 30 | 30 20 20 20 20 20 42 41 23 1000 BA
00000020: 54 54 45 52 59 20 46 41 | 49 4c 45 44 0d 0a TTERY FAILED..
Hyper-Terminal Output:
07/06/11 12:33:23 1000 BATTERY FAILED
- import processing.serial.*;
- PrintWriter output;
- int lf = 10; // Linefeed in ASCII
- String ComPortString = null;
- Serial myPort; // The serial port
- void setup() {
- output = createWriter("Log-" + DateTimestamp() + ".txt");
- // List all the available serial ports
- println(Serial.list());
- // I know that the first port in the serial list on my mac
- // is always my Keyspan adaptor, so I open Serial.list()[0].
- // Open whatever port is the one you're using.
- myPort = new Serial(this, Serial.list()[0], 9600);
- myPort.clear();
- // Throw out the first reading, in case we started reading
- // in the middle of a string from the sender.
- ComPortString = myPort.readStringUntil(lf);
- ComPortString = null;
- output.println(timestamp() + " " + "Logging Started");
- }
- void draw() {
- while (myPort.available() > 0) {
- ComPortString = myPort.readStringUntil(lf);
- if (ComPortString != null) {
- println(ComPortString);
- output.println(timestamp() + " " + ComPortString);
- output.flush();
- }
- }
- }
- String timestamp() {
- Calendar now = Calendar.getInstance();
- return String.format("%1$tH:%1$tM:%1$tS", now);
- }
- String DateTimestamp() {
- Calendar now = Calendar.getInstance();
- return String.format("%1$tm%1$td%1$ty_%1$tH%1$tM%1$tS", now);
- }
- void stop() {
- output.flush(); // Write the remaining data
- output.close(); // Finish the file
- super.stop();
- }
1