Problem using serrxd timeout

Ramy

New Member
Hi,
I'm trying to add a timeout command to my code but it's not working correctly. My code is basically this:

Code:
main:
let b2 = 0
let b1 = 0

serrxd [1000,timeout],b2,b1

if b2 = 20 then led
goto main

led:
let pins = b1
goto main

timeout:
let pins = 0
low portc 1
low portc 0
goto main
Without the timeout, my code works perfectly, but when I add the timeout command, the PIC starts switching on the wrong outputs. Eg. If I send the PIC "020 002" by serial, it should switch on output 1, but with the timeout command added, it sometimes switches on output 1, sometimes output 0, sometimes both and sometimes a different output.

Please tell me what's going wrong here. Thankyou
 

BeanieBots

Moderator
The line
serrxd [1000,timeout],b2,b1

is expecting to receive TWO bytes of data. Both in the range 0 to 255.
The string "020 002" is 7 bytes long. (including the space)

Also, numbers sent as ASCII text will need to be converted back to numbers.
For example, if you send "1", it will be recieved as 49. ($30 in hex).

I believe (never tried it myself) you can use the '#' operator with serrxd.
Thus, serrxd [1000,timeout],#b2,#b1 would be able to recieve TWO SINGLE DIGIT numbers in ASCII format. eg "12" or "34" or "99".

If you want to receive numbers in the format "020 002", you need to assign a variable for EACH character sent and convert accordingly.
 

Ramy

New Member
Sorry I should have been clearer on this. I'm not sending it as Ascii, I'm sending it as just decimal values.

As I said before, if I remove the timeout command so that it just says "serrxd b2,b1", then it works perfectly.
 

BeanieBots

Moderator
OK, sorry I got the wrong end of the stick!

The only thing I can think of is that you may be getting a partial dataset between timeouts.
Something to try would be to add an extra variable and test for that AS WELL as b2=20.
Hopefully, somebody with more knowledge than myself can see something.
 

hippy

Ex-Staff (retired)
Worked okay and reliably for me. I changed if b2=20 to if b2="Z" so I could send data from the Programming Editor Terminal.

Which PICAXE are you using, with what ( and how ) are you sending the data ?

You could perhaps add a SERTXD to report back what's actually being received.
 

Technical

Technical Support
Staff member
If the timeout code (low portc etc.) happens to be processing whilst you send data, the data could become corrupt because only part of the data will be received by the time the PICAXE gets back to the serrxd line - ie the serrxd lines starts part way through a byte.

The way to overcome this is is to use a qualifier at the start of your code sent from the computer e.g

serrxd [1000,timeout],("?"),b1,b2

Then b1 and b2 will only ever get data if the correct "?',X,Y data is received.
 

Ramy

New Member
Thanks for the suggestions. I will try to add in a sertxd command to see what data the pic is receiving.

I should have mentioned this earlier. I'm using a 28X1 which is running at 8mhz without an external resonator, and I'm sending data from the computer to the PIC.

I'm not sure if I can add a qualifier, because I'm using Logicator 2004 to send the commands to the pic and I don't have much control over what commands it sends to the PIC.
 
Last edited:

Ramy

New Member
I've added a sertxd command to see what data the pic is receiving. If I send "020 002", the value of b1 ends up being anything between 1 and 3.

The error occurs mostly when I'm sending the PIC commands very quickly. Does the timeout command affect the PIC's ability to interpret serial data quickly? Is there a workaround for this?
 

Technical

Technical Support
Staff member
Does the timeout command affect the PIC's ability to interpret serial data quickly? Is there a workaround for this?
See post #6 above - it answers your question before you asked it! The quicker the data the more likely it is to be corrupt. The workaround solution is also there - use a qualifier!
 
Top