serial connection issues, Windows and Chrome app

Status
Not open for further replies.

gbathree

Active member
First, background: we are using the Teensy 3.1 to build a handheld spectroscopic device for measuring plant health (www.photosynq.org).

We have slowly troubleshot connection with Windows, and finally we have a pretty consistent method for connecting to the device:

First, you have to have the driver from teensyduino installed. Then, power the device first from batteries (ie turn on the teensy), then plug it in to the USB, then turn on chrome app. At that point, it works. If you unplug the device or restart it you must wait 7 seconds before plugging it back in (windows has to release the COM port, which takes some time), then you must follow the same protocol.

Any other combination (plug in to USB without turning on teensy first, start chrome app first, etc.) does not work, or works once in a blue moon.

I think Paul told us some about this problem when we met at the Maker Faire, but I'd love an update or any suggestions to address it.

MORE INFO:

In case you want more info about the device, we've tested in mac, windows, and linux as follows:

NOTE: Anything that says 'OK' was tested by sending 10 protocols (about 4000 chars)

Windows 8 and Windows 7:
Using serial communication software like cutecom, coolterm, etc.
- Serial: Works fine ONLY when you power the device first, then plug in AND if you unplug the device you must wait 7 seconds before plugging it back in (windows has to release the COM port, which takes some time)
- Bluetooth: OK
Using Chrome app for serial communication
- Serial: Works fine ONLY when you power the device first, then plug in AND if you unplug the device you must wait 7 seconds before plugging it back in (windows has to release the COM port, which takes some time)
- Bluetooth: OK

MacOS (Mavericks):
Using serial communication software like cutecom, coolterm, etc.
- Serial: OK
- Bluetooth: OK
Using Chrome app for serial communication
- Serial: OK
- Bluetooth: OK

Ubuntu Linux:
Using serial communication software like cutecom, coolterm, etc.
- Serial: OK
- Bluetooth: nope - problem is doesn't natively display rfcomm in lists of com ports
Using Chrome app for serial communication
- Serial: OK
- Bluetooth: nope - problem is doesn't natively display rfcomm in lists of com ports

See link below for the full file if you want to see all the code.
View attachment 2219

So we connect the teensy through either bluetooth or USB. The device listens for either channel to send a '[', then it takes up 5k characters from that channel and puts them into a string (see code below). We then use the JsonParser library (https://github.com/bblanchon/ArduinoJsonParser) to pull the data from the JSON to flash lights, read detectors, or run sensors. The JSON has the format [{"info":4,...},{"info":5,...}...] and can be fairly long, maybe a few hundred characters.

Code:
  if (Serial.peek() == 91) {                     // if I see a '[' then go
#ifdef DEBUGSIMPLE
    Serial.println("comp serial");
#endif
    which_serial = 1;
  }
  else if (Serial1.peek() == 91) {                    // if I see a '[' then go
#ifdef DEBUGSIMPLE
    Serial.println("bluetooth serial");
#endif
    which_serial = 2;
  } 

  Serial.read();                                       // flush the "["
  Serial1.read();                                       // flush the "["
  
  switch (which_serial) {
    case 0:
      Serial.println("error in choosing bluetooth or serial");
      Serial1.println("error in choosing bluetooth or serial");
      break;
    
      case 1:
      Serial.setTimeout(2000);                                          // make sure set timeout is 1 second
      Serial.readBytesUntil('!',serial_buffer,serial_buffer_size);
      for (i=0;i<5000;i++) {                                    // increments through each char in incoming transmission - if it's open curly, it saves all chars until closed curly.  Any other char is ignored.
          if (serial_buffer[i] == '{') {                               // wait until you see a open curly bracket
            while (serial_buffer[i] != '}') {                          // once you see it, save incoming data to json2 until you see closed curly bracket
              json2[number_of_protocols] +=serial_buffer[i];
              i++;
            }
            json2[number_of_protocols] +=serial_buffer[i];            // catch the last closed curly
            number_of_protocols++;
          }        
      }  
      for (i=0;i<number_of_protocols;i++) {
        if (json2[i] != '0') {
#ifdef DEBUGSIMPLE
          Serial.println(json2[i]);
#endif
        }
      }
      break;

      case 2:
      Serial1.setTimeout(1000);                                          // make sure set timeout is 1 second
      Serial1.readBytesUntil('!',serial_buffer,serial_buffer_size);
      for (i=0;i<5000;i++) {                                    // increments through each char in incoming transmission - if it's open curly, it saves all chars until closed curly.  Any other char is ignored.
          if (serial_buffer[i] == '{') {                               // wait until you see a open curly bracket
            while (serial_buffer[i] != '}') {                          // once you see it, save incoming data to json2 until you see closed curly bracket
              json2[number_of_protocols] +=serial_buffer[i];
              i++;
            }
            json2[number_of_protocols] +=serial_buffer[i];            // catch the last closed curly
            number_of_protocols++;
          }        
      }  
      for (i=0;i<number_of_protocols;i++) {
        if (json2[i] != '0') {
#ifdef DEBUGSIMPLE
          Serial.println(json2[i]);
#endif
        }
      }
      break;
  }

I'm primarily using the Serial.readBytesUntil function with Serial.setTimeout in order to accept the JSON over serial. The incoming data can end with a '!' or just be silent for 2 seconds to close out the buffer.
 
Status
Not open for further replies.
Back
Top