Parallax Propeller P8X32 chip serial interface .spin code.
Mano from Netherlands  [27 posts]
7 years
I have a parallax robot control board wich holds the P8X32 chip.
It is used very much allthough no-one seems to know how to properly interface to and from roborealm.

So what i'm looking for is the .spin code for the propeller that understands the string that is beeing send by roborealm, splits it up into seperate value's and links every value to a variable.

Just to be clear; I don't use the propeller "servo" controller but the 32 bit allround microcontroller chip.

@ RoboRealm: a plugin for this platform would be widely apreciated!!

Any help is welcome!!! i have been struggeling for weeks now...
Anonymous 7 years

What have you tried? Do you have any spin code that reads the serial line from a PC?

If you look and search for the serial module within this forum you will find many examples of communicating to a BS2, PIC, etc. using the serial module. That module allows you to determine your own protocol that can be much simpler than a full module implementation that is typically more generic than it needs to be for a specific task.

If you can post some code that you've been working on I think you'll get a better response.

Mano from Netherlands  [27 posts] 7 years
I have tried many scripts but below is the best one. I does receive the value but also the second value. so if i ask to blink the led when value is higher than 300, it also light up when the second value is >300.

I have (evantually) a array of 10 values in one string. for now i have two. MOUSE_X and MOUSE_Y.

_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000

rxpin = 31 ' Receive on I/O pin 1 <- black from master black txpin p1
txpin = 30 ' Transmit on I/O pin 0 -> red to master red rxpin p0

LONG myInput
LONG txVal_1, txVal_2 ' transmit
LONG rxVal_1, rxVal_2 ' receive from master and PST

{************************************************* *****************}

serial : "Extended_FDSerial" '' "FullDuplexSerial"
f : "Float32Full"

{************************************************* ****************}
PUB Main | DutyCycle ' new stuff DutyCycle

serial.start(rxpin,txpin,%0000, 115200) '- rxpin,txpin,mode,Baud


    IF rxVal_1 > 300
      outa[1] := %011110

PUB BlinkLeds


    outa[1] := %100001
    waitcnt(clkfreq/4 + cnt)
    outa[1] := %011110
    waitcnt(clkfreq/4 + cnt)
    outa[1] := %100001
    waitcnt(clkfreq/4 + cnt)
    outa[1] := %011110
    waitcnt(clkfreq/4 + cnt)
    outa[1] := %100001
    waitcnt(clkfreq/4 + cnt)
    outa[1] := %011110
    waitcnt(clkfreq/4 + cnt)

' receive datas as integers
myInput := serial.rxDec

PUB Receiver

rxVal_1 := serial.rxDec

PUB forwardTransmitter

txVal_1 := 6
PUB Transmitter

Mano from Netherlands  [27 posts] 7 years
FYI, i do use the serial plugin of roborealm.
Anonymous 7 years
Do you have any SPIN code example that use serial?

Mano from Netherlands  [27 posts] 7 years
The code i posted is that spin code. Or do you mean the drivers behind that?
Mano from Netherlands  [27 posts] 7 years
Mano from Netherlands  [27 posts] 7 years
here is the complete project....
Anonymous 7 years
Ah, yes, sorry, didn't see your previous post.

The easiest way to get this to work is to send binary data over the serial com. So the serial RoboRealm script should look something like


(those are double back slashes) which would transmit a total of 8 bytes (two 4 byte integers) to the propeller. Your propeller receive would then be something like

PUB Receiver

rxVal_1 := RX
rxVal_2 := RX
rxVal_3 := RX
rxVal_4 := RX

rxVal := rxVal_1 | (rxVal_2<<8) | (rxVal_3<<16) | (rxVal_4<<24)


which would then be used TWICE to read in the full 8 bytes or two integers.

See if this works and we can go from there.

(Note this was not syntax checked so there may be some minor errors)

Should you want to keep this script your Robofile Serial module would have to be something like


Since that serial package expects a newline to delimit a string number.

Anonymous 7 years
Actually that last line should read


as \n is 10 and \r is 13

Mano from Netherlands  [27 posts] 7 years
Ok, This is my output after implementing your code.

this is the input string from roborealm:

That outputs the following to the propeller:

00011: \r\r47\r67\r447 -------->send string
00012: 1545615156<cr>---|
00013: 879910002<cr> ----|-->Result from propeller
00014: 913464375<cr>-----|
00015: 879909943<cr>-----|
00016: \r\r47\r61\r448
00017: 1545615156<cr>
00018: 879910002<cr>
00019: 913464375<cr>
00020: 879909937<cr>
00021: \r\r47\r67\r447
00022: 1545615412<cr>
00023: 879910002<cr>
00024: 913464375<cr>
00025: 879909943<cr>
00026: \r\r47\r66\r446
00027: 1545615156<cr>
00028: 879910002<cr>
00029: 913464375<cr>
00030: 879909942<cr>
00031: \r\r47\r64\r447
00032: 1545614900<cr>
00033: 879910002<cr>
00034: 913464375<cr>
00035: 879909940<cr>
00036: \r\r47\r64\r448
00037: 1545615156<cr>
00038: 879910002<cr>
00039: 913464375<cr>
00040: 879909940<cr>
00041: \r\r47\r71\r448
00042: 1545615412<cr>
00043: 879910002<cr>
00044: 930241591<cr>
00045: 879909937<cr>
00046: \r\r47\r72\r448
00047: 1545615412<cr>
00048: 879910002<cr>
00049: 930241591<cr>
00050: 879909938<cr>
Mano from Netherlands  [27 posts] 7 years
i just noticed that the mouse value was empty. this is because i forgot to move my mouse over the screen.

so i just wanted to post a piece of code that was correct, when i noticed this....

When i move over the screen and the mouse value is filled instead of before, the output gets scrambled.

00071: \r\r47\r64\r447
00072: 1545615156<cr>
00073: 879910002<cr>
00074: 913464375<cr>
00075: 879909940<cr>
00076: \r517\r175\r420\r448
00077: 1545615156<cr>
00078: 540554292<cr>
00079: \r504\r132\r418\r448
00080: 808809052<cr>
00081: \r549\r135\r422\r448
00082: 896687136<cr>
00083: \r544\r169\r428\r448
00084: 1918640184<cr>
00085: \r544\r177\r427\r448
00086: 1545615412<cr>
00087: 540554292<cr>
Mano from Netherlands  [27 posts] 7 years
set the baud rate to 38400. that solved the scrambling. Though now i got 5 values back instead of the four sent.
Anonymous 7 years
I assume you mean the code that expects a 4 byte binary number?

Try this one instead (you forgot the double \\ in front of the variables)


The \r's are ONLY needed if you continued to use your script and the rxDec library ... which if you are using the code we sent you are not. The above should do the trick.

Mano from Netherlands  [27 posts] 7 years
This is what i get now is this;

00168: \244\1\0\09\1\0\0&\2\0\0\192\1\0\0
00169: 500<cr>
00170: 313<cr>
00171: 550<cr>
00172: 448<cr>
00173: \244\1\0\09\1\0\0'\2\0\0\192\1\0\0
00174: 128032<cr>
00175: 80128<cr>
00176: 141056<cr>
00177: 114688<cr>
00178: \244\1\0\09\1\0\0(\2\0\0\192\1\0\0
00179: 32776192<cr>
00180: 20512768<cr>
00181: 36175872<cr>
00182: 29360128<cr>
00183: \244\1\0\09\1\0\0&\2\0\0\192\1\0\0
00184: -199229440<cr>
00185: 956301313<cr>
00186: 637534209<cr>
00187: -1073741822<cr>
00188: 536870913<cr>
00189: \244\1\0\09\1\0\0'\2\0\0\192\1\0\0
00190: 500<cr>
00191: 313<cr>
00192: 551<cr>
00193: 448<cr>
00194: .\2\0\0\177\0\0\0(\2\0\0\192\1\0\0
00195: 142880<cr>
00196: 45312<cr>
00197: 141312<cr>
00198: 114688<cr>
00199: .\2\0\0\177\0\0\0(\2\0\0\192\1\0\0
00200: 36577280<cr>
00201: 11599872<cr>
00202: 36175872<cr>
00203: 29360128<cr>
00204: "\2\0\0\207\0\0\0(\2\0\0\192\1\0\0
00205: 572522496<cr>
00206: -822083582<cr>
00207: 671088640<cr>
00208: -1073741822<cr>
00209: 536870913<cr>
00210: "\2\0\0\207\0\0\0)\2\0\0\192\1\0\0
00211: 546<cr>
00212: 207<cr>
00213: 553<cr>
00214: 448<cr>

it seems to count up numbers.

here is my main code:

PUB Receiver

rxVal_1 := serial.rx
rxVal_2 := serial.rx
rxVal_3 := serial.rx
rxVal_4 := serial.rx

rxVal := rxVal_1 | (rxVal_2<<8) | (rxVal_3<<16) | (rxVal_4<<24)


PUB Transmitter

serial.dec(rxVal_1 | (rxVal_2<<8) | (rxVal_3<<16) | (rxVal_4<<24))
Anonymous 7 years

There is a space after


that is getting trasmitted too that you don't see. Edit the serial module and just delete that space at the end of that string. That should solve the issue.

Mano from Netherlands  [27 posts] 7 years
yes! that worked!

00437: \245\1\0\0\12\1\0\0\212\1\0\0\192\1\0\0
00438: 501<cr>
00439: 268<cr>
00440: 468<cr>
00441: 448<cr>
00442: \245\1\0\0\12\1\0\0\231\1\0\0\192\1\0\0
00443: 501<cr>
00444: 268<cr>
00445: 487<cr>
00446: 448<cr>
00447: \245\1\0\0\12\1\0\0\237\1\0\0\192\1\0\0
00448: 501<cr>
00449: 268<cr>
00450: 493<cr>
00451: 448<cr>
00452: \245\1\0\0\12\1\0\0\228\1\0\0\192\1\0\0
00453: 501<cr>
00454: 268<cr>
00455: 484<cr>
00456: 448<cr>
00457: \245\1\0\0\12\1\0\0\215\1\0\0\192\1\0\0
00458: 501<cr>
00459: 268<cr>
00460: 471<cr>
00461: 448<cr>
Mano from Netherlands  [27 posts] 7 years
Thank you so much! This is great. Once i will have everything setup i will ask if roborealm can add it to the site.

Is there a rule for how many variables i can add to the string?
Anonymous 7 years
There isn't any limit to how many you can add other than the bandwidth that will be consumed each time you add a variable. For example, 38400 is what you have to work with. Adding too many variables can cause it to slow. If you don't need all the variables each time you can create a more advanced protocol that first sends an id byte and then the actual value. The id would then be used to identify what value it is.

That complexity is what we normally do when we create modules specific to a particular processor ... something we may still do with the propeller but not anytime soon enough for your project.

Have fun!
Anonymous 7 years
I have been struggling for sone time bow with the code you gave me. There seems to be a problem.

The value_1 is received correctly. But value_2 doesn't seem to work. It ias beeing sent. And also send back to roborealm. But if i try to drive a servo with value_2 it doesn't move!

I can't find the problem myself. The value's are received in opposite order. So value_1 becones 4 etc..

Anonymous 7 years

Try using the servo_2 value to drive servo_1 so that you can eliminate if this is a software or hardware issue. If the servo_1 does not work (and it does when its own value is used) then you know that it is a software issue, likewise if it does work you will know that it is a hardware issue. Narrowing down the cause of the problem will help to resolve this.

If it is a software issue, can you zip up the spin and robofiles that you are using just so that we are sure we are looking at the same stuff that you are currently running as it has been a while since the previous  posts were made.

Mano from Netherlands  [1 posts] 7 years
I have tried that indeed.

I changed the pins, tried different servos, and tried every value_* on both servos. when i just switch servo one and two. the other servo will move and the first does nothing.

the robo and .spin file are attached
Anonymous 7 years
Hmm, I can see that you are still a bit confused as to what the Spin file is really doing. The rxVal_X values are temporary values and are NOT the received full numbers. ONLY rxVal is a correctly received number ... but that's just one of the numbers. You need 4 of those in order to read in everything correctly (and more as you transmit more). Otherwise you will not know what you are reading. Your heading is being calculated incorrectly from temporary numbers that are just part of a complete number. For small numbers rxVal_2 will ALWAYS be zero since that is just part of a 4 byte number. The correct way is to use SerCog 4 times in order to get all the correct values.  (SerCog is now renamed to ReadInt which makes more sense).

So your correct spin code is (note that this was NOT compiled so you may need to fix any syntax errors).


  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000

  rxpin = 31 ' Receive on I/O pin 1 <- black from master black txpin p1
  txpin = 30 ' Transmit on I/O pin 0 -> red to master red rxpin p0
  ServoCh1 = 0  'Steering servo                            
  ServoCh2 = 4


  LONG SerStack[32]
  LONG TransmitStack[32]
  LONG txVal_1, txVal_2, txVal_3, txVal_4 ' transmit
  LONG rxVal_1, rxVal_2, rxVal_3, rxVal_4' receive from master and PST
  LONG rxVal
  LONG rxHeight
  LONG rxWidth
  LONG rxMouseX
  LONG rxMouseY
  LONG Heading1
  LONG Heading2


  serial : "FullDuplexSerial"   ' "Extended_FDSerial" ''
  f : "Float32Full"
  SERVO : "Servo32v7.spin"

PUB Main | DutyCycle ' new stuff DutyCycle

  serial.start(rxpin,txpin,%0000, 57600)                  '- rxpin,txpin,mode,Baud
  SERVO.Start                                             'Start Servo handler

      rxHeight := ReadInt
      rxWidth := ReadInt
      rxMouseX := ReadInt
      rxMouseY := ReadInt
PUB ReadInt

    rxVal_1 := serial.rx
    rxVal_2 := serial.rx
    rxVal_3 := serial.rx
    rxVal_4 := serial.rx

    rxVal := rxVal_1 | (rxVal_2<<8) | (rxVal_3<<16) | (rxVal_4<<24)
    RETURN rxVal
PUB Transmitter


PUB WheelAngle

    heading1 := (rxMouseY * 2) + 1180      
    heading2 := (rxMouseX * 2) + 1260
Anonymous 7 years
Yes it works now. it needed some changes but i got it working. Both servo's are turning now.
Anonymous 7 years
It's working now!!

Anonymous 7 years
So after playinga biwith he code i started wondering on ow to add more dat to the string. I tried a few setups and changes, but with no possitive effect....

The best i can do is getting back scrambled data. some of the data is repeated in the next 4 values, allthough not everything.

I can post my code but it might be unnescesairy, since you wrote the code... above  
Mano from Netherlands  [27 posts] 6 years
I have Started a blog for my robot.
All the updates and code will be there.


This forum thread has been closed due to inactivity (more than 4 months) or number of replies (more than 50 messages). Please start a New Post and enter a new forum thread with the appropriate title.

 New Post   Forum Index