Unable to read data over serial USB from certain software

I’m trying to make a serial cash drawer activator using an Arduino Uno. It’s a pretty simple project that just requires the Uno to watch for a character code on the serial interface and energise the cash drawer’s solenoid for a split second when it receives it.

I have it working for the most part, with the Uno attached via it’s USB cable I can send it the code using Putty and it opens the cash drawer correctly.

The point of sales software that I need it to work is a different story however, as far as I can tell the Uno doesn’t receive any data I send from this software. The port settings are all the same as Putty’s and if I sniff the port traffic it looks like the code is being successfully sent.

Unfortunately I’m not experienced enough with serial communication to figure out why the Uno is unable to read the data and was hoping someone might be able to point me in the right direction. I’ve attached sniffer logs of the port data in case that helps.

Here’s the code I have loaded in the Uno itself, it’s currently set to trigger on an ‘a’.

int drawer = 3; // the pin the cash drawer is connected to
unsigned int lastTrigger = 0;

void setup() {
  pinMode(drawer, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if((millis() - lastTrigger) > 100) {
    digitalWrite(drawer, LOW);
  }
}

void serialEvent() {
  while (Serial.available()) {
    byte value = Serial.read();

    if(value == 97) {
      digitalWrite(drawer, HIGH);
      lastTrigger = millis();
    }
  }
}

Logs.zip (3.62 KB)

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

…R

Cheers Robin,

I tried changing my Arduino code to the following as the post suggests:

int drawer = 3; // the pin the cash drawer is connected to
unsigned int lastTrigger = 0;

void setup() {
  pinMode(drawer, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if(Serial.available() > 0) {
    if(Serial.read() == 97) {
      digitalWrite(drawer, HIGH);
      lastTrigger = millis();
    }
  }

  if((millis() - lastTrigger) > 100) {
    digitalWrite(drawer, LOW);
  }
}

Unfortunately it still behaves the same way. It will open the drawer if I send the character through Putty but will not when the point of sale package sends it.

It really is just sending a single character, so the simplest example in the post you linked to should work, but it isn't.

One thing to note is that the Uno is designed to reset when a serial connection is opened. So if the POS software opens the serial connection, then immediately sends the data, the Uno will miss it because it will be in bootloader mode. This reset is done by the DTR signal. If you can configure that behavior, you can prevent the reset. You can also disable the reset in hardware, either by connecting a 10 uF capacitor between the Uno's Reset and GND pins or by cutting the "RST-EN" jumper on the board. The reason for this auto reset behavior is to make it easy to upload sketches to the Uno. If you have it disabled, you'll need to manually reset the Uno at just the right time (right when the sketch size is displayed in the IDE's console pane) during the upload.

nalyden:
I tried changing my Arduino code to the following as the post suggests:

It's easier to help if you use the code as presented in the Tutorial - at least until you get things working.

In view of the problem you are having you need to use SoftwareSerial to create another line of communication and connect your PC to the SoftwareSerial pins using a USB-TTL cable. That way you can use softwareSerial.print() to show on the Arduino Serial Monitor the actual character than your Arduino is receiving from the cash drawer.

...R

PS ... @pert has made a very good point which I had not considered.

Cheers @pert, you were spot on.

I'd even noticed the POS software was opening and closing the COM port each time it sent the code and read about the Uno resetting when the COM port is opened but failed to put two and two together. I knew I'd missed something simple.

And cheers to you as well @Robin2, thank you both for your time.

You're welcome. I'm glad if I was able to be of assistance. Enjoy!
Per