Go Down

Topic: Reading serial data from the Arduino in C (Read 2028 times) previous topic - next topic


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

Code: [Select]
#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");
        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!


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

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


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 :)

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131