Thread: Read by Serial Communications

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    2

    Read by Serial Communications

    Hi.

    I read correctly in MatLab some data by serial port of a device, but in C... nothing.

    Code:
    #include <cstdlib>
    #include <stdio.h>
    
    int main(void)
    {
        
        FILE *in; 
        char linha[30];
        
        while(1)
        {
        
            // Abrir porta COM de onde se ligou a placa
            in = fopen( "COM8", "r" );
            if( in == NULL )
            {
                printf("ERRO: não consigo abrir porta COM\n");
                exit(1);
            }
        
            // Ler de um ficheiro e escrever no outro
            while( fgets(linha, 30, in) != NULL )
            {
                printf("%s",linha);    // Mostrar no terminal
            }
    
        }
    
        // Fechar os ficheiros
        fclose( in );
    
        return 0;
    }
    The message "ERRO: não consigo abrir porta COM\n" don't appear, so I haven't error there. I just do not see anything in the terminal.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,077
    Did you stop the comport usage by the other program.
    Normally only a single program can be using a comport at the same time.

    Edit: I still use the old "mode" command from the command prompt to see if the comport is free.

    Code:
    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
    
    mode
    
    Status for device LPT1:
    -----------------------
        Printer output is not being rerouted.
    
    
    Status for device COM1:
    -----------------------
        Baud:            1200
        Parity:          None
        Data Bits:       7
        Stop Bits:       1
        Timeout:         OFF
        XON/XOFF:        OFF
        CTS handshaking: OFF
        DSR handshaking: OFF
        DSR sensitivity: OFF
        DTR circuit:     ON
        RTS circuit:     ON
    
    
    Status for device COM2:
    -----------------------
        Baud:            1200
        Parity:          None
        Data Bits:       7
        Stop Bits:       1
        Timeout:         OFF
        XON/XOFF:        OFF
        CTS handshaking: OFF
        DSR handshaking: OFF
        DSR sensitivity: OFF
        DTR circuit:     ON
        RTS circuit:     ON
    
    
    Status for device CON:
    ----------------------
        Lines:          240
        Columns:        80
        Keyboard rate:  31
        Keyboard delay: 1
        Code page:      437
    In my case COM1 and COM2 are not being used.

    Tim S.
    Last edited by stahta01; 10-18-2014 at 12:11 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    2
    Read by Serial Communications-porta8-jpg

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,800
    And is the device at the other end of the serial line sending data at 1200 baud, 7 bits, 1 stop bit, no parity?

    If it isn't, then simply calling fopen() won't work. It won't understand the data being sent to it.
    Check your matlab serial port config to see how it configures the port, then you need to do the same thing in your C code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serial Communications w/API
    By john6304 in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2005, 07:27 AM
  2. Serial Communications with win32 api
    By Blackthorne in forum C Programming
    Replies: 1
    Last Post: 01-26-2003, 12:45 PM
  3. Serial communications problem.
    By papudi in forum C Programming
    Replies: 1
    Last Post: 10-11-2002, 04:23 PM
  4. Serial port read..can someone tell me..
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-27-2002, 08:21 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM

Tags for this Thread