1

I am building some sensors with Arduinos, which each have a unique integer address from 1-9 (This address is an int variable stored in a struct 'Matrix', the variable name is Matrix.Address). The sensors will all be connected to a PLC via SoftwareSerial (BitBanging) through an RS232 converter. Since all of the Sensors will be connected to the same RX & TX ports on the PLC, I need a way to address each sensor through a unique string.

My plan is the following:

*If the PLC wants a reading from sensor #1, it will send the string: "1,read" All of the sensors should receive this string, but I only want the sensor with address '1' to respond.

*What I am trying to do is come up with a line of code similar to the following:

    if (plcCommand == (Matrix.Address + ",read<CR>")) {
  mySerial.print(GenerateEquation());

Basically, I need to insert that 'Matrix.Address' variable into a string which will be compared to the incoming string.

It seems like a very simple thing to do, but I am having trouble coming up with a solution.

Any help would be appreciated!

1
  • what type are those variables?
    – Juraj
    Commented Nov 5, 2017 at 19:25

3 Answers 3

1

You're thinking backwards. Instead of building up one string and comparing it to another, you should instead be splitting down the string you are being sent and dealing with the different parts of it separately.

Say you are being sent the string 32,read\r. Instead of making another string 32,read\r and comparing it to what you have received you should instead be splitting the incoming string up.

That way you then have a string containing 32 which can be converted to an integer, and a string containing read. It now becomes very simple to do your comparisons without messing with building strings in RAM wasting precious memory.

0

it will send the string: "1,read"

Fairly simple:

1) the dumbest would be to use serial print to print the number, and then ",read".

2) a close 2nd (in terms of dumbness) is to use s/printf(): simple but costly on a mcu.

3) a smarter approach is to convert the number to a string - you can use standard c routines if your compiler supports them, or hand-roll your own.

4) if the number of sensors are limited, a look-up table or a switch/case statement would work well.

0

If the PLC wants a reading from sensor #1, it will send the string: "1,read" All of the sensors should receive this string, but I only want the sensor with address '1' to respond.

Is your question about sending the "1,read". If you're using a serial line like you say then there is no difference between sending that as a single string in one print command or sending first the number and then the text part using two print commands back to back. The exact same thing will end up going out over the wire either way. So just use two separate print commands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.