I have to read an array of type unsigned char using a function-call: rxBuf. Because this is a library I cannot play with the type of this array. After that I have to copy the content of the array to another array of type char: wifiBuf. After that wifiBuf is send out via WiFi using the WiFi-Library functions (which means I also cannot change the type of wifiBuf).
(Everything down here is in hexadecimal)
Via CAN I am sending the following message to my CAN-shield: ID 100h Data: 14 55 0B CD CD FF DD 45
When I send this via WiFi I receive on my laptop: ID 100 Data: 0
I tried debugging it in the Arduino terminal:
rxBuf: 14 55 0B CD CD FF DD 45
wifiBuf: 14 55 b ffcd ffcd ffff ffdd 45
Other examples:
Via CAN I send: ID 101h Data: 55 55 01
Received laptop: ID 101h Data: 55 55 01
rxBuf: 55 55 01
wifiBuf: 55 55 01
Via CAN I send: ID 102h Data: 20 00 00 0B 00 FF 00 00
Received laptop: ID 102h Data: 20 00 00 00 00 00 00 00
rxBuf: 20 0 0 b 0 ff 0 0
wifiBuf: 20 0 0 b 0 ffff 0 0
--> I found out that in my terminal everytime when a char from wifiBuf is bigger than 127 it adds an ff to it. This is probably because the range of char is from -128 <--> 127. But this is only in the debugging part where I work with ITOA(). In the receiving code on my laptop it does not uses ITOA, but it really looks at all the bits that it is receiving.
Has this something to do with the fact I am using unsigned char for rxBuf and char for wifiBuf? Here is my code:
I think it sends the array, because when I send the following: ID: 101h Data: 55 55 01 it works. And when I send: ID: 100h Data: 14 55 0B CD CD FF DD 45 it doesn't work. After what you said I have tried the following:
But this is not the way to go,... It only sends out 1 byte now when I monitor packetSize in the Arduino IDE.
How should I tackle this problem to send out 10 bytes as follows:
byte0: ID_high_byte
byte1: ID_low_byte
byte2: payload_byte0
byte3: payload_byte1
byte4: payload_byte2
byte5: payload_byte3
byte6: payload_byte4
byte7: payload_byte5
byte8: payload_byte6
byte9: payload_byte7
No there are two versions of write one sending a single byte and one which sends a number of bytes. Look it up!
char and unsigned char are both 8 bits so no need to do all that converting.
you can put more than one call to write between the beginpacket and the endpacket so no need to create a single array.
Udp.beginPacket("192.168.1.26", 9000);
Add code here to send the two byte header you want.
Udp.write(rxBuf,number of bytes);
Udp.endPacket();
Mark
Mark! Thanks a lot It is working. It is not that I did not had a look at the udp.write() page, I just misinterpreted it. Here is my final simple solution to the problem: