problem with unpack()

Discuss PocketC for Palm OS (including Desktop Edition)

Postby Doug on Tue Feb 24, 2004 8:03 pm

// The problem I am having is with the unpack() function.
// I can't get it to extract info to the data array
// Can someone point out the error in the following test code?
// TIA, Doug

//---------------- code below
//Unpack Test 1.0
@cid "UPAK";
@name "Upack";
@dbname "Upack";
@category "IR";
@ver "1.0";

string message="Upack 1.0\n";
// Signon message

#define SWITCH1 0
#define PACKETSIZE 2
#define BUFFSIZE 10

string csum1 = "90";
string csum2[3] = { "89\0" };
int sw1;

main() {
int data1[BUFFSIZE], data2[BUFFSIZE];
// # elements in the string

text(0,10, csum1 + " " + *csum2);
// Print test 'strings'

// unpack(data1, csum1, "1", PACKETSIZE);
// The previous statement should be the proper syntax
// but generates a Virtual Machine Error
// Addr:81,Func:main,Global ref out of range
// at run-time.

unpack(data1, &csum1, "2", PACKETSIZE);
// Should extract hex string to int
// does not error but computes incorrect result.
// Syntax does not seem correct.

sw1 = data1[SWITCH1];
text(0, 30, sw1);
// displays a '0'

// Following statement is a char array
// instead of string, results are the same
unpack(data2, csum2, "2", PACKETSIZE);
// Should extract hex string to int

sw1 = data2[SWITCH1];
text(0, 40, sw1);

Posts: 6
Joined: Tue Feb 17, 2004 2:42 pm
Location: USA

Postby dewey on Wed Feb 25, 2004 2:29 am

The second parameter to unpack, pSerData, is a pointer to either an array or ints or array of chars. You are passing it a string, which is what is causing the VM errors.

For example, the way csum1 should have been declared is:
char csum1[2] = { '9', '0' };

Site Admin
Posts: 749
Joined: Sun Oct 21, 2007 5:12 am

Postby Doug on Wed Feb 25, 2004 9:39 pm

Thanks Jeremy, that was the missing piece. I have since discovered that unpack() does not provide the format that I need. I am getting the 34 byte data packet (in ascii hex format) at 57,600 baud and need to convert to int as fast as possible to update a display every 60mS.

unpack() takes hex data and converts it to int so my '90' string becomes 0x39(57 decimal), 0x30(48) in successive array locations (for 1 byte ints) and 14,640 for a 2 byte int ((57 * 256) + 48). It should be 0x90 (144 decimal).

Right now I am converting each byte pair individually and adding them together i.e.: '91' = (('9' - '0') * 16) + ('1'-'0'). Doing this 17 times creates a lot of overhead not even counting the case of 'A'-'F'.

Anyone have any ideas on how to translate hex ascii byte pairs to an int array really fast?
Posts: 6
Joined: Tue Feb 17, 2004 2:42 pm
Location: USA

Postby Doug on Wed Feb 25, 2004 9:40 pm

>> It should be 0x90 (144 decimal).

Should read:
I need it to be 0x90 (144 decimal).
Posts: 6
Joined: Tue Feb 17, 2004 2:42 pm
Location: USA

Postby Vigon on Thu Feb 26, 2004 4:45 am

Some code may help...
(Best time in Palm IIIx was 230 millisecons)
(Best time in Sony Clie SJ20 was 140 millisecons)
Code: Select all
//By Vigon (2004)
int i,Time;
int Integers[17];
string Hexstring;

//InBuffer is Filled for test purpose.
//Use serrecva(InBuffer,34) to fill it in your program;
char InBuffer[35]={   

InBuffer[34]=0;      //append 0 at the end for the use of ctostr()


for(i=0;i<17;i++)Integers[i]=(int)("0x" + substr(Hexstring, i*2, 2));

puts("Execution Time="+(ticks()-Time)+"0 Millisecons\n");   //Elapsed Time.

for(i=0;i<6;i++) puts(Integers[i]+"\n");      //Show the first 6 results.
Posts: 43
Joined: Wed Apr 17, 2002 2:26 pm
Location: Venezuela

Postby Doug on Thu Feb 26, 2004 8:23 pm

Thanks Vigon,

The statement:
Integers[i]=(int)("0x" + substr(Hexstring, i*2, 2));

shows me a PocketC conversion trick that I didn't know and it helps me out. Thanks for the tip.

Posts: 6
Joined: Tue Feb 17, 2004 2:42 pm
Location: USA

Return to PocketC for Palm OS

Who is online

Users browsing this forum: No registered users and 1 guest