Reading serial input

It is posible to read a serial device, like cod bar scanner or a usb to serial interface with particle photon?

Yes. I have a project on my desk where I connect a serial barcode scanner (RS232 I believe) via a serial DB9 connector adapter (which I believe has a Serial-to-TTL chip on it). That adapter is then connected to TX/RX on my photon. I am able to read a barcode scan and send it to my SQL database (via custom http APi) and cloud publish. The breadboard setup is very simple and functions well.

Thanks for your answer, i am going to search info about Serial to TTL chip. Do you read character by character in a loop, the information from your serial barcode scanner in your firmware?

FYI, if you’re going to use the UART (a.k.a. serial port) to read a sensor, I strongly recommend the lib developed by @rickkas7: Particle Serial Interrupt. It will save you many headaches.

1 Like

I also use rickkas7’s SerialBufferRK library. It works well. Here’s the code I use to read the barcode and then send to various enpoints including a custom API written for .Net core and a Particle.publish():

// This #include statement was automatically added by the Particle IDE.
#include <PublishQueueAsyncRK.h>

// This #include statement was automatically added by the Particle IDE.
#include <HttpClient.h>

#include "Particle.h"

#include "SerialBufferRK.h"

//System modes and threading setup.

//Publish Queue/Buffer
retained uint8_t publishQueueRetainedBuffer[2048];
PublishQueueAsync publishQueue(publishQueueRetainedBuffer, sizeof(publishQueueRetainedBuffer));

//http client variables
HttpClient http;

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
    { "Content-Type", "application/json" },
    //  { "Accept" , "application/json" },
    { "Accept" , "*/*"},
    { NULL, NULL } // NOTE: Always terminate headers will NULL

http_request_t request;
http_response_t response;

//Other objects and forward declarations
SerialLogHandler logHandler;            //Log output handler
SerialBuffer<4096> serBuf(Serial1);     //Serial buffer object

void resetSequence();   //Forward Declaration

//primary datatype variables (int, bool, etc)
unsigned long lastRead = 0;     //Last time a char was read.
int readTimeout = 500;          //How long to wait for a full barcode in ms.
bool terminateNow = false;      //Set high if a terminating char is encountered in a barcode read (i.e. CR, LF, Tab).

uint8_t charsRead = 0;          //Number of chars read in this barcode sequence.
uint32_t totalChars = 0;        //Total chars read since boot.
uint32_t totalBarcodes = 0;     //Total barcodes read since boot.

uint8_t lastCharIndex = 0;      //Holds the index of the last char read. Used to determine if read timeout should be set.

char pubString[255];            //String to use for publishing to endpoint.
char barCode[255];              //String to hold current barcode.

int LED = D7;                   //Use the built-in LED as a read timeout indicator.

void setup() {
	Serial.begin();             //Start USB serial for monitoring.
	Serial1.begin(9600);        //Start UART serial for reading barcode data from RS232-capable barcode scanner.
	Serial.println("Waiting for Serial Monitor...");
	delay(5000);    //Delay to allow time to connect serial monitor.
	Serial.println("Resetting Serial1 buffer...");

	resetSequence();    //Purge serial buffer.
	pinMode(LED, OUTPUT);   //Set built-in D7 LED to use as an output.
	Serial.println("Waiting for next barcode scan...");

void loop() {
	//Read serial buffer until empty.
	while(true) {
		int c =;  //Read one char at a time.
		if (c < 0) {            //If serial buffer returns a negative value, the buffer is empty.
		//If there is a char to read, and this is the first char, then output the diagnostic message prefix.
		if (charsRead == 0) {
            Serial.print("Chars Received: ");
		//Look for special characters that indicate the end of a barcode sequence.
		if ((char)c == '\r') {  //Detect carriage returns as the end of a barcode sequence.
		    Serial.println("Carriage return detected.");
		    terminateNow = true;
		else if ((char)c == '\n') {  //Detect line feeds as the end of a barcode sequence.
		    Serial.println("New line detected.");
		    terminateNow = true;
		else if ((char)c == '\t') {  //Detect horizontal tabs as the end of a barcode sequence.
		    Serial.println("Horizontal tab detected.");
		    terminateNow = true;
		else {
		    Serial.print((char)c);          //Output each character to serial monitor for diagnostics.
		    barCode[charsRead] = (char)c;   //Add char to the end of the current barcode.

		charsRead++;    //Increment the current barcode char index.
		totalChars++;   //Increment the total chars counter.
	//Set a timeout period indcator after each new character is added to the barcode.
	if (lastCharIndex < charsRead) {
	    serBuf.readClear();         //Clear the serial buffer.
	    lastRead = millis();        //Set a timeout period if no end character was received.
	    lastCharIndex = charsRead;  //Update the lastCharIndex to the current index.
	    digitalWrite(LED, HIGH);    //Turn on the LED to indicate the timeout period is active.
	//After the timeout, or a termination character is received, process the barcode.
	if ( (millis() - lastRead > readTimeout || terminateNow) && charsRead > 0 )  {

//Processes a received barcode.
void resetBarcode() {
    digitalWrite(LED, LOW);     //Turn off the timeout period indicator LED.
    terminateNow = false;       //Reset the special char found bool.
    barCode[charsRead] = '\0';  //Terminate the char array with null terminator.
    totalBarcodes++;            //Increment the total barcodes counter.
    Serial.print("Full Barcode Received: ");
    //Publish the barcode to where it needs to go.
    Serial.println("Publishing to required endpoints...");
    //Send barcode to enpoints...
    //http to web API.
    //Cloud publish.
    //barCode = {""};
    charsRead = 0;      //Reset the current barcode char index to the start.
    lastCharIndex = charsRead;  //Reset the lastCharindex to the current index.
    Serial.println("Waiting for next barcode...");

void resetSequence() {


	// Keep reading for 1 second to make sure read buffer is cleared
	unsigned long startTime = millis();
	while(millis() - startTime < 1000) {;

//Sends the ticket number to the API
void sendToAPI(char *ticketNumber) {
    if (WiFi.ready()) {
        //Create the http request to the API endpoint.
        char deviceID[255];
        request.hostname = "";
        request.port = 80;
        request.path = "/willcalltickets";
        sprintf(pubString, "{\"DeviceID\":\"%s\",\"PickTicketNo\":\"%s\"}", deviceID, ticketNumber);
        request.body = pubString;
        //Debugging output.
        char diagString[100];
        char hostName[50];
        char path[255];
        char body[255];
        request.hostname.toCharArray(hostName, 50);
        snprintf(diagString, sizeof(diagString), "Sending to %s\:%d%s", hostName, (char*)request.port, path);
        snprintf(diagString, sizeof(diagString), "Request Body: %s", body);
        //Send iit., response, headers);
        Serial.print("Application>\tResponse status: ");
        Serial.print("Application>\tHTTP Response Body: ");
    else {
        Serial.println("WiFi not ready. Cannot send to API.");

void sendToCloud(char *ticketNumber) {
    if (Particle.connected()) {
    else {
        Serial.println("Cloud not connected. Cannot publish to cloud.");


  1. I’m not the best coder so use at your own risk!
  2. I did not find that reading terminating characters at the end of the barcode to be reliable enough so I added a “readTimeout”. If a character hasn’t been read within the timeout milliseconds, then the barcode is assumed to be terminated.
  3. I send the data to an HTTP endpoint which is on my local network controlled by me. The data is also non-sensitive. If you need security, look at webhooks instead.
  4. The baud rate of my barcode scanner is max 9600 baud. The SerialBufferRK library isn’t really necessary but it works nonetheless.
  5. I also use the PublishQueueAsyncRK library to manage the publish rate. Again, this might not be necessary since I doubt someone could generate enough barcode scans to exceed the acceptable cloud publish rate.
  6. I do not have the Photon serial TX (blue wire in photo) hooked up to the DB9 adapter. It was causing spurious character reads on the RX port.

Link to DB9 Serial to TTL converter:

1 Like

@ninjatill thanks for sharing this, I had a couple questions:

  1. Which barcode scanner are you using and how are you powering the barcode scanner, in this case? Are you using something like this? I assume any standard handheld that can be powered via USB (with RJ45 connection on the barcode end) should work, but thought I’d double check

  2. More of a general question, is there a reason you still have a TX wire (sending to an unused breadboard position) vs. just omitting the wire completely? I’ve been doing the latter, but am new to electronics so don’t know if that’s frowned upon in some way.