Register

Prizm 3 wire serial communications

Discuss issues related to the Casio Prizm 3rd party development
Senior Member
Posts: 69
Joined: Sat Feb 02, 2013 4:29 am
Calculators: Casio Cfx Series, Casio fx-CG10

Re: Prizm 3 wire serial communications

Postby nsg » Tue Mar 19, 2013 2:43 am

I managed to successfully receive data from BS2.
Image
In this picture you can see the BS2 (BASIC Stamp 2) and CFX connected together. Pins from serial cable go directly into inputs of the BS2 -- there is no extra hardware needed for communication.
CFX is running a program
Code: Select all
Lbl 1
Receive(F)
FDisp
Goto 1


BS2 part is a little bit more involved. It processes the request by getting the value of photoresistor and sending it back.
Values on the screen are different because I covered and uncovered photoresistor while taking readings.

Apparently, the CFX after sending handshake, receiving responce and sending ":REQ" packet waits for response idefinitely, so BS2 has all the time it needs to process the reading from the sensor.

Senior Member
User avatar
Posts: 605
Joined: Sat Sep 15, 2012 6:59 am
Location: Krautland ****
Calculators: Casio fx-7400GII, Casio fx-7400GII (SH4), Casio fx-9750GII, Casio fx-9750GII (SH4), Casio fx-9860G, Casio fx-9860G SD, Casio fx-9860G Slim, Casio fx-9860GII SD, Casio fx-9860GII SD Power Graphic 2, Casio Classpad 330 plus, Casio fx-CG20, Casio fx-CG50, Casio Classpad fx-CP400

Re: Prizm 3 wire serial communications

Postby SimonLothar » Tue Mar 19, 2013 6:53 am

nsg wrote:I managed to successfully receive data from BS2.
Well done!
I'll be back!

Senior Member
Posts: 69
Joined: Sat Feb 02, 2013 4:29 am
Calculators: Casio Cfx Series, Casio fx-CG10

Re: Prizm 3 wire serial communications

Postby nsg » Fri Mar 22, 2013 12:27 am

Here is example BASIC Stamp program that communicates with Casio calculator.
It understands Send() and Receive() requests of alpha variables (no lists or matrices).
Calculator should be connected to pins 2 and 4 of Basic stamp (base goes to ground as usual)
Here TIP means the contact that connects to a TIP part of a calculator female connector and not the tip of the free end of crossover Casio communication cable.

The idea is that the BASIC Stamp does minimum amount of processing, it justs sends raw word-sized data back to calculator as-is. The calculator does complicated scaling and conversion, as well as data storage and presentation, it is better equipped for that.


Code: Select all
' {$STAMP BS2}
' {$PBASIC 2.5}

' Reference implementation of Casio calculators communication protocol.
' This implementation allows communicate values in the range 0..65535

' Examples:
' Calculator 3 wire port should be connected:
'   TIP to the pin 2
'   RING to pin 4
'   BASE to VSS
' Receive(L)
'   If var is L, then returns the dischanrge time of a capacitor at PIN 9 (lightsns)
'   (this is standard way to measure resistance in BASIC Stamp)
'   All other variable requests return 12345
' Send(D)
'   Waits for D seconds
'   all other sent variables are ignored

' All protocol related variables and constants start with Calc...
CalcTX PIN 2 ' BS2 reads bytes from this pin, connected to a "tip"
CalcRX PIN 4 ' BS2 writes to this pin, connect to "ring"
' "base" should be connected to ground

T9600 CON 84  ' Serial port communication mode

' Initial value of description packet checksum
CalcDescrCalcChkSum CON -27*$ff-"V"-"A"-"L"-"V"-"M"-1-1-"V"-"a"-"r"-"i"-"a"-"b"-"l"-"e"-"R"-$0a

' ----- Value packet constants/variables
' infobyte flags
Calc_IsComplex CON %10000000 ' if complex
Calc_RpIsNeg   CON %01010000 ' sign of a real part is negative
Calc_ExpIsPos  CON %00000001 ' if exponent is positive
' initial value checksum, needs to be adjusted based on actual value
CalcValueCalcChkSum CON -1-1-Calc_ExpIsPos
CalcExp VAR Byte ' exponent

CalcValue VAR Word ' the word to send back to calc. Sensor value 0..65535 goes into this variable
CalcBCD VAR Byte(3) ' BCD representation of CalcValue
CalcVarType VAR CalcBCD ' request type, VM, LT, etc. Only VM is supported
' ----- Calculator Variable names
CalcVarName VAR Byte ' name of a requested variable
CalcNameLowR CON $cd  ' variable name lowcase "r"
CalcNameTheta CON $ce ' varaiable name greek theta
CalcByte VAR Byte ' hold space for read and write
CalcIndex VAR Nib ' index used during BCD parsing/formatting

CalcChkSum VAR Byte


trigpin PIN 10 ' pin for oscilloscope external trigger

' example sensor in formation
lightsns PIN 9


OUTPUT trigpin
LOW trigpin

'request format:
' ":REQ",0
' "VM" 0or PC or LT or MT)
' $ff,$ff,$ff,$ff
' CalcVarName (r=$cd, theta=$ce)
' 13-49 all $ff
' 50 checksum

DO

' SERIN Rpin {\Fpin}, Baudmode, {Plabel,} {Timeout, Tlabel,} [InputData]
restartcomm:
LOW trigpin
  ' --Receive    --Send
  ' Casio BS2    Casio BS2
  ' $15   $13    $15   $13
  ' :REQ  $06    :VAL  $06
  ' $06   :VAL   :0101 $06
  ' $06   :0101  :END
  ' $06   :END
SERIN CalcTX,T9600,[CalcByte]
IF CalcByte<>$15 THEN
  DEBUG "?",HEX CalcByte,CR
  GOTO restartcomm
ENDIF
  SEROUT CalcRx,T9600,[$13]

  SERIN CalcTx,T9600,[WAIT(":"),CalcByte,SKIP 2,SKIP 1,STR CalcVarType\2,SKIP 4,CalcVarName,SKIP 37,CalcChkSum]
  ' before $06 is sent it is ok to perform lengthy calculations and sensor processing

  IF "V"<>CalcVarType(0) OR "M"<>CalcVarType(1) THEN
    DEBUG "Uninplemeted type:",STR CalcVarType\2,CR
    GOTO restartcomm
  ENDIF
  ' CalcByte is "R"[EQ] when calc executes Receive command, "V"[AL] when Send( command
  LOOKDOWN CalcByte,["RV"],CalcByte
  BRANCH CalcByte,[PROCESS_RECEIVE,PROCESS_SEND]
  DEBUG "Unknown packet type",CR
  GOTO restartcomm

PROCESS_RECEIVE:
  SELECT CalcVarName
    CASE "L"
      ' --------------------------------------
      ' place sensor code here
      ' --------------------------------------
      HIGH lightsns
      PAUSE 100
      RCTIME lightsns,1,CalcValue
      ' --------------------------------------
    CASE ELSE
      CalcValue=12345
  ENDSELECT

  DEBUG CalcVarName,"(",STR CalcVarType\2,"):=",DEC CalcValue, ",", IHEX4 CalcValue,CR
  SEROUT CalcRx,T9600,[$06]
  SERIN CalcTx,T9600,[CalcByte]
  IF CalcByte<>$06 THEN
    DEBUG "Unexepected (at 1) ",HEX CalcByte, "(instead of $06)",CR
    GOTO restartcomm
  ELSE
    DEBUG "1 ok",CR
  ENDIF
  ' Generate and send variable description packet
  ' The only part which can be varied in this packet is CalcVarName,
  ' otherwise need to change expression for checksum calculation.
  CalcChkSum=CalcDescrCalcChkSum-CalcVarName
  SEROUT CalcRX,T9600,[":VAL",0,"VM",0,1,0,1,CalcVarName,REP $ff\7,
    "Variable","R",$0a,REP $ff\20,CalcChkSum]
  SERIN CalcTx,T9600,[CalcByte]
  ' CFX actually sends $15 if there is something wrong
  IF CalcByte<>$06 THEN
    DEBUG "Unexepected (at 2) ",HEX CalcByte, "(instead of $06)",CR
    GOTO restartcomm
  ELSE
    DEBUG "2 ok",CR
  ENDIF

  'Generate and send value packet
  CalcExp=4
  FOR CalcIndex=0 TO 5
    CalcBCD.LOWNIB(5-CalcIndex^1)=CalcValue DIG CalcIndex
  NEXT
'  Alternative way to format CalcValue:
'CalcBCD.LOWNIB(4)=CalcValue DIG 0
'CalcBCD.LOWNIB(5)=CalcValue DIG 1
'CalcBCD.LOWNIB(2)=CalcValue DIG 2
'CalcBCD.LOWNIB(3)=CalcValue DIG 3
'CalcBCD(0)=CalcValue DIG 4


  CalcChkSum=CalcValueCalcChkSum-CalcBCD(0)-CalcBCD(1)-CalcBCD(2)-CalcExp
  DEBUG "CalcBCD=",HEX2 CalcBCD(0),HEX2 CalcBCD(1),HEX2 CalcBCD(2)," CalcChkSum=",HEX2 CalcChkSum,CR
  SEROUT CalcRX,T9600,[":",0,1,0,1,STR CalcBCD\3,REP 0\5,Calc_ExpIsPos,4,CalcChkSum]
  ' HIGH trigpin:PAUSE 1:LOW trigpin
  'SEROUT CalcRX,T9600,[":",0,1,0,1,
  '  $01, ' int part, 1 nib
  '  $23,$00,$00,$00,$00,$00,$00, ' frac part, CalcBCD
  '  %00000001, ' infobyte
  '  $02, 'exp, CalcBCD
  '  $d7

  SERIN CalcTx,T9600,[CalcByte]
  IF CalcByte<>$06 THEN
    DEBUG "Unexepected (at 3) ",HEX CalcByte, "(instead of $06)",CR
    GOTO restartcomm
  ELSE
    DEBUG "3 ok",CR
  ENDIF

  ' Send :END packet
  SEROUT CalcRX,T9600,[":END",REP $ff\45, $56] ' end packet
  GOTO restartcomm

PROCESS_SEND:
  DEBUG "received SEND(",CalcVarName,") request",CR
  SEROUT CalcRX,T9600,[$06]
  '                             0101   int\1    frac\7   info   exp
  SERIN CalcTX,T9600,[WAIT(":"),SKIP 4,STR CalcBCD\3,SKIP 5,SKIP 1,CalcExp,CalcChkSum]
  ' ---- process received value of CalcVarName here
  DEBUG ? CalcExp
  CalcValue=0
  FOR CalcIndex=0 TO 5
    CalcValue=CalcValue*10+CalcBCD.LOWNIB(CalcIndex^1)
  NEXT
  FOR CalcIndex=CalcExp TO 3
    CalcValue=CalcValue/10
  NEXT
  DEBUG CalcVarName,"=",DEC CalcValue,CR
  SELECT CalcVarName
    CASE "D"
      ' n seconds delay
      DEBUG "Wait ",DEC CalcValue," seconds",CR
      PAUSE CalcValue*1000
  ENDSELECT
  ' ...
  ' ----
  SEROUT CalcRX,T9600,[$06]
  SERIN CalcTX,T9600,[WAIT(":END"),SKIP 45,CalcChkSum]
  GOTO restartcomm

LOOP


And here is example calculator code that goes with above program:
Code: Select all
Lbl 1
Receive(L)
Locate 1,1,"     "
Locate 1,1,L
2->D
Send(D)
Goto 1

It reads value of L (variable resistor connected to pin 9 through RC circuit), displays it, waits for 2 seconds and repeats.

(I have not tested it with prizm yet, but it works with CFX-9850G)

User avatar
Posts: 89
Joined: Thu Apr 05, 2012 3:16 pm
Location: Akron, OH, USA
Calculators: Casio fx-9750GII, Casio fx-CG10

Re: Prizm 3 wire serial communications

Postby flyingfisch » Mon May 20, 2013 2:00 am

This seems very interesting... is it possible to connect two calcs together and communicate with BASIC? I was told it wasn't.

Senior Member
Posts: 69
Joined: Sat Feb 02, 2013 4:29 am
Calculators: Casio Cfx Series, Casio fx-CG10

Re: Prizm 3 wire serial communications

Postby nsg » Mon May 20, 2013 5:55 am

Not in any meaningful way. Receiver has to go first, but sender must respond within really small window, so you have to time them just right for one communication and i goubt you can keep them synchronized like that for long enough to be practical. Receive38K/Send38K, though, may have longer waiting window. But I do not have 2 calcs that both support 38k mode to try.

User avatar
Posts: 89
Joined: Thu Apr 05, 2012 3:16 pm
Location: Akron, OH, USA
Calculators: Casio fx-9750GII, Casio fx-CG10

Re: Prizm 3 wire serial communications

Postby flyingfisch » Mon May 20, 2013 5:42 pm

Do you know what the 38k mode programs should look like? Like, what protocol does it have? I have access to 3 PRIZM's and one fx9860GII. ;)

Senior Member
Posts: 69
Joined: Sat Feb 02, 2013 4:29 am
Calculators: Casio Cfx Series, Casio fx-CG10

Re: Prizm 3 wire serial communications

Postby nsg » Mon May 20, 2013 7:11 pm

Try
Code: Select all
123->X
Send38K(X)


on one and
Code: Select all
Receive38K(X)
X


on the other. Try to time them so first starts a bit (like 1/4 of a second) earlier, then the other.
Protocol, as I read it, does not allow them to communicate that way because both requests are "master" or "client" type, they expect other party to be listening at all time and responding appropriately. However, 38K versions have some weird delays that may be indicative of some kind of special case that may allow one of them to act as a server.

User avatar
Posts: 89
Joined: Thu Apr 05, 2012 3:16 pm
Location: Akron, OH, USA
Calculators: Casio fx-9750GII, Casio fx-CG10

Re: Prizm 3 wire serial communications

Postby flyingfisch » Tue May 21, 2013 2:23 am

Well, that didn't work, so i tried

Code: Select all
123->X
Send38k X


and

Code: Select all
0->X

While X=0
Recieve38k X
WhileEnd

X


Still nothing. I run the receive code, and it runs fine, even without being connected. It just keeps trying to receive. However, when I run the Send38k command on the other calc, nothing happens. Is there some sort of protocol I don't know about?

Senior Member
Posts: 69
Joined: Sat Feb 02, 2013 4:29 am
Calculators: Casio Cfx Series, Casio fx-CG10

Re: Prizm 3 wire serial communications

Postby nsg » Tue May 21, 2013 2:06 pm

Send and Receive implement communication protocol, described, for example, in
http://www.nexusresearchgroup.com/downloads/Casio-Picaxe-manual.pdf

The way i understand it, Send and Receive are incompatible with each other, so you cannot use them to send data between calculators. It is to send data between calc and other device. Calc acts as a client, other device as a server in both Send and Receive scenarios. (BTW, the other device may be another calc running add-on that implements server side of Send/Receive protocol.)
However the different timing of 38k versions (Receive38k waits longer and does not raise error afair) made me suspicious that there may be more behind the scenes. Apparently, there isn't. Oh, well, it was worth trying. Nothing interesting here. Moving on.

User avatar
Posts: 89
Joined: Thu Apr 05, 2012 3:16 pm
Location: Akron, OH, USA
Calculators: Casio fx-9750GII, Casio fx-CG10

Re: Prizm 3 wire serial communications

Postby flyingfisch » Tue May 21, 2013 10:12 pm

oh, i see. ok.

has anyone figured out how to hook into casio basic to add functions? Could we add a usable send/receive function?

PreviousNext

Return to Casio Prizm SDK

Who is online

Users browsing this forum: No registered users and 15 guests