How to detect if the board is connected to PC?

What is most important, in such a case my Ard program will not start logging data on SD card.

I don't care for that sort of argument; I asked a question and it looks like there is no answer to it except but 'no can do'.

Can you imaging a generic application that just listens to serial port and handles data regardless of what device is sending it?

Actually, no, I can't. Handle is such a vague term, and most likely the application would not "handle" the data in a way that I would need it to.

Is meaningless trolling how you've got your 23 posts?
Stop it please, I hate trolling.

You could try connecting an unused pin on the atmega328 to an unused pin on the atmega8u2 and then reprogramming the 8u2 to write it HIGH when it gets a usb connection.

The simplest solution is to get your logging program to just send a character when it starts up.

Be reasonable, he's trying to help you. Detecting the presence of power is no guarantee that the PC is running the application you want. Also the buffer doesn't fill up just because there is no program at the other end reading it.

You haven't answered the reasonable question: why not send a message to the Arduino to tell it so start transmitting? That is the reliable way. Of course you need to send another message to tell it to stop later.

PaulS has a high post count because he helps a lot of people. Some aren't very helpful back. For example, they ignore his questions.

Regarding your question, "why not send a message to the Arduino". Honest, I was sure I said enough to explain it, well, if not, here is my more detailed explanation to you:
I have a program that receives data from COM port (without handshake) and "handles" input stream in a special way - it displays received numbers as a long diagram, that is, ECG (not necessarily, it might be EEG, or EMG, or what not), applies different filtration and peak detection algorithms and many more. 'Handles' means 'it does a lot of things with', is it more clear that way?
I can configure input port parameters - port number, speed etc, but I cannot modify program code to generate any "data transfer request", this program acts as a passive receiver.
I am using arduino board for interfacing different sensors with that program; it is also important to provide a possibility for Ard board to accumulate data in standalone mode, without connecting to PC. So if my code could determine that there is something , anything connected to USB port, then it would transmit sensor data; if not - data is logged on SD card.
I'm afraid I can't explain this obvious requirement more clear; besides, this is probably not that important. Point is, I am relatively new to this particular board and I had some sort of illusion there might be some hidden tricks I am not aware of.

I think there was a thread a while back about detecting if you are connected to USB or not. Something to do with checking the voltage level of Vin. That may help.

I should point out that, unless you modify things a bit, connecting to the board via serial comms tends to assert RTS which resets the board. You may need to take that into account.

Another possibility is to have a switch or two on the board. Hit a button and it sends its data.

and that was exactly the post I was referring to in my first reply ... in this thread. I can refer to it again:

Moderator edit: Insult deleted.

Why are you asking then? Even so, I don't think it will be particularly reliable. Merely having power doesn't guarantee anything. I think my suggestion of a button you push when everything is connected, and the receiving program is ready, would be better.

Nick, if you could bother yourself with reading the original post, you could have noticed I was asking for solutions other then two most obvious ones. And with obvious reason as well: first one - I cannot modify program code, second one - hardly reliable.

Keep on insulting people, and see what happens.

Wow!
Don't be so important please.
Where is an insult? The mere fact I pointed out you are giving replies without reading original question?
Ban me, it will make you happy.

Stan09:
Are there any programmatically only alternatives?

Can we just say "No" and be done with this?

Perhaps try to think outside of the box.. dont detect it.
You might write to both usb and to the SD you do it in a circular logging way. (FILO)
Circular logging is sometimes used in databases to have some time window to be able to restore data
Ofcourse its not the best.. but its better then nothing, and it might be all you want.
That is if you do not have to much data (or else your SD will be full quickly)..but it will always overwrite oldest data (so there is a time limit of how much data can be recovered back in time)..
Also keep in mind how often you can rewrite your SD...
If it is important data get it to a networked file cluster or so ..

Not sure how SD storage works (i dont have it)
if you have a file system on it, then keep a pointer to oldest write somewhere, and update it once you overwrite your oldest stored data samples. (circular logging).
When connected to PC perhaps also store a date time on the SD
Perhaps use clock shield or so.

You might have 2 arduino's around, another one to replay a SD to your compter, to restore data
or write a program that replays trough a comlink to fool the original computer program

PS for medical data it might not be wise to use an arduino, see its license its not to be used in environments that could change the dead or alive situation of a carbon based life form..or something like that.
Ask yourself what is the cost if your solution would fail, its not foolproof hardware it will brakedown someday

PS It might interest you to read about atomic databases (no its not about physics) >Exchange uses it.

1 Like

The only solution I can see, given those constraints, is to have a second Arduino powered off the USB which acts as a bridge between the logger and the PC; you can arrange for this to give a handshake with the logger so that the logger knows whether it is there and hence whether the USB is powered up. Of course we have to assume that power on the USB means that the USB interface is fully functional, because you have ruled out every way I can think of to confirm that.

Not a brilliant solution, but the only one I can think of.

PGTBOOS:
Perhaps try to think outside of the box.. dont detect it.
You might write to both usb and to the SD you do it in a circular logging way. (FILO)

Well, first of all it was just "nice to have", and as I can see now, there are no simple ways, so heck with it.

PGTBOOS:
PS for medical data it might not be wise to use an arduino, see its license its not to be used in environments that could change the dead or alive situation of a carbon based life form..or something like that.
Ask yourself what is the cost if your solution would fail, its not foolproof hardware it will brakedown someday

it costs nothing at all because there is also a "science". And even more to this, it is about mostly exercising... like, 'your zone' of Polar which actually has purchased the idea for cash from "unnamed" (Eastern European) source.
This is just my hobby... connect Arduino board to the living being, collect some data and tell when exactly that "living being" is gonna bust away :slight_smile:

Friend, I'm not sure about what do you want. But when I need to check if PC and Arduino is connected I use this.

String serialnumber;
void setup() {
Serial.begin(9600);
Serial.println ("Data between Arduino and PC ok!");
int sensorValue = analogRead(A0);
pinMode (8, OUTPUT);
pinMode (9, OUTPUT);
be continue........

But when I need to check if PC and Arduino is connected I use this.

Which only assures that the Arduino was able to connect to one end of the serial port. There is nothing there that assures that there is anything on the other end of the port.

vpinho13:
Friend, I'm not sure about what do you want. But when I need to check if PC and Arduino is connected I use this.

String serialnumber;
void setup() {
Serial.begin(9600);
Serial.println ("Data between Arduino and PC ok!");
int sensorValue = analogRead(A0);
pinMode (8, OUTPUT);
pinMode (9, OUTPUT);
be continue........

Are you saying that if Serial.begin does not fail this along proves it is connected?
Certainly not.
The have such a functionality for Ard Leonardo, though, as I stated in my initial post, that is a clause 'if(serial)'