10 years, 6 months ago.

why stop the filestream when I send a char?

I have a FileStream, I am sending via a serial interface. But if I send a char (or any thing else) by the receiving station (Putty), the filestream stopes.

edit: If i send an "e"...the engine begins to turn and the filestream stops. Why?^^

Sending filestream (now)

    pc.baud(115200);
    pc.format(8,Serial::None,1);

        if(pc.readable()) {   // Check if data is available on serial port.
            key = pc.getc();  //collects data

            if(key=='e')  //e=clockwise rotation
{
            clockw =  true;
            aClockw = false;
}

if(outputCounter%2000 == 0) {
            short pc_id = 0xCDAB; // ID
            short pc_count = 0x0004; // Channel-count * 2
            short pc_value1 = (short)(currentPos/100.0f);
            short pc_value2 = (short)(avgBufSmall->GetAverage()*100f);
            fwrite(&pc_id, sizeof(short), 1, pc);
            fwrite(&pc_count, sizeof(short), 1, pc);
            fwrite(&pc_value1, sizeof(short), 1, pc);
            fwrite(&pc_value2, sizeof(short), 1, pc);
}

outputCounter++;

In a previous program, I sent only printf statements. If I sent as a sign by the receiving station (Putty), the data transmission doesn't stopped.

Sending filestream (old)

    pc.baud(115200);
    pc.format(8,Serial::None,1);

        if(pc.readable()) {   // Check if data is available on serial port.
            key = pc.getc();  //collects data
            if(key=='e')
                ekey=true;

            if(key=='e')  //e=clockwise rotation
{
                pc.printf("\x1B[5;1HX");
                clockw =  true;
                aClockw = false;
}

if(outputCounter%2000 == 0) {
            pc.printf("\x1B[8;14H%0.1f%% ",(float)(currentPos/10000)); 
            pc.printf("\x1B[9;14H%0.2fmA "avgBufSmall->GetAverage();
}

outputCounter++;

Sending filestream (new2)

    pc.baud(115200);
    pc.format(8,Serial::None,1);

        if(pc.readable()) {   // Check if data is available on serial port.
            key = pc.getc();  //collects data

            if(key=='e')  //e=clockwise rotation
{
            clockw =  true;
            aClockw = false;
}

if(outputCounter%2000 == 0) {
            short pc_id = 0xCDAB; // ID
            short pc_count = 0x0004; // Channel-count * 2
            short pc_value1 = (short)(currentPos/100.0f);
            short pc_value2 = (short)(avgBufSmall->GetAverage()*100f);
                           
                pc.putc(pc_id & 0xFF);
                pc.putc((pc_id >> 8) & 0xFF);
                
                pc.putc(pc_count & 0xFF);
                pc.putc((pc_count >> 8) & 0xFF);
                
                pc.putc(pc_value1 & 0xFF);
                pc.putc((pc_value1 >> 8) & 0xFF);
               
                pc.putc(pc_value2 & 0xFF);
                pc.putc((pc_value2 >> 8) & 0xFF);
}

outputCounter++;

This works...but why not with frwite...it should be the same...?is serial.h/stream.h fucked?

While I have no idea why it stops (or what your program exactly does), why do you use a filestream instead of just printf/putc?

posted by Erik - 28 Oct 2013

The indenting is inconsistent, and the bracket counts don't match.

Also the second example uses another stream "com" and you have not included its declaration.

posted by Oliver Broad 07 Nov 2013

yes i renamed the stream identifier... I rewrote it for my question..had fixed it. add a new solution.."new2"...it works BUT WHY fwrite dont?

posted by q qq 08 Nov 2013

2 Answers

q qq
poster
10 years, 5 months ago.

my Program is a simpel get-analog-data-from-an-engine-and-send-it-to-PC-AND-controlling-the-engine-have-to-be-also-possible- -Program. with receiving chars i set some I/O for motordriver-control.

On PC side i use Visual C# Form to record enginedata.

10 years, 5 months ago.

I think the "if" statement at line 13 only executes if the condition at line 4 (pc.readable())is true. That probably wasn't your intention but if you follow the curly braces that's how it reads. That means the "fwrite" only executes if at least one character (any character) has been sent.

I would guess that fwrite is blocking. I'm not even sure that pc is the correct type to pass to fwrite, they seem to be entirely different I/O models.

I'm going to try to clarify that: I think there's a difference between the old "C" way of doing things and the new "C++" way. fwrite is a "C" operation and takes a "C" file handle. You would need to open the device using fopen, but I think fopen is only implemented for storage devices. I thought it was unimplemented but the MBED flash FS library appears to implement it. I think the C++ version if it exists would be pc.write or pc.fwrite.

You probably ought to use pc.putc and send it byte by byte. Unfortunately my C++ is no way good enough to code it right now but if you want uninterrupted execution you need to check pc.writeable() before sending each byte.

edit.wrong button (del pls if possible)

posted by q qq 08 Nov 2013

I hope someone can clarify this, I don't know the mbed well enough to answer exactly though I think I see what's gone wrong.

posted by Oliver Broad 08 Nov 2013