SVP serial Tx bytes

I have modified the serial example for the atmega1284p SVP, such that in the main loop, there is a condition that should default to send a byte representing an integer (between 0 and 255). I have tried various permutations of sending an integer, but on the receiving end (a simple python serial instance), I receive ‘\x00’. This goes for many numbers I have tried, not just 1. When I just pass it a hard-coded c-string, the receiving end reads it in correctly. I would like to be able to combine integer byte values as well as characters in a single buffer, and I know this is possible, so am I missing sth obvious?

                if (button_is_pressed(MIDDLE_BUTTON))
//                    memcpy(send_buffer, "123!\r\n", 6);
//                    serial_send = 1 & 0xFF;
                    int right_V = 1;
                    memcpy(send_buffer, (unsigned char) right_V, 1);
                    serial_send(USB_COMM, send_buffer, 6); 


Can you try:

memcpy(send_buffer, (unsigned char*) right_V, 1);

- Ben

Yeah I get the same results using the pointer (which I am pretty sure is the right way to write it).

I had originally tried to assign values to the send buffer as follows :

send_buffer[0]=1;  // or 1&0xFF
serial_send(USB_COMM, send_buffer, 3); 

I just tried that method again, and the 1 actually made it across correctly (’\x01’). However, for this case the characters “A\n” are translated to “K” and “M”. If I try “Z\n”, the output is the same! This also occurs if I try to force a cast of (char) or (unsigned char) onto “A\n”…

Finally, if I try a hack such as

memcpy(send_buffer[1], "A\n", 2);

I can’t read anything on the other end. I know this should be a very easy fix, I just havent found it yet.


The problem with what you are doing here is you are setting a char equal to the memory address of a string (“A”). It will work if you change it to:


Oh, and I think my previous suggestion should have been:

memcpy(send_buffer, (unsigned char*) &right_V, 1);

- Ben

Wow. Even easier than I thought, I obviously need a refresher on char/strings in c, and really c in general. Is there a preferred method to populate the buffer? I saw that the example used memcp_P instead of memcp (I understand why, but I’m not sure if it is going to matter in this case), but the above method seems like the easiest.

Thanks Ben!

memcpy_p() copies from program space (flash) while memcpy() copies from RAM, so the appropriate one to use depends on where the data to copy is stored. In general, the AVR you are using has a lot more flash memory than RAM, so you probably generally want to store long, constant strings in program memory.

You can populate the send_buffer array in whatever way is most convenient for you, or you can skip using it altogether. For example, if you want to transmit a two-byte integer, you could just do something like:

int value = 1000;
serial_send(USB_COMM, (unsigned char*)&value, 2);

You would need to reassemble it on the other side with something like:

// read two bytes from the serial port and put them in receiveBuffer array
int value = receiveBuffer[0] + 256*receiveBuffer[1];

- Ben