Arduino code to write and send at the same time simultaneously

I am working a graphical blocks for Scilab (software similar to MATLAB) simulation environment. There should be blocks called AI (analogue input), AO, DO. Also, there will be two programs: 1) on the side of Scilab, one which sends and receives data; 2) on the side of Arduino, one which sends and receives data.

Right now I am working on Arduino side code. It, the code, should read voltage values from 6 inputs and sends them to Scilab via serial and simultaneously write 1/0 to its outputs when it gets data from Scilab. In my understanding, data sent/received should have information about pin number (to know what pin it should affect) and value itself. Also, it should read and receives at the same time.

Any ideas how I could do it? Help would be much appreciated. Thank you.

Since you only have one processor it can't do both jobs at the same instant, but computers rarely work that way. What you would do is monitor you communication channel for messages and respond to them as they arrive, and also monitor the state of the relevant inputs and do whatever you need to when they change - sending an update message, for example. If you need to do anything on a timed basis, you would monitor the passage of time too and carry out the timed actions when they were due.

So, basically your sketch spends its time looking for something that needs doing, does it, and then goes back to looking for the next thing that needs doing.

PeterH:
Since you only have one processor it can't do both jobs at the same instant, but computers rarely work that way. What you would do is monitor you communication channel for messages and respond to them as they arrive, and also monitor the state of the relevant inputs and do whatever you need to when they change - sending an update message, for example. If you need to do anything on a timed basis, you would monitor the passage of time too and carry out the timed actions when they were due.

So, basically your sketch spends its time looking for something that needs doing, does it, and then goes back to looking for the next thing that needs doing.

Thank you. Yes but let's assume the monitor of inputs notices state change in inputs and sends update message but this message, as it is going via serial, is caught by the other monitor which monitors channel. Here comes conflict.

flighttothemoon:
this message, as it is going via serial, is caught by the other monitor which monitors channel. Here comes conflict.

I can't make sense of that. What's the 'other monitor' you're referring to? If you mean the part of your sketch which processes messages from Scilab, that only happens when your sketch decide to check for messages. It only does one thing at a time, and that's all it needs to do.

It makes sense. Yes, one at a time. You are right. Also, how can I monitor communication channel for messages? Serial.available >0 ? Sorry, I am new to this.

Have you worked through any of the example sketches in the IDE? There are several that relate to serial communication.

Yes, Serial.available() is the method you're looking for.
If commands are one character long, you can use code like this (silly example, I know :slight_smile: )

#define CMD_VERSION 'V'        // let's pretend CMD_VERSION means "tell me the code version"
#define CMD_OTHER 'k'

void loop() {
    char ch;               // we'll use this to read characters from Serial one at a time

    // other code...

    while (Serial.available() > 0) {    // loop until you have no more characters waiting in the buffer
        ch = Serial.read();

        switch (ch) {
            case CMD_VERSION:
                Serial.println("V1.0");
                break;

            case CMD_OTHER:
                // code to execute the other command
                break;
        }
}

If you open the serial monitor and send a V to the sketch, it'll respond with "V1.0".

Another technique uses a buffer (i.e. char array) to store characters until a particular one is found (for example newline or #). That character has the conventional meaning of "end of command". When you see it, you know you the command string is complete and you can start to work on it (for example search a particular substring, extract numbers, etc.).

HTH