trouble with serin on 40x2

Schu1212

New Member
I am working with serial communications for another project I'm working on. What happens is when I poll a device using serout and immediately use serin after that to wait for a response, the data I receive isn't correct. But if I use hserout and follow it immediately with a serin command, I get the correct data.

this example returns incorrect data:
serout b.6,t9600_8,(1,$37)
serin [10,timeout_1],b.5,t9600_8,device_data

This example returns the correct data:
hsersetup b9600_8,%001
hserout 0,(1,$37)
serin [10,timeout_1],b.5,t9600_8,device_data

Does anyone have any idea why this would happen? The only thing I am changing with these two examples is what pin I'm wired to on the picaxe and the code syntax because of hserout and serout. That's all that I see, am I missing something?
 

MartinM57

Moderator
...the data I receive isn't correct...
What isn't correct about it - there is none (i.e. the timeout occurs), off by 1, total garbage, always the same wrong answer/always a different wrong answer etc?

Any chance the device (what is it - and home made or commercial?) is thinking it's received some other command (i.e does the data received by serin look like the output from a different command to the device?)
 

hippy

Ex-Staff (retired)
inglewoodpete may have spotted the problem in this case. Another potential problem may be how quickly the device responds once sent to.

Using SEROUT and then SERIN, the PICAXE will wait until it has sent its data and only then commence executing the SERIN and some time later be ready to receive the response. With HSEROUT, relying on background hardware and not bit-banging, as soon as the data is prepared to be sent the SERIN will begin to execute at the same time as the data is being sent. The SERIN will be ready to receive more quickly than with SEROUT.

This is often not a problem in practice as a receiver will take some time to respond to data and deliver its response. If it is a problem then either use HSEROUT with SERIN, SEROUT and HSERIN background receive, or use HSEROUT and HSERIN background receive, or run at a higher SETFREQ frequency which will reduce the time between the SEROUT and being ready to receive in the SERIN.

If you have controll over the receiver's response ( if it's another PICAXE, or an own-wrtten micro or PC program ) you can add appropriate delays so the SERIN will always be ready after SEROUT has finished.
 

Schu1212

New Member
thanks for the responses everyone. OK, here are some responses to questions you guys had. The device I'm communicating with is receiving the correct data when I transmit to it. It functions and does everything it's supposed to do. But after each command it receives, it sends back an acknowledgement to let me know it is respsonding. Also, when using serout, I am setting the output high shortly before transmitting so my first byte of data isn't corrupted. So I have that aspect under control. The data that I'm receiving that isn't correct is just a single byte of data. When I am supposed to receive $50 as an acknowledge, I get something totally different, I am getting $f9. But the data I receive is always the same, so it's consistent. But consistently wrong. I was wondering if it had anything to do with what you said hippy, the timing could be quite fast and not be allowing serin to get the correct response. I will do a little investigating today and see if I can make any progress. I'll get back to you guys with more info when I know more. Thanks again for the input.
 

hippy

Ex-Staff (retired)
Receiving $F9 when $50 expected does feel to me as a slowness to respond. You can try changing ...

serout b.6,t9600_8,(1,$37)
serin [10,timeout_1],b.5,t9600_8,device_data

to ...

setfreq m32
pause 10
serout b.6,t9600_32,(1,$37)
serin [40,timeout_1],b.5,t9600_32,device_data
setfreq m8
 

Schu1212

New Member
It was a speed issue, I bumped the frequency up to 16MHz and it worked fine. 8 must have been just a little too slow like you said hippy. Thanks for the help!
 

westaust55

Moderator
I am working with serial communications for another project I'm working on. What happens is when I poll a device using serout and immediately use serin after that to wait for a response, the data I receive isn't correct. But if I use hserout and follow it immediately with a serin command, I get the correct data.

this example returns incorrect data:
serout b.6,t9600_8,(1,$37)
serin [10,timeout_1],b.5,t9600_8,device_data

This example returns the correct data:
hsersetup b9600_8,%001
hserout 0,(1,$37)
serin [10,timeout_1],b.5,t9600_8,device_data

Does anyone have any idea why this would happen? The only thing I am changing with these two examples is what pin I'm wired to on the picaxe and the code syntax because of hserout and serout. That's all that I see, am I missing something?
In your second example I am guessing that you are in fact using HSERIN (not serin) and the rest of that line is different as well. :confused: are are you actually using hserout and serin :confused:

But you seemingly have the initial (speed related) problem solved anyway.
 
Last edited:

Schu1212

New Member
Yes I am in fact using Hserout and serin. I'm gearing up to be able to communicate with several pieces of equipment and at that time I will need hserin to background receive so that's why I am using serin right now. To make sure everything communicates correctly and works smoothly on my next project I am simply trying to use the same programming I will use next time as well. My plan is to have multiple devices that have a unique identifier hooked up to my hserout line. These devices are only polled for a status report so each device will have it's transmit line hooked up to an individual serin because that is the only time I will need to monitor that line for a response. With Hserin I am using that to get data from other picaxe chips I will have hooked up that will need to communicate at any given time, so I will need to background serial receive with hserin. It might sound a little confusing, but I'm hoping it will work :)
 
Top