3i've a small C program for read and write Hex Data over serial port. In reading i've not problems, bu when i try to write a Hex Data as this:
static const unsigned char cmdActuatorOn[] = "\x41\x54\x2B\x18\x12\x00\x12\x4B"
i see that the write function send over serial port only 5 bytes...so i suppose that it for any reason cut Hex Data at "0x00" character. Below my serial port settings:
int init_port(char const *const device)
{
int descriptor, result;
struct termios settings;
descriptor = open(device, O_RDWR | O_NOCTTY);
result = fcntl(descriptor, F_SETFL, O_NONBLOCK);
result = tcgetattr(descriptor, &settings);
settings.c_cflag &= ~PARENB;
settings.c_cflag &= ~CSTOPB;
settings.c_cflag &= ~CSIZE;
settings.c_cflag |= CS8;
settings.c_iflag |= IGNPAR;
settings.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXOFF | IXANY | INPCK);// raw data input mode
settings.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);// raw data output
settings.c_oflag &= ~OPOST;// setting timeouts
settings.c_cc[VMIN] = 1; // minimum number of chars to read in noncanonical (raw mode)
settings.c_cc[VTIME] = 5; // time in deciseconds to wait for data in noncanonical mode (raw mode)
cfsetispeed(&settings, B9600);
cfsetospeed(&settings, B9600);
result = tcsetattr(descriptor, TCSANOW, &settings);
return descriptor;
}
Function for write data is:
int write_port(void const *const data, size_t const size)
{
unsigned char const *p = (unsigned char const *)data;
unsigned char const *const q = (unsigned char const *)data + size;
ssize_t n;
while (p < q) {
do {
n = write(port_descriptor, p, (size_t)(q - p));
} while (n == (ssize_t)-1 && errno == EINTR);
if (n == (ssize_t)-1 && errno == EWOULDBLOCK) {
/* Sleep for a millisecond, then retry. */
usleep(1000);
continue;
}
if (n == (ssize_t)-1)
return errno;
else
if (n < (ssize_t)1)
return EIO;
p += (size_t)n;
}
if (p != q)
return EIO;
return 0;
}
I call function write_data as below:
result = write_port(cmdActuatorOn, strlen(cmdActuatorOn));
This work fine for others binary data as:
static const unsigned char cmdGetCoordAddress[] = "\x40\x04\x2B\x02\x08\x52\x41\x9D";
static const unsigned char cmdReadCoordChannelAddress[] = "\x43\x12\x0B\x07\x08";
static const unsigned char cmdReadCoordPanId[] = "\xAF\x50\x2B\x07\x09\x52\x49\x90";
Write function in this case write all correct bytes on serial port. Only cmdActuatorOn not, so i suppose that 0x00 is the problem.
Where are my errors? I've wrong serial port configuration? I set it for use with Raw Mode...is ot correct?
Thanks a lot
write_port(), don't usestrlen()since the data is not a string, but binary (as @MartinJames pointed out, just making it more concrete since you're still not showing that part of the code). - unwind