Poole
Offline
Jr. Member
Karma: 0
Posts: 83
I'm not a complete idiot. Some bits are missing
|
 |
« Reply #15 on: November 02, 2012, 02:30:48 am » |
I tried to compile a sketch including the code you posted and I get the following errors: sketch_nov01a:2: error: expected ',' or '...' before '&' token sketch_nov01a:2: error: ISO C++ forbids declaration of 'T' with no type
Using Arduino 1.0.1 on a Mac
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 11
Posts: 393
|
 |
« Reply #16 on: November 02, 2012, 02:32:24 am » |
If you buy a gun, you can shoot yourself in the foot. If you want to.
True. It really comes down to whether or not you have the skill to handle the gun. You have to know the limits of your competence. So I guess you never do any assembly language programming at all then, huh? How dangerous is that? No type checking at all. Every line of code a virtual death trap. 
|
|
|
|
« Last Edit: November 02, 2012, 02:34:51 am by pico »
|
Logged
|
|
|
|
|
Poole
Offline
Jr. Member
Karma: 0
Posts: 83
I'm not a complete idiot. Some bits are missing
|
 |
« Reply #17 on: November 02, 2012, 02:34:30 am » |
True. It really comes down to whether or not you have the skill to handle the gun. You have to know the limits of your competence.
Wow. Well this could always be settled with a duel. Handbags at dawn perhaps? However, in the mean time.... why won't my code work? 
|
|
|
|
|
Logged
|
|
|
|
|
Poole
Offline
Jr. Member
Karma: 0
Posts: 83
I'm not a complete idiot. Some bits are missing
|
 |
« Reply #18 on: November 02, 2012, 02:41:45 am » |
Well whist you guys were fighting I tried this and it seems to work. Thanks template <class T> void sendAnything(const T& value) { const byte* p = (const byte*)(const void*)&value; unsigned int i; for (i = 0; i < sizeof(value); i++) { if (*p == 0x7E || *p == 0x7D) { //byte stuffing Serial.write(0x7D); Serial.write(*p++ ^ 0x20); } else { Serial.write(*p++); } } }
|
|
|
|
|
Logged
|
|
|
|
|
Poole
Offline
Jr. Member
Karma: 0
Posts: 83
I'm not a complete idiot. Some bits are missing
|
 |
« Reply #19 on: November 02, 2012, 02:46:57 am » |
Just one other quick question: How might I modify the above code to send the bytes MSB first?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Offline
Shannon Member
Karma: 118
Posts: 10153
|
 |
« Reply #20 on: November 02, 2012, 03:19:51 am » |
How do you expect that to work when you call sendAnything with a string?
|
|
|
|
|
Logged
|
|
|
|
|
Poole
Offline
Jr. Member
Karma: 0
Posts: 83
I'm not a complete idiot. Some bits are missing
|
 |
« Reply #21 on: November 02, 2012, 03:28:50 am » |
Oh yeah, duh. As I said. Just finished a night shift. Not optimum time for writing code.....
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #22 on: November 02, 2012, 04:23:29 am » |
So I guess you never do any assembly language programming at all then, huh? How dangerous is that? No type checking at all. Every line of code a virtual death trap.  What is this assembly language of which you speak?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #23 on: November 02, 2012, 04:24:39 am » |
Well whist you guys were fighting I tried this and it seems to work. Thanks
While you were fixing dinner I was making a sandwich.
|
|
|
|
|
Logged
|
|
|
|
|
Germany
Offline
Edison Member
Karma: 27
Posts: 1495
|
 |
« Reply #24 on: November 02, 2012, 05:00:25 am » |
How do you expect that to work when you call sendAnything with a string?
Well, it works somehow. You might rename it to sendAnyValueOrItsAddress  Arduino is famous for NoErrorHandling Software, so I'd max. add a comment in the library .cpp source code. @Chris: rather look at the Serial.print() ( resp. hardware\arduino\cores\arduino\Print.h / .cpp ) code for a nice sample of function overloading.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #25 on: November 02, 2012, 05:07:32 am » |
if (*p == 0x7E || *p == 0x7D) { //byte stuffing
You lost me on that line. Perhaps I should learn assembler?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #26 on: November 02, 2012, 05:09:12 am » |
Arduino is famous for NoErrorHandling Software ...
So is Microsoft.
|
|
|
|
|
Logged
|
|
|
|
|
Germany
Offline
Edison Member
Karma: 27
Posts: 1495
|
 |
« Reply #27 on: November 02, 2012, 05:22:46 am » |
if (*p == 0x7E || *p == 0x7D) { //byte stuffing
You lost me on that line. Perhaps I should learn assembler? It's plain good old C, and provides } as an escape character to avoid ever sending a tilde character ~ 0x7e Don't ask my why, nor why some might consider this "fun".
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #28 on: November 02, 2012, 05:40:57 am » |
if (*p == 0x7E || *p == 0x7D) { //byte stuffing
You lost me on that line. Perhaps I should learn assembler? It's plain good old C, and provides } as an escape character to avoid ever sending a tilde character ~ 0x7e Don't ask my why, nor why some might consider this "fun". Oh I see. It's so obvious now. So really: if (*p == '~' || *p == '}') { //byte stuffing Serial.write('}');
That would certainly stuff the bytes. Particularly if the data type was, say, an int. And the point of this line? Serial.write(*p++ ^ 0x20);
Honestly, this is why you shouldn't fiddle with pointers if you don't understand what you are doing.
|
|
|
|
|
Logged
|
|
|
|
|
Germany
Offline
Edison Member
Karma: 27
Posts: 1495
|
 |
« Reply #29 on: November 02, 2012, 05:57:47 am » |
And the point of this line? Nick, are you joking? It's sending a ~ as }^ and a real } as }]It's allowing to insert meta data in the stream, marked by a ~ starting character. Or add other special characters -- by prefixing them with }
|
|
|
|
« Last Edit: November 02, 2012, 06:02:41 am by michael_x »
|
Logged
|
|
|
|
|
|