Go Down

Topic: Interfacing with Matlab, reset of arduino on fopen (Read 375 times) previous topic - next topic

xauni

Dear all,

I currently try to build a remote control for a laboratory current amplifier. I would need the arduino to persist its state when a computer is connecting. Is this possible? In the following, I will post minimal examples of sketch and matlab software to reproduce the loss of state. 

Sketch:
Code: [Select]

bool state = true;

void setup() {

  Serial.begin(9600);
  Serial.setTimeout(50);
}


void loop() {

  if (Serial.available())  {

    const char c = Serial.read();
   
    if (c == 105) {  // type an "i" to switch on
      state = true;
    }

    if (c == 111) {  // type an "o" to switch off
      state = false;
    }
 
    if (c == 115) {  // type an "s" to get state
      Serial.println((int) state);
    }
   
  }
}


Matlab:
Code: [Select]

instrreset();
s = serial('COM4','BaudRate',9600,'Timeout', 50/1000);

fopen(s);

pause(2); //gives timeout error if serial is accessed too early

fprintf(s,'s');
fscanf(s)  // returns initial state 1

fprintf(s,'i'); // sets state to 0

fprintf(s,'s');
fscanf(s) // returns state 0

fclose(s);

fopen(s); // seems to reset arduino

pause(2);

fprintf(s,'s');
fscanf(s) // reads initial state 1

fclose(s);


The problem was also discussed in thread
http://forum.arduino.cc/index.php?topic=353861.0

From that thread the behavior seems to be intended, but is there any way to prevent that?

Thank you for your help!

Juraj

you connect once and hold the connection open all the time. then you read and send data without waiting
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

xauni

Thank you for the answer,

unfortunately, this is not the use case. It is a state sensitive application, where a reset of the state might results in the loss of days of work. Currently we do it exactly like that. A computer controls a NI digital IO module
http://www.ni.com/de-de/support/model.usb-6501.html
which drives the TTL input bits of the amplifier. When the remote computer is switched off, or even the controlling software resets, this results in a fatal loss of state.

I wanted to use the arduino as an always "on" (power served by amplifier) system to persist the state. If the initialization of the connection results in loss of state, this seems to be the wrong way to go.

Does anyone have an idea, how to persist state with the arduino? EEPROM also seems to be not possible, because I guess, that the arduino reset also resets the digital output ports until the state is recalled.

Juraj

https://forum.arduino.cc/index.php?topic=363128.msg2503831#msg2503831
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

Paul_KD7HB

Thank you for the answer,

unfortunately, this is not the use case. It is a state sensitive application, where a reset of the state might results in the loss of days of work. Currently we do it exactly like that. A computer controls a NI digital IO module
http://www.ni.com/de-de/support/model.usb-6501.html
which drives the TTL input bits of the amplifier. When the remote computer is switched off, or even the controlling software resets, this results in a fatal loss of state.

I wanted to use the arduino as an always "on" (power served by amplifier) system to persist the state. If the initialization of the connection results in loss of state, this seems to be the wrong way to go.

Does anyone have an idea, how to persist state with the arduino? EEPROM also seems to be not possible, because I guess, that the arduino reset also resets the digital output ports until the state is recalled.
You are hinting you do not have an adequate UPS on the system. IS this the case?

Paul

xauni

Thank you Juraj,

I frequently read, that the 10μF capacitor should not be a permanent solution. Can I expect any damage from long term usage? Is there something similar to  post #2 in
https://forum.arduino.cc/index.php?topic=363128.msg2503831#msg2503831
on the Arduino nano V3.3 like cutting a track (I guess, it means the DTR (Data Terminal Ready) track)?


Thank you Paul,

I would say we have an adequate UPS (Uninterruptible Power Supply), which is none. If there is no power, we have much bigger problems than the amplifier gain. A power down occurred twice during the last 7 years and a couple of tens of thousands euros is just not worth it. A windows update on the remote computer (windows updates are disabled by default, just think of something similar which might unexpectedly interrupt a non-embedded system) is much more frequent.

This is why I want to transfer the state management to an embedded system.

Christian

Robin2

A simple solution may be to use a USB-TTL cable to connect from the PC to the Arduino connected directly to the Rx Tx and GND connections on the Arduino.

When the PC tries to talk to the Arduino that way it will not cause the Arduino to reset.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

xauni

Thank you very much,

I will consider both, disabling auto reset by capacitor and connecting via additional USB/TTL converter.

Would the additional USB/TTL converter allow for long distances between converter and Arduino, since the signal is 9600 Baud only? This would be my second problem, that USB would not allow for for the necessary 10m connection. I read about RS485, but the additional shield would probably exceed the tolerable power consumption (50mA at 12V).

Christian

Robin2

I will consider both, disabling auto reset by capacitor and connecting via additional USB/TTL converter.
If you get the USB-TTL cable then you won't need any capacitor.

Quote
Would the additional USB/TTL converter allow for long distances between converter and Arduino,
It should work exactly the same as any USB cable. I regularly use a 2m USB extension cable in addition to the common 1.5m cable. What distance do you need?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

xauni

What distance do you need?
It is going to be around 10m. Active USB repeaters are an option, but I could not find shielded ones and I fear electromagnetic radiation. There is an online shop selling a 10m passive extension and claims it works for USB2.0 due to high cable quality.

Christian

Paul_KD7HB

In my estimation, you are just digging a deeper hole for your project. The bottom line is you have to have a communication protocol that allows for errors and error recovery.

The protocol has to be the Arduino does not consider the message transmission complete until it gets and acknowledgement from the PC that the message was correctly received. Until then, the Arduino must be ready to resend the last message the next chance it gets.

So, to get this to work, the PC must POLL the pc for a message and ACKnowledge the receipt. In addition, the message may need to have a CRCC to ensure the text of the message has not been corrupted.

Paul

Robin2

It is going to be around 10m.
There was no mention of that in your Original Post and, to be honest, I can't see how the Title of your post is relevant to the question of sending data 10m.

You could try connecting a long 3-wire cable (Tx Rx and GND) from a USB-TTL converter to your Arduino and see if that works sufficiently well.

If it does not then I think you need the appropriate hardware at each end so you can use an RS485 connection.

Or maybe you could use a wireless connection using Bluetooth? Most PC's have Bluetooth. Basic Bluetooth is just serial-by-wireless.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

sterretje

I would go for RS232 at that distance.

Get a MAX232 breakout board and connect the TTL side to the Arduino.

Get a USB-to.RS232 cable for the PC side.

Make / buy a 10 meter RS232 cable.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Robin2

I would go for RS232 at that distance.
I had not thought of that. I have tended to think of RS232 as yesterday's solution, but obviously it still has its uses. And should be simpler than RS485

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up