Offloading serial polling onto Arduino

Hello all! This is my first post here on the forums so please bear with me while I learn.

For my first project I would really like to be able to put my serial polling workload onto the Arduino. I would like it to be able to poll a device via Serial1 until it gets a change in value and then send that change back to my PC through a different serial (Serial2). I basically only want the PC to receive values if they have changed. Does this seem like a suitable idea to try and implement?

Ok, so break it up into bits.

  1. You want to check if there is a character available on Serial port 1 - Serial1.available() to the rescue.
  2. If there is a character, you want to read it and check if it is not equal to the last one.
    2a) That means you need to also remember what the last character was, so there needs to be a variable to save it into.
    2b) You want to compare both the last character and the new character, and if they are the same send it back to the serial port.

psuedo code:

IF character available
  read character
  IF character != last character
    last character = character
    print character
  End
End
Repeat

Solution: (try to make your own based on the hints, and then compare with below - highlight it to see the text)
void loop()
{
static char lastCharacter = 0;
// This code loops consecutively
if(Serial1.available()){
char character = Serial1.read();
if (character != lastCharacter){
lastCharacter = character;
Serial2.write((byte)character);
}
}
}

jford128:
I would like it to be able to poll a device via Serial1.

When you write 'poll a device' do you mean read the output and discard any repeated characters?

How do you envisage the device being connected to the Arduino, and the Arduino to the PC? If the device is designed to be connected directly to the PC via USB then I guess you would need to provide a USB Host port on the Arduino, and then have the Arduino emulate whatever type of USB device the real device is. But perhaps that's not what you're trying to achieve. Could you clarify?

When I say "polling" what I mean is sending a serial command that will query the device (Serial1) which in turn makes the device send a response back with the variable I want to push through to the (Serial2). The device is meant to be attached to the PC or control module via a serial cable so the connection will be as such. (Controller <---serial cable---> Arduino <---serial cable---> Device

The reason I am doing this is to take the polling workload off of the main controller because it has plenty of other functions it needs to be doing.

I'm starting to get the idea. I guess that 'serial cable' means TTL or RS232 or something, and not USB?

Yes, exactly. It's rs232.

I want to be able to send a query such as:
"#SPA ?"

Which will give me a response of:
"!00 SPA = x"

Where x is 1 or 0

Tom has the basic idea of what I want.

any help implementing my code into what he currently has structured would be greatly appreciated.

In that case I'd add a bit to Tom Carpenter's suggestion:

You need to decide how often to poll the device, and poll it at that interval. The Blink Without Delay example sketch shows you how to do that. In your case, instead of turning the LED on or off you would send your command string to the device.

Let's suppose the device replies with the response you expect. The response will arrive at the serial port, one character at a time. You need to read each character from the serial port as it arrives, and store it in a local buffer. Generally, you'd accumulate the whole string in a buffer and then parse it when you knew you had a complete message. In this case since you only want the very last character in the message and know that the last character will be a CR, you could just hang on to the last character and when the next character is a CR you can then process the last character.

Then you're back to Tom's solution of deciding whether the new result is different to the previous one and so on.