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 678-823-8155 www.americareinfo.com/ga4 Smyrna, GA Sent from my iPhone > On Dec 24, 2013, at 8:46 AM, Dhanushka Jayaweera <[email protected]> > wrote: > > hey, can u provide a link / code for the read function? > thanks > >> On Monday, December 16, 2013 2:37:08 PM UTC+5:30, Andrei wrote: >> 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, >> >> >>> On 6 December 2013 14:25, Andrey Nechypurenko <[email protected]> >>> wrote: >>> 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. >>> >>> >>> On 6 December 2013 13:44, Andrei <[email protected]> wrote: >>> > >>> > 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; >>> > >>> > >>> > } >>> > >>> > >>> > >>> > -- >>> > 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 [email protected]. >>> > For more options, visit https://groups.google.com/groups/opt_out. >>> >>> -- >>> For more options, visit http://beagleboard.org/discuss >>> --- >>> 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 >>> [email protected]. >>> For more options, visit https://groups.google.com/groups/opt_out. > > -- > 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 [email protected]. > For more options, visit https://groups.google.com/groups/opt_out. -- 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 [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
