Read a port in C

Master_Chang Posted messages 30 Registration date   Status Member Last intervention   -  
sambia39 Posted messages 610 Registration date   Status Member Last intervention   -
Hello

I would like to analyze information sent by an Arduino on the COM port, not really mastering C, so I’m turning to you to advise me on such or such commands. I know Google is my friend but I don’t understand much of what’s on the various forums. For info I’m on Windows 7 and I tried a similar batch approach but there isn’t a clean solution in that language.

Thank you

1 answer

  1. sambia39 Posted messages 610 Registration date   Status Member Last intervention   50
     
    Hello
    There isn’t really a command to run in a Windows terminal and interfacing it with your application, or even tinkering to redirect the stream to your application or vice versa.
    The best thing to do is to write a function that will allow you to communicate directly with the hardware in question, so you basically need to implement more or less an interface “causète” with your hardware that must take:
    -> the serial port address
    -> the write speed and a variable "buffer" to write commands and receive them.

    In short, this would give an example below that I’m posting and which is not optimized; rest of the instructions are up to you to do according to your needs and do some documentation on the structure "termios" to really know what you want to do because my example is used for a personal application so some configuration parameters of “termios” are useful to my application but not necessarily yours.
    See you soon

     //libC #include <stdio.h> #include <stdlib.h> //Système #include <unistd.h> #include <termios.h> #include <sys/fcntl.h> #define BUFF 1024 //Function connection void f_ConnexionCom( const char *pCom, const unsigned Baud ){ int flag = 0; struct termios OldT; struct termios NewT; flag = open( pCom, O_RDWR|O_NOCTTY ); if( flag <= 0 ){ printf( "*\tImpossible d'ouvrir le port série\n" ); printf( "*\tAssurer vous que vous avez les droits administrateurs\n" ); perror( "*\tEtat Erreur" ); exit( EXIT_FAILURE ); } /** * Save & Create a * new termios to use **/ tcgetattr( flag, &OldT ); tcgetattr( flag, &NewT ); //Config of termios personalize (&NewT)->c_iflag = IGNBRK; (&NewT)->c_oflag = 0L; (&NewT)->c_lflag = 0L; (&NewT)->c_cc[VMIN] = 1; (&NewT)->c_cc[VTIME] = 0; (&NewT)->c_cflag |= CREAD; (&NewT)->c_cflag &=~ ( CSIZE|PARENB|CSTOPB|CRTSCTS ); (&NewT)->c_cflag |= CS8; (&NewT)->c_cflag |= CLOCAL; (&NewT)->c_iflag &=~ ( IXON|IXOFF|IXANY ); //Baudrate configuration example 19200 cfsetispeed( &NewT, Baud ); cfsetospeed( &NewT, Baud ); tcsetattr( flag, TCSANOW, &NewT ); tcflush( flag, TCIFLUSH ); //Allocate command variable char *pCmd = (char*)calloc( BUFF, sizeof(char) ); if( pCmd == NULL ){ perror( "Impossible de d'initialiser la variable de commande\n" ); exit( EXIT_FAILURE ); } /** * Send command on * the serial/Com port * not optimized the exit condition * (to redo) **/ do{ int ret; //Send command printf( "Entrer votre commande\t: "); scanf( "%c", pCmd ); printf("\n"); //Read information ret = write( flag, pCmd, sizeof(pCmd) ); if( ret ){ printf( "# Attente de lecture.....\n"); ret = read( flag, pCmd, 1 ); if( ret ){ printf("=>Reçut\t:%s\n", pCmd ); pCmd = 0; } if( ret <= 0 ){ printf( "\n *\tAucune donné reçut\n" ); return; } } }while( *pCmd != '@'); //dummy exit condition /** * Free & Restore the * initial Termios **/ free( pCmd ); tcsetattr( flag, TCSANOW, &OldT); close( flag ); } //Main function int main( void ){ /** * Warning here I use the terminal * Ctrl+Alt+F1 on UNIX to test this * and I do not recommend it. use the address of * your serial port and make sure you are in "su" mode **/ f_ConnexionCom("/dev/tty1", 19200 ); return ( EXIT_SUCCESS ); } 


    --
    Toute connaissance est une réponse à une question.
    0