PDA

View Full Version : Help!! serin problem



danlo
01-23-2007, 06:57 PM
I want to receiver a string, and then write·it·into EEPROM
There is my code

'EEPROM
Totalprice··· DATA·· 0
price·········· VAR···· Byte(5)

'Program code
· SERIN 9,813, [STR price\5\"#"]
· DEBUG STR price
· WRITE totalprice,price
·· .....
·· .
For example,
price = 321, totalprice should be equal to 321
but the real result of totalprice = 3, only write the first character of price....why?

What wrong of my programming code? Please Help!!

Post Edited (danlo) : 1/23/2007 12:08:37 PM GMT

Bruce Bates
01-23-2007, 07:36 PM
danlo -

When you read in the string you did so this way:

SERIN 9,813, [STR price\5\"#"]

and you told SERIN you wanted up to 5 bytes (price\5\"#") as you should. However, when you went to WRITE the data, you didn't specify anything in your WRITE command, nor for that matter could you:

WRITE totalprice,price

The variable "price" is an array of single BYTES, of 5 in number. Why would you think that WRITE would presume to use more than one BYTE? Additionally "totalprice" (a rather odd name for a location index!) is never incremented, so you will only EVER WRITE into EEPROM location ZERO, and nowhere else.

The following would work (just as a simple example) but it's very cumbersome and not sufficiently universal:

WRITE totalprice,price(0), totalprice+1,price(1),totalprice+2,price(2),totalp rice+3,price(3) ... etc.

You will need to perform an iterative WRITE for the proper number of BYTES based on the size of the individual input received each time AND you need to increment the EEPROM location for each BYTE to be written. Also remember to clear your array to some unused characters each time, so you can locate the end of the individual (unknown length) input strings.

Regards,

Bruce Bates

Post Edited (Bruce Bates) : 1/23/2007 12:52:10 PM GMT

Unsoundcode
01-24-2007, 12:04 AM
Hi danlo, you are almost there, as Bruce pointed out the problem is that you are trying to write the whole array (price) into memory location 0 and what you need to be doing is writing one element at a time and indexing the memory location as you go. Here is an alternative solution using an index variable to iterate through the memory locations and the array.

Totalprice DATA 0
price VAR Byte(5)
price_data VAR Byte
index VAR Nib

· 'SERIN 9,813, [STR price\5\"#"]
·· DEBUGIN STR price\5
· DEBUG STR price,CR

· FOR index =0 TO 4
· WRITE totalprice+index,price(index)
· NEXT

· FOR index =0 TO 4
· READ totalprice+index,price_data
· DEBUG price_data
· NEXT
· READ Totalprice+2,price_data
· DEBUG CR,price_data

Jeff T.

danlo
01-26-2007, 11:24 AM
Jeff.T

thank you so much