Reputation: 11
I have method which accepts "constant char*" as a parameter. But when I pass the below array to the method "*SendUartMessage(int uartFd, const char dataToSend)**" only first two hexadecimal values are showing inside the method in the parameter. To be precise as the third value is zero 0x00, it blocks the other values pass inside the method. Can any one provide solution to pass all the array value inside the method.
const char updateAllChannelData[] = { 0x01, 0x06, 0x00, 0x19, 0x00, 0x01, 0x99, 0xCD };
SendUartMessage(uartFd, updateAllChannelData);
This is the method
static void SendUartMessage(int uartFd, const char* dataToSend)
{
size_t totalBytesSent = 0;
size_t totalBytesToSend = strlen(dataToSend);
int sendIterations = 0;
close(r1PinFd);
r1PinFd = GPIO_OpenAsOutput(MIKROE_PWM, GPIO_OutputMode_PushPull, GPIO_Value_High);
while (totalBytesSent < totalBytesToSend) {
sendIterations++;
// Send as much of the remaining data as possible
size_t bytesLeftToSend = totalBytesToSend - totalBytesSent;
const char* remainingMessageToSend = dataToSend + totalBytesSent;
ssize_t bytesSent = write(uartFd, remainingMessageToSend, bytesLeftToSend);
if (bytesSent == -1) {
Log_Debug("ERROR: Could not write to UART: %s (%d).\n", strerror(errno), errno);
exitCode = ExitCode_SendMessage_Write;
return;
}
totalBytesSent += (size_t)bytesSent;
}
int c, d;
sleep(5);
close(r1PinFd);
r1PinFd = GPIO_OpenAsOutput(MIKROE_PWM, GPIO_OutputMode_PushPull, GPIO_Value_Low);
Log_Debug("Sent %zu bytes over UART in %d calls.\n", totalBytesSent, sendIterations);
}
Upvotes: 0
Views: 101
Reputation: 153303
Can any one provide solution to pass all the array value inside the method.
In C, when an array is passed to a function, the arrray is first converted to the type and address of the first element.
const char updateAllChannelData[] = {
0x01, 0x06, 0x00, 0x19, 0x00, 0x01, 0x99, 0xCD };
SendUartMessage(uartFd, updateAllChannelData);
SendUartMessage()
receives a pointer to updateAllChannelData[0]
, not the array.
For SendUartMessage()
to know how much of the array to use, also pass the array element count and re-write SendUartMessage()
to accept the count.
static void SendUartMessage(int uartFd, size_t count, const char* dataToSend);
const char updateAllChannelData[] = {
0x01, 0x06, 0x00, 0x19, 0x00, 0x01, 0x99, 0xCD };
size_t count = sizeof updateAllChannelData / sizeof updateAllChannelData[0];
SendUartMessage(uartFd, count, updateAllChannelData);
Upvotes: 0
Reputation: 7324
The array has a 0 byte at index 2. strlen
calculates the length of a C-string, that is, a null terminated (0 byte terminated) array. Treating your array as a string, the length is 2. If you want to know the real length of the array you have to pass it as a parameter:
const char updateAllChannelData[] = { 0x01, 0x06, 0x00, 0x19, 0x00, 0x01, 0x99, 0xCD };
SendUartMessage(uartFd, updateAllChannelData, sizeof(updateAllChannelData));
static void SendUartMessage(int uartFd, const char* dataToSend, size_t totalBytesToSend)
{
...
}
Upvotes: 2
Reputation: 223689
You're using strlen
on a character array that is not a string.
Strings in C are null terminated, meaning a byte with value 0 ends the string. You don't have a string but an array of bytes whose valid values include 0.
You need to pass the size of the array to the function as a separate parameter.
Upvotes: 0