[ O4SiS @ 29.04.2007. 18:09 ] @
Poštovanje
imam problem sa prikazom hex vrijednosti vrijednosti koja je stigla na serijski port.
napravio sam jedan program (upotrebom serial programming howto)
Izvorni kod programa
Code:

#include <stdio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>
#include <stdlib.h>
struct termios tio;

int main()
{
  int fd, status, whichBaud, result,i;
  long baud;
  char buffer[255];

  char stanje[] = "\x1b\x53";
  baud = B9600;

 fd = open(device ,O_RDWR | O_NOCTTY);
    if (fd <0) 
    {
        printf("error");
        exit(1);
      }
  tio.c_cflag = baud | CS8 | CREAD | CLOCAL;

  tio.c_iflag = IGNPAR;  /* ignore parity errors */

  tio.c_oflag = 0;       /* set output flag non-canonical, no processing */

  tio.c_cc[VTIME] = 0;   /* no time delay */
  tio.c_cc[VMIN]  = 0;   /* no char delay */
  tcflush(fd, TCIFLUSH); /* flush the buffer */
  tcsetattr(fd, TCSANOW, &tio); /* set the attributes */
  
/* Set up for no delay, ie non-blocking reads will occur. 
   When we read, we'll get what's in the input buffer or nothing */
  fcntl(fd, F_SETFL, FNDELAY);

write(fd,&stanje,2);

usleep(100 * 1000);

  result = read(fd,buffer,255);
  printf("%c\n",result);
  buffer[result] = 0; // zero terminate so printf works
  printf("%s\n",buffer);
}
/* close the device file */
  close(fd);
}


Povezao sam dva kompjutera i kad pošaljem hex vrijedost 31 ovamo imam 1 i tako dalje (ASCII)
Ali kad pošaljem 0x81 odnosno b10000001 vrijednost ne dobivam ništa...
Kako da prikažem pristiglu vrijednost kao hex vrijednost ili još bolje binarnu vrijednost?
Da li je potrebna konverzija? Ima li gotova funkcija da se ta vrijednost kovertira?

[ rj444 @ 21.05.2007. 18:14 ] @
Imas konverziju %x za funkciju printf kojom ceo broj (int) prikazujes u heksadecimalnom obliku.

Binarni oblik mozes da dobijes konvertujuci pristigli podatak bit po bit.
To radis tako sto maskiras sve bite sem MSB (skroz levi bit podatka). Uradis bitsku I operaciju sa maskom ( | ) i ispises sta dobijes, to je taj MSB bit. Zatim pomeris posmatranu rec za jedan bit ulevo i onda opet. To sve ponovis sizeof(char) puta ako primas ASCII kodove ili sizeof(int) ako radis sa celim brojevima.

Nadam se da sam pomogao, pozdrav.