PICAXE 20M2 serial communication problem

willo132

New Member
Hi, I am having trouble with the serial interface, using commands SERRXD and SERTXD through the PICAXE cable from a host pc. I am using the PICAXE 20M2, I have also tried the 20X2.

I am working on a prject to probe a pin to check the state. The host computer sends a command to the picaxe and and the picaxe sends back the relevent information. I have written the code and all works fine throught the serial terminal from the picaxe editor. When I try to send commands to the serial from a python script the picaxe or pc seems to drop a number from the command being sent to the picaxe.

so I wrote a small script to recieve a command through the serial in, as a number i.e 99, then send the recieved command back to the host pc. As i said beofore the serial terminal from the picaxe editor works perfectly, but as soon as i send the command from a python script it seems to drop the a number from the command and recieves 9 not 99 intermitantly (sometimes I get 99 and others I get 9)

Code:
pause 3000
disconnect
symbol CMD = w11
main:
    
    serrxd[2000, main], #CMD
    sertxd("Recieved: ",#CMD,cr,lf)
    goto main
One of the things that really confuses me is that sometimes the picaxe will recieve the correct command and act accordingly but most of the time it will drop a digit from the command. It really seems random weather it recieves the command or drops a digit.

I have added the python results as well as the picaxe editor serial terminal results. Through the editor I always get the correct responce but from python its random

picaxe_editor.jpg

pythonser.png

I have a PICAXE 18M2 that i have tried the same code with and the command seems to work with no problem from python (i still need to test again but seems fine). Does anyone know why there is a difference between the PICAXE 20 chips and the 18 chips serial in? Also i would rather not redesign my circuit for the 18M2 and would like to get the 20M2 working if possible. If anyone can help i would appriciate it greatly.

Thanks in advance.
 

papaof2

Senior Member
I'd suspect the PICAXE isn't quite fast enough to handle serial bytes as sent by anything other than the PICAXE editor which seems to put in a bit of delay between consecutive characters. Maybe have python send a character, loop for a count of 5 or ten and then send the next character?
Or try the hardware serial commands instead?
 

willo132

New Member
I'd suspect the PICAXE isn't quite fast enough to handle serial bytes as sent by anything other than the PICAXE editor which seems to put in a bit of delay between consecutive characters. Maybe have python send a character, loop for a count of 5 or ten and then send the next character?
Or try the hardware serial commands instead?
Thank you, I will try this later on and post the outcome.

Can I still use the PICAXE cable with the hardware serial?
 

steliosm

Senior Member
Hello.
How many characters are you sending from the python script? I can see in the code for picaxe that you expect a single byte but you send 3 bytes (CMD, CR, LF). Can you try sending just one character from the python script? I see in the console image that you get a response back, is this the 18m2 chip?
 

willo132

New Member
In the console I get a response back from all chips tested 18M2, 20M2 and 20X2. When sending command 99crlf the 99 is the bytes to be sent and the crlf represents carriage return /r and line feed /n as these need to be at the end of the transmission. Basically I want to send 99 to initiate a probe command and 98 for a reset command and 97 for a reprogram command, everything works with no problem in the picaxe console. But when I move to python it only receives the first digit most of the time, and other times both digits. Seems random to be fair, hopefully adding a delay between the characters will work.

Cheers
 

AllyCat

Senior Member
Hi,

All the SERRXD and SERTXD instructions are performed by software "Bit Banging" the input and output pins, so not only cannot the PICaxe "multi-task" (Full Duplex), but it needs significant delays between the SERRXD and SERTXD instructions for the slow interpreted Basic program to execute the other (main program) tasks. Also, the SERRXD and SERTXD commands attempt (and will probably fail) to send at a higher baud rate than is usual with the SERIN and SEROUT instructions.

So the first thing to try is slower SERIN and SEROUT commands and/or with a higher clock frequency, e.g. N2400_16 (for 2400 baud, Setfreq 16 MHz, Idle Low). There's no point in changing the clock frequency with SERRXD or SERTXD because their Baud rate is a fixed divisor of the clock rate. Note that the "SEROUT/IN" commands cannot normally be used on the same pin(s) as the SERRXD/TXD commands, but there are exceptions, particularly on the 08M2.

Yes, certain parts of the 18M2 (and 08M2) Hardware do operate somewhat faster than the 14/20M2 (and certainly faster than the 20X2 at the same clock rate). One reason is that the 14/20 pin "base PIC" chips have a different internal bus arrangement (A, B and C ports) compared with the PICaxe's external ports (mainly B and C), so some delays occur with the "re-mapping" of the pins. That's one reason why the "AXE133/4" LCD/OLED driver boards still use the 18M2.

For high baud rates, the best solution is to use the HSERIN and HSEROUT commands, although these are probably still not "Full Duplex" (at least with the M2s). With the 20X2 you can use "Background Receive", but the M2 chips have several "issues". Their hardware cannot receive the "Idle Low" (N) protocol which the PICaxe Terminal transmits, and there are some Bugs in the working of the HSERIN command. Much of this is now "solved" in several threads, to which I can link, if that's the way you wish or need to proceed.

PS: Yes, also the Picaxe Editor introduces a very significant delay between each character. The "default" is nominally 5 ms, but it's usually much longer (~20 ms !).

Cheers, Alan.
 

willo132

New Member
Thank you so much for your detailed description Alan, it is most helpful. I will have a proper read in the morning and adapt my script, maybe the circuit, to account for the slow picaxe speed. I will try sending single characters with a delay first, if this does not fix the issue ill go through your suggestions one by one.

Finding this information in the manuals can be quite a task, thanks for your help.

Cheers

Will
 
Top