У меня есть цикл, скажем, от 1 до 32. В этом случае десятичные числа от 1 до 32. Я должен вставить шестнадцатеричное значение от 1 до 32 в массив без знака, а затем выполнить send. Мой код выглядит следующим образомКак вставить шестнадцатеричное значение внутри массива шестнадцатеричного символа
char hex[3];
unsigned char hexunsigned[3];
int dec;
int i=0;
do
{
// this is the unsigned char array , i have to insert at 4th pos.
unsigned char writebuffer[8] ={0x01, 0x05, 0x00, 0x20, 0xFF, 0x00, 0x00, 0x00};
// to place the hex value of each point on writeBuffer[3]
dec=i+1;
decimal_hex(dec,hex); //this function returns hex value of corresponding i value.
memcpy(hexunsigned,hex,sizeof(hexunsigned)); //converting char to unsigned char
writebuffer[3]= hexunsigned; //shows the error cannot convert from 'unsigned char [3]' to 'unsigned char'
unsigned short int crc = CRC16(writebuffer, 6); // Calculates the CRC16 of all 8 bytes
writebuffer[6] = ((unsigned char*) &crc)[1];
writebuffer[7] = ((unsigned char*) &crc)[0];
serialObj.send(writebuffer, 8);
//send another packet only after getting the response
DWORD nBytesRead = serialObj.Read(inBuffer, sizeof(inBuffer));
i++;
}while(nBytesRead!=0 && i<32);
так, линия
writebuffer[3]= hexunsigned;
показывает ошибку, как не может конвертировать из 'unsigned char [3]'
в 'unsigned char'
.
Как вставить hex unsigned
в writebuffer
массив.
когда
char hex[3];
int dec;
dec=i+1;
decimal_hex(dec,hex);
memcpy(writebuffer+3, hex, sizeof(writebuffer+3));
используется, он переводит 01 (разл), как 31.
Я попытался ниже код также, он также перевод 01 (разл), как 31.
sprintf(hex, "%X", dec);
memcpy(writebuffer+3, hex, sizeof(writebuffer+3));
Я думаю, что это лечение «1» получил в переменной шестигранным, как ASCII и отправка шестнадцатеричное значение символа «1», как 31.
Функция отправки ниже:
void serial::send(unsigned char data[], DWORD noOfByte)
{
DWORD dwBytesWrite;
WriteFile(serialHandle, data, noOfByte, &dwBytesWrite, NULL);
}
Какой тип 'hexunsigned'? – kmort
i обновил код – user3048644