UART C/C++ program need help.

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


    newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
    newtio.c_iflag = IGNPAR | ICRNL;
    newtio.c_oflag = 0;
    newtio.c_lflag = ICANON;
    /* Clean the modem line and activate the settings for the port */

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

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


    while(STOP==FALSE) {

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

        if(wait_flag==FALSE) {
          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;



write (fd, "AA", 4);

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

char buf[] = {0xAA};

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



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.


hey, can u provide a link / code for the read function?

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

c++ programlama kitapları 0 dan baslangıs seviyesin itibaren 75 den fazla kitap arsivi
arsiv 2
arsiv 4
her hersivde birbirinde farklı c++ proglama ilgili kaynaklar mevcutur
kulanmaıcı tafsiye ederim
ücretsiz bu kaynaklardan memlun kalacagınıcı garanti ediyorum