I want to send a hexadecimal value to the write function. (e.g., 0×90). This is because the device that needs to communicate receives commands in hexadecimal numbers. The unused variables appeared as they were tested and annotated to lose the hexadecimal value, and will be erased later. How can I write a write function with a hexadecimal value other than String?
For beginners, please tell us how to exchange hexadecimal values through read and write functions.
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <time.h>
#include <pthread.h>
int main(void)
{
int fd;
fd=open("/dev/ttyHSL6", O_RDWR|O_NOCTTY );
struct termios newtio;
char buffer[2000];
int bytes_read=0;
char *data;
//int *a=0x90;
char *a="0X90";
const int *num1;
if (fd == -1)
{
printf("Error! in opening port");
exit(-100);
}
memset(&newtio, 0, sizeof(struct termios));
newtio.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 1;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &newtio);
data=malloc(sizeof(char)*140);
while(1){
const char *str ="0x91";
//write(fd, str, strlen(str)+1);
bytes_read = read(fd,buffer,sizeof(buffer));
if (bytes_read > 0)
{
buffer[bytes_read]=0;
printf("%s", buffer);
}
usleep(100000);
}
close(fd);
return 0;
}
Current Progress:
I set up transmission and reception variables and compiled the code using unsigned char, but such an error occurs.
./serial.c:48:10: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘unsigned char *’ [-Wformat=]
printf("%x\n",str);
If I use %p, there is no compilation error, but as you know, the address value is printed, so it is different from the result I want. I don't know how to do it because I'm a beginner.
The revised parts are as follows.
while(1){
//const char *str ="0x91";
unsigned char str[13] = {0xA5,0x80,0x90,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xBD};
write(fd, str, strlen(str));
printf("%x\n",str);
bytes_read = read(fd,buffer,sizeof(buffer));
// printf("%x\n",*str);
if (bytes_read > 0)
{
buffer[bytes_read]=0;
printf("%p\n", buffer);
}
usleep(100000);
//printf("%s\r\n",buffer);
}
close(fd);
return 0;