Pages: [1]   Go Down
Author Topic: Reading serial data from the Arduino in C  (Read 1401 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 3
Posts: 82
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to read serial data printed by the Arduino with this C program -

Code:
#include <stdlib.h>
#include <stdio.h>

int main (int argc, char *args []) {

    char test;
    FILE *serialPort;

    system( "MODE COM4: BAUD=9600 PARITY=n DATA=8 STOP=1" );
    serialPort = fopen("COM4:", "r" );

    if (serialPort == NULL) {
        printf ("Error: unable to open serial port COM4\n");
        exit (1);
    }

    while (1) {
        test = fgetc (serialPort);
        if (test == EOF) {
            printf ("End of file\n");
            break;
        }
        printf ("%c", test);
    }

    fclose(serialPort) ;

    return 0;
}

The problem is that it just stops when it gets to the fgetc line.  It doesn't quit or anything, it just sits there with a blinking cursor.  Is that because of an error with the code on my Arduino (it's just looping over 'Serial.print ("x");' over and over again), or is this method of reading data just not going to work?

Thanks very much!
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12487
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

have you checked the examples here - http://www.arduino.cc/playground/Main/InterfacingWithSoftware
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Jr. Member
**
Karma: 3
Posts: 82
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, the example for interfacing in C doesn't seem to compile in Windows though - I might take another go at it if I have time, but I wasn't really planning on spending too much time trying to get this to work.

Thanks for replying smiley
Logged

Pages: [1]   Go Up
Jump to: