UART C/C++ program need help.

2,729 views
Skip to first unread message

Andrei

unread,
Dec 6, 2013, 7:44:59 AM12/6/13
to beagl...@googlegroups.com

Hello, I need a help with the UART, more precisly with the c code. When the following c script goes to write () function, it sends the data over to the uart and I can see the output on the oscilloscope, but the problem is that it sends it in ASCII, which is not what I want it to do. on the Linux shell I usually used "echo -en \\xAA > /dev/ttyO4"  and it did the job. But I want it to the job using the C code.  Hopefully somebody can help me. 
#include <iostream>

#include <cstdio>

#include <unistd.h>

#include <stdlib.h>

#include <string>

#include <termios.h>

#include <fcntl.h>

#include <sys/signal.h>



#define BAUDRATE B9600

#define DEVICE "/dev/ttyO4"

#define _POSIX_SOURCE 1



#define FALSE 0

#define TRUE 1

volatile int STOP=FALSE;

int wait_flag=FALSE;



using namespace std;



void signal_handler_IO (int status){



    cout << "received SIGIO signal "<< endl;

    wait_flag = FALSE;

}



int main()

{

    int fd, c, res;

    struct termios oldtio, newtio;

    struct sigaction saio;

    char buffer [255];



    system ("echo BB-UART4 > /sys/devices/bone_capemgr.8/slots");



    fd = open (DEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);

    if (fd <0) {perror(DEVICE); exit (-1);}



    /*install the siganl handler before making the device ansynhronous */

    saio.sa_handler = signal_handler_IO;

    saio.sa_mask.__val[0] = 0;

    saio.sa_mask.__val[1] = 0;

    saio.sa_mask.__val[2] = 0;

    saio.sa_mask.__val[3] = 0;

    saio.sa_flags = 0;

    saio.sa_restorer = NULL;

    sigaction(SIGIO, &saio, NULL);



    /* Allow the process to receive SIGIO */

    fcntl(fd, F_SETOWN, getpid());

    /* Make the file descriptor ansynchronous */

    fcntl(fd, F_SETFL, FASYNC);



    tcgetattr(fd,&oldtio);



    newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;

    newtio.c_iflag = IGNPAR | ICRNL;

    newtio.c_oflag = 0;

    newtio.c_lflag = ICANON;

    newtio.c_cc[VMIN]=1;

    newtio.c_cc[VTIME]=0;

    /* Clean the modem line and activate the settings for the port */

    tcflush(fd,TCIFLUSH);

    tcsetattr(fd,TCSANOW,&newtio);



     //------------ WRITE



 c = write (fd, "AA", 4);
            if ( c < 0)
                fputs ("write() of 4bits failed!\n", stderr);



    //--------------READ



   

    while(STOP==FALSE) {



        usleep(1000000);

        putchar ('*'); fflush(stdout);



        if(wait_flag==FALSE) {

          buffer[res]=NULL;

          res = read (fd,buffer,sizeof(buffer));



            printf(":%s :%d\n", buffer, res);

            if (res==1) STOP=TRUE;



            wait_flag = TRUE;

          }



    }





    //restore old port settings

tcsetattr(fd,TCSANOW, &oldtio);



    close (fd);

    return 0;

}


Andrey Nechypurenko

unread,
Dec 6, 2013, 8:25:24 AM12/6/13
to beagl...@googlegroups.com
Hi,

> write (fd, "AA", 4);

you are sending string, not a binary data. Try the following instead:

char buf[] = {0xAA};

write (fd, buff, sizeof(buff));

HTH,
Andrey.
> --
> For more options, visit http://beagleboard.org/discuss
> ---
> You received this message because you are subscribed to the Google Groups
> "BeagleBoard" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to beagleboard...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Andrei Kondratjev

unread,
Dec 16, 2013, 4:07:08 AM12/16/13
to beagl...@googlegroups.com
Hi,

Thanks for help Andrey, do you happen to know how to display binary value READ function . At the moment when I use read function, shell displays corresponding ascii symbol (character) as the read. If I send 0x43, beaglebone black read is displaying the 'C' on a shell as a read in, but is there a way of displaying hexadecimal on binary value on the read.

Thanks,


You received this message because you are subscribed to a topic in the Google Groups "BeagleBoard" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/beagleboard/I26rrzcpr-g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to beagleboard...@googlegroups.com.

Dhanushka Jayaweera

unread,
Dec 24, 2013, 8:46:21 AM12/24/13
to beagl...@googlegroups.com
hey, can u provide a link / code for the read function? 
thanks

kpha...@gmail.com

unread,
Dec 24, 2013, 1:10:37 PM12/24/13
to beagl...@googlegroups.com
If you are using iostreams use the iomanipulator hex when outputting the character read.

cout << hex << *buf;

If using printf ...
printf"0x%x", *buf);

Both versions will output a single character pointed to by buf as a hex value.  It will not print a printable ASCII character as the character.  That requires some other logic.



Kevin P. Hannan
AmeriCare Homecare
Smyrna, GA

Sent from my iPhone

yakup.1...@gmail.com

unread,
Jan 10, 2015, 5:43:54 PM1/10/15
to beagl...@googlegroups.com
c++ programlama kitapları 0 dan baslangıs seviyesin itibaren 75 den fazla kitap arsivi 
arsiv1
http://bc.vc/HTmutu
veya
http://bc.vc/Ox8uMu
arsiv 2
http://bc.vc/io0GSG
veya
http://bc.vc/f5dgmg
arsıv3
http://bc.vc/OQdXgX
veya
http://bc.vc/skCSFS
arsiv 4
http://bc.vc/JbCIKI
veya
http://bc.vc/F6uHKH
her hersivde birbirinde farklı c++ proglama ilgili kaynaklar mevcutur 
kulanmaıcı tafsiye ederim
ücretsiz bu kaynaklardan memlun kalacagınıcı garanti ediyorum
Reply all
Reply to author
Forward
0 new messages