PDA

View Full Version : Need help capturing a response.



electromanj
11-12-2010, 04:57 AM
Hello everyone! I have a question that is probably simple for you, but I don't know where to start.

I am sending commands to a controller and then getting a response. This is my code:


pub send_telegram
waitcnt(clkfreq/100+cnt)
debug.str(string("sending telegram",cr))
waitcnt(clkfreq*2+cnt) 'wait for at least 3.5 character intervals
plc.tx($01)
plc.tx($03)
plc.tx($40)
plc.tx($9B)
plc.tx($00)
plc.tx($02)
plc.tx($A0)
plc.tx($24)
waitcnt(clkfreq*2+cnt)'wait for at least 3.5 character intervals
GetResponse
Pub GetResponse
repeat
rx:=GetDataByte


PUB GetDataByte|Flag,Data,_Rx

Flag := 0
repeat while Flag == 0
Data := plc.rxcheck
if Data <> -1
_Rx := Data
Flag := 1

Result := _Rx
debug.tx(34) 'places a " between hex characters so I can tell them apart
debug.hex(_RX,2)

I am reading the response fine on the parallax serial terminal.
My question is... how would I capture the response and hold it so I can parse it out later as useful data?

Many thanks in advance.

ElectricAye
11-12-2010, 05:08 AM
You can store data on an EEPROM or record it on an SD card. Is that the kind of thing you want to do?

electromanj
11-12-2010, 05:15 AM
No sorry I was not very clear. I can read the response from the controller, but I don't know how to "store" the response so I can work with it. Maybey I already am and don't know it? I am completely lost here. I can read the response on the serial terminal, but I would like the propeller to read the response and parse data from the response then perform an action. I hope this makes sense.

electromanj
11-12-2010, 05:34 AM
In other projects I've done when I get data from a sensor I store it in a variable and then do something with that variable.
For instance RC time on a pot. I get the rc time decay and then compare it with a table to decipher where the pot is at.
With my current program I have figured out how to display the response from the controller on the serial terminal, but I dont know how to capture the response within the program to get info from it so the program can use that data for actions. Mostly because I'm a moron.

Mike Green
11-12-2010, 05:49 AM
Well, you could store the response in a variable. What does the response look like?

electromanj
11-12-2010, 06:01 AM
Hello Mike,
The resonse is actually similiar to the telegram.
For instance send: 01 03 40 9B 00 02 A0 24
recieve: 01 03 3f 00 01 C4 D4 F6

The response is not exact but that's the format.

Mike Green
11-12-2010, 06:09 AM
You have not described a format, just given a single example of it. How about describing a pattern or structure of the byte strings?

Have a look at Extended FullDuplexSerial in the Object Exchange. It's an extension for FullDuplexSerial for receiving character strings terminated by a carriage return, but the same technique can be used for receiving other byte strings of information and processing the bytes to derive something useful.

electromanj
11-12-2010, 06:21 AM
The protocol is modbus RTU

The message would be: 01 03 3F 29 00 02 19 D7
The response would be: 01 03 04 00 00 00 C4 FB A0

Message:
01 -slave address
03 -read register
3F, 29 -parameter to be read
00, 02- 32 bit register
19 ,D7- CRC

electromanj
11-12-2010, 06:29 AM
I'm not 100% clear on this yet, but I think the response goes like this.

01-slave adress
03-read register
04- byte count to follow
00,00,00,C4- result of the read
FB,A0- return CRC

Ariba
11-12-2010, 07:51 AM
You can do something like that to parse the response:

PUB GetResponse : value | cmd, bcnt, crc
repeat until plc.rx == $01 'wait for first byte (slave address)
cmd := plc.rx 'get command
bcnt := plc.rx 'get byte count
value := 0
repeat bcnt 'get value
value := value << 8 + plc.rx
crc := plc.rx << 8 + plc.rx 'get crc


But depending of the number of possible responds and slaves, you might need a more complex parser, and also test if the command is right.

Andy

electromanj
11-12-2010, 10:30 PM
That worked out great! Thank you very much.

I Appriciate everyone's help!