I have got a task of sending hexadecimal data to my COMPORT in linux. I have written this simple C code, but it sends only a decimal number. Can anyone help me in sending an hexadecimal bit.
Here is the code I 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 for the port */
fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
perror("open_port: Unable to 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 🙂 🙂
writeis defined as:That is, it sends
countbytes tofdfrombuf. In your case, the data is always the string “AZTR\r”, plus undefined data after that (if count is > 5). Your program sends neither hexadecimal nor decimal data.Do you want to send binary data or a string of hexadecimal characters?
For option one, you can use:
write(fd, somebuffer, len);, where some buffer is a pointer to any set of bytes (including ints, etc).For option two, first convert your data to a hexadecimal string using
sprintfwith%02Xas the format string, then proceed towritethat data to the port.