Show Posts
|
|
Pages: [1] 2 3 4
|
|
3
|
Using Arduino / Interfacing w/ Software on the Computer / Re: Forming a hex array to send on serial port
|
on: October 09, 2012, 12:32:59 pm
|
That makes sense now. Also explains why i've seen *char every now and again. Heres the solution i've got that seems to work now, although if anyone can tell me how to get away with not passing the length of the msgData array then feel free! This calls the function: byte msgDataArray[] = {0x04, 0x01, 0x99, 0xAA}; int msgDataSize = sizeof(msgDataArray); sendMsg(0x44,0xBF,0x74, msgDataArray, msgDataSize);
The function itself: void sendMsg(byte source, byte destination, byte msgType, byte *msgData, int msgLen) { // 1 2 3 4 5 6 7 // [src] [len] [dest] [msgtype] [d0]...[dn] [crc] //get length of msg data for len field byte len = msgLen + 3; //Set known data byte GetData[255] = {source, len, destination, msgType}; for (int i = 0; i < msgLen; i++) { int index = i+4; GetData[index] = msgData[i]; } //Location of ChkSum int crcLoc = msgLen + 4; //Set ChkSum GetData[crcLoc] = getCheckSum(GetData);
//Print data to serial for (int i = 0; i < len+2 ; i++) { Serial.print(GetData[i], HEX); } }
and the CRC function: int getCheckSum(byte *string) { int i; int checksum; int length; checksum = 0; length = string[1]+1; //Serial.print("length: "); //Serial.println(length); for (i = 0; i < length; i++) { checksum ^= string[i]; } return checksum; }
Many thanks to those that helped.
|
|
|
|
|
4
|
Using Arduino / Interfacing w/ Software on the Computer / Re: Forming a hex array to send on serial port
|
on: October 09, 2012, 11:06:12 am
|
What is a "HEX array"?
Apologies, thought it was more obvious than it is. Its an array of hex bytes. It is formed in the way i showed in the OP, but where the message data bytes sit needs to be expandable and fed from another array of bytes. You want the sendMsg() function to accept an array as the 4th argument? Then, define it that way: void sendMsg(byte source, byte destination, byte msgType, byte msgData[someMaxSizeHere]) { or void sendMsg(byte source, byte destination, byte msgType, byte *msgData) { Statically sizing GetData will be required. You'll need to define the array large enough to hold the largest message you plan to support. Copying the data from msgData to GetData is easy, using a for loop. I was trying this but because i did not know the exact length it wouldnt accept the array being initialised as byte msgData[] which threw me. i have got the code to fill the array from a for loop, that came to me shortly after posting. Can you explain the concept of *msgData? I'll try these solutions now and let you know. Thanks!
|
|
|
|
|
5
|
Using Arduino / Interfacing w/ Software on the Computer / Forming a hex array to send on serial port
|
on: October 09, 2012, 06:54:14 am
|
Im trying to form a hex array with a variable number of data bits. I have a function which accepts the src, dest, message type and message data all in bytes and with single bytes everything works. I cant work out how to get it to accept an array of bytes for the message data as this can be anything from 1 to 20 bytes long. I will effectively end up with [SRC][LEN][DST][MSGTYPE][MSGDATA0]...[MSGDATA99][CRC] So, how do i get it to accept this array within an array?! Heres the code... void sendMsg(byte source, byte destination, byte msgType, byte msgData) {
byte len = sizeof(msgData) + 3; /* DEBUG Serial.println(""); Serial.print("Src: "); Serial.println(source, HEX); Serial.print("Len: "); Serial.println(len, HEX); Serial.print("Dst: "); Serial.println(destination, HEX); Serial.print("Msg:"); Serial.println(msgType, HEX); Serial.print("MsgData: "); Serial.println(msgData, HEX); */
byte GetData[] = {source, len, destination, msgType, msgData, 0x00}; byte chkSum = getCheckSum(GetData); int chkSumLocation = sizeof(GetData) -1; /* DEBUG Serial.print("chkLoc: "); Serial.println(chkSumLocation); */
GetData[chkSumLocation] = chkSum;
for (int i = 0; i < sizeof(GetData); i++) { Serial.print(GetData[i], HEX); } //Serial.println(""); }
int getCheckSum(byte *string) { int i; int checksum; int length; checksum = 0; length = string[1]+1; //Serial.print("length: "); //Serial.println(length); for (i = 0; i < length; i++) { checksum ^= string[i]; } return checksum; }
Also, while printing the packet out i am only getting one digit if the number printed is less than 10, for example instead of getting 04050610 i get 45610. any ideas? Many thanks!
|
|
|
|
|
7
|
Using Arduino / Microcontrollers / Re: Attiny Breakout Board - TinyDuino
|
on: December 10, 2011, 10:57:51 am
|
|
The Attiny4313, pin for pin compatible witht the 2313 has enough space for a bootloader, not sure if anyones tried it though, and also has a UART. The easiest way to use that will be with an FTDI connector as it'll require no more external parts.
|
|
|
|
|
10
|
Using Arduino / Microcontrollers / Re: Arduino style board for AtTiny2313
|
on: September 19, 2011, 04:02:59 pm
|
|
You can probably have one instead of the switch, or i could put pads on the back and you can leave off the switch? Any thing else you want? I was thinking of putting inpads to disable/enable auto reset if ppl are going to use the bootloader?
|
|
|
|
|
12
|
Using Arduino / Microcontrollers / Arduino style board for AtTiny2313
|
on: September 18, 2011, 05:02:22 pm
|
Hi, Im looking at throwing together some PCBs for an arduino style board using the AtTiny2313 MCU. Its pretty much a bare bones board but it might be useful to more people than just me. I havent finished the SCH or PCB files, but it'll give you an idea of what im aiming at. I'll be looking to get a few PCBs made in the next few weeks, let me know if your interested and i'll get some to you for cost. Havent been priced up yet, but it wont be much more than 7/8 quid I would think.  Ta, Dave
|
|
|
|
|
14
|
Forum 2005-2010 (read only) / Troubleshooting / Serial Input in Windows 7
|
on: June 03, 2009, 12:46:21 pm
|
Hi Im trying to use Arduino-15 in windows 7 and it seems to work until i try and use the serial terminal in the IDE. It just kicks back random symbols. For instance the following code : void setup() { Serial.begin(9600); }
void loop() { Serial.println("A"); delay(1000); }
returns this : @ó@ó8ó@ó<ó@ù@ó
I started using the luminet port and thought it might have been a problem with that but its happenign with the normal IDE too. Any Ideas?
|
|
|
|
|
15
|
Forum 2005-2010 (read only) / Syntax & Programs / Re: Firmata and VB
|
on: February 20, 2009, 10:56:51 am
|
|
Im also interested in this, I have VB communicating with a board and values being transfered, but i would like to use this protocol or the simpler messaging protocol. I would be willing to share my code, can you do the same if you have found any solutions?
Thanks
|
|
|
|
|