c - Sending Hexadecimal data through Serial Port Communication in Linux -
i have got task of sending hexadecimal data comport in linux. have written simple c code, sends decimal number. can me in sending hexadecimal bit.
here code have written
#include <stdio.h> /* standard input/output definitions */ #include <string.h> /* string function definitions */ #include <unistd.h> /* unix standard function definitions */ #include <fcntl.h> /* file control definitions */ #include <errno.h> /* error number definitions */ #include <termios.h> /* posix terminal control definitions */ int number,n; void main(void){ open_port(); } int open_port(void) { int fd; /* file descriptor port */ fd = open("/dev/ttyacm0", o_rdwr | o_noctty | o_ndelay); if (fd == -1) { perror("open_port: unable open /dev/ttyacm0 - "); } else{ printf("port opened successfully\n"); number = 1; while(number!=55){ scanf("%d",&number); n = write(fd, "atz\r", number); if (n < 0) fputs("write() of 4 bytes failed!\n", stderr); } } return (fd); }
please help
thanks in advance :) :)
write
defined as:
ssize_t write(int fd, const void *buf, size_t count);
that is, sends count
bytes fd
buf
. in case, data string "aztr\r", plus undefined data after (if count > 5). program sends neither hexadecimal nor decimal data.
do want send binary data or string of hexadecimal characters?
for option one, can use: write(fd, somebuffer, len);
, buffer pointer set of bytes (including ints, etc).
for option two, first convert data hexadecimal string using sprintf
%02x
format string, proceed write
data port.
Comments
Post a Comment