Page 1 of 1

Send bytes over UART

Posted: Thu Feb 06, 2020 3:59 am
by NIghthunter101
I'm trying to control a TMC2009 driver over UART and I need to send the commands as bytes but I only seem to be able to send characters. What do I need to change to get it sending bytes not characters?
I've got the following code for the UART,

Code: Select all

int sendData(const char* logName, const char* data)
{
	const int len = strlen(data);
	const int txBytes = uart_write_bytes(UART_NUM_2, data, len);
	ESP_LOGI(logName, "Wrote %d bytes", txBytes);
	return txBytes;
}

static void tx_task()
{
	static const char *TX_TASK_TAG = "TX_TASK";
	while (1) {
		sendData(TX_TASK_TAG, "0xA000004F");
		vTaskDelay(2000 / portTICK_PERIOD_MS);
	}
}

Re: Send bytes over UART

Posted: Thu Feb 06, 2020 5:06 am
by somesh
NIghthunter101 wrote:
Thu Feb 06, 2020 3:59 am
I'm trying to control a TMC2009 driver over UART and I need to send the commands as bytes but I only seem to be able to send characters. What do I need to change to get it sending bytes not characters?
I've got the following code for the UART,

Code: Select all

int sendData(const char* logName, const char* data)
{
	const int len = strlen(data);
	const int txBytes = uart_write_bytes(UART_NUM_2, data, len);
	ESP_LOGI(logName, "Wrote %d bytes", txBytes);
	return txBytes;
}

static void tx_task()
{
	static const char *TX_TASK_TAG = "TX_TASK";
	while (1) {
		sendData(TX_TASK_TAG, "0xA000004F");
		vTaskDelay(2000 / portTICK_PERIOD_MS);
	}
}
you need to convert string into hex bytes or directly send bytes by allocating in an array.
for example, uint8_t a[]={0xA0,0x00,0x00,0x4F};

Re: Send bytes over UART

Posted: Thu Feb 06, 2020 5:22 am
by NIghthunter101
uart_write_bytes needs a char not uint8_t. Is there a different function I should be using or do I have to make something new?

Re: Send bytes over UART

Posted: Thu Feb 06, 2020 7:37 am
by somesh
NIghthunter101 wrote:
Thu Feb 06, 2020 5:22 am
uart_write_bytes needs a char not uint8_t. Is there a different function I should be using or do I have to make something new?
you can use char also.