So I don't know C so I'm just trying to get some very very basic code running on my arduino to send it commands over serial.

I'm feeding serial into buf

irsend.sendNEC(0x210704FB, 32)

this works but I need to read from buf instead of hardcoding for one code.

irsend.sendNEC(buf, 32) won't compile but irsend.sendNEC((uint32_t) buf, 32) will. It just doesn't do anything.

When I use

serial.print((uint32_t) buf, 32);

it prints just seemingly random letters?

I'm trying to just feed it a serial code with a language I know a little better. I can send the hex code over serial with python but I don't know the first thing about C. I don't understand why I can't just feed it a variable and have it read it as is. here's my code

#include <IRremote.h>

char buf[80];

int readline(int readch, char *buffer, int len) {
  static int pos = 0;
  int rpos;

  if (readch > 0) {
    switch (readch) {
      case '\r': // Ignore CR
      case '\n': // Return on new-line
        rpos = pos;
        pos = 0; // Reset position index ready for next time
        return rpos;
        if (pos < len-1) {
          buffer[pos++] = readch;
          buffer[pos] = 0;
  return 0;

void setup() {

IRsend irsend;

void loop() {
  if (readline(Serial.read(), buf, 80) > 0) {
    //tried to set up code to show what the arduino received and tried ways to send
    //Serial.print("You entered: >");
    //doesn't seem to do anything irsend.sendNEC((uint32_t) buf, 32);
    //works but only for this one button irsend.sendNEC(0x210704FB, 32);
    //want to use irsend.sendNEC(buf, 32);
    //me trying to figure out what irsend is being fed through buf Serial.print((uint32_t) buf, 32);
  • 2
    What is buf? The first two sendNEC in your question are identical but you say one compiles and one doesn't - probably a wrong copy/paste. Please edit your question to clarify.
    – Mat
    Nov 10, 2020 at 6:43
  • 1
    serial.print(...., 32). "32" is not a valid second parameter for the serial.print() function when printing an integer value. Just leave it out or replace it with either "BIN", "DEC", "OCT" or "HEX". BTW, what is your question exactly?
    – StarCat
    Nov 10, 2020 at 13:45
  • I updated my post yesterday to add some clarity and show my entire code instead of just the specific function I was having a problem with. Any help is very much appreciated. Nov 11, 2020 at 16:29

1 Answer 1


Same issue for me, but this is my solution.

Mapping beween python struct (packed data) and arduino struct is as follows:

Python              Arduino
?                   boolean
i                   int32_t or uint32_t 
f                   float

Arduino Code:

// The struct we want to send over the serial connection
struct Data {
  int32_t timer;
  float value2;
  uint32_t sensor1;
  uint32_t datasize;  
 Data data;
void setup() {
  // Initialize the serial connection

void loop() {
  // Create an instance of the Data struct
  data.timer = micros();
  data.value2 = 3.14;
  // read the input on analog pin 0:
  data.sensor1 = analogRead(A0);

  // Calculate the size of the struct in bytes
  size_t size = sizeof(data);
  data.datasize = size; 
  // Allocate a buffer for the packed data
  uint8_t *buf = (uint8_t*)malloc(size);

  // Pack the data into the buffer
  memcpy(buf, &data, size);

  // Send the packed data over the serial connection
  Serial.write(buf, size);

  // Free the buffer

  // Wait before sending the data again

Python Code:

# read struct from arduino / sample code above

import serial
import struct

# Open the serial port
ser = serial.Serial('/dev/ttyUSB0', 115200)

# The struct we expect to receive from the Arduino
#struct Data {
#  int32_t value1;
#  float value2;
#  uint32_t value3;
expFormat = "i f i i"         # Format to match arduino struct
structDataLength = struct.calcsize(expFormat)
print(structDataLength)       # RESULT IS 12

while True:
# Read the data from the serial port
# We read until we have received all the bytes for the struct
    data = b''
    while len(data) < structDataLength:
        data += ser.read(structDataLength - len(data))
    # Unpack the data into an instance of the Data struct
    val = struct.unpack('i f i i', data)

# Print the values

# Close the serial port

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.