Shop OBEX P1 Docs P2 Docs Learn Events
Peter, Need Help with Translating code from Stamp to Java !!! — Parallax Forums

Peter, Need Help with Translating code from Stamp to Java !!!

JMLStamp2pJMLStamp2p Posts: 259
edited 2005-10-11 16:00 in General Discussion
Hello Peter, This upper portion of code works the way we want it to for the BS2.
Under the line is the equivlent code that we are trying to write for the Javalin chip.
Would you please make the nessessary changes with comments, it would be greatly appreciated. We are attempting to learn Java as fast as we can :>) ...
We are using a Parralax PLC on the transmitting end with an A\D converter installed. On the Receiving end we are using a PDB with the Javalin chip.



' {$STAMP BS2pe}
' {$PBASIC 2.5}

ClkAdc· PIN 0
CsAdc PIN 3
AoutAdc PIN 4
AinAdc· PIN 5
adResult VAR Word··········· 'Holds 16 Bit Value
Main:
LOW CsAdc·········································································· ·'Enable A\D for Communication with Stamp
SHIFTOUT AinAdc,ClkAdc, MSBFIRST, [noparse][[/noparse]Shift [noparse][[/noparse]%11111000]··············'Shift out Control Byte To A/D Converter·····
HIGH CsAdc············································································'End Control Sequence
LOW CsAdc·········································································· ·'Enable for Data
SHIFTIN AinAdc,ClkAdc, MSBPRE, [noparse][[/noparse]adResult\12]························· · 'Data is shifted in from Right to Left !
HIGH CsAdc··········································································· 'End Data Sequence
adResult = adResult>>8
DEBUG "Output was:·· ",adResult,CR
PAUSE 3000
GOTO Check_Voltage

Check_Voltage:
IF adResult >=·10 THEN MAIN
IF (adResult < 10) AND (adResult > 3) THEN Ground_Fault
IF (adResult < 3) THEN Voltage_Off

Ground_Fault:
SEROUT 16, 84, [noparse][[/noparse]"1"]
GOTO Main

Voltage_Off:
SEROUT 16, 84, [noparse][[/noparse]"2"]
GOTO Main



______________________________________________________________________________________________________________________
· import stamp.core.*;

· public class PLC{
··· static int adResult;
··· final static int ClkAdc = CPU.pin0;
··· final static int CsAdc = CPU.pin3;
··· final static int AoutAdc = CPU.pin4;
··· final static int AinAdc = CPU.pin5;
··· final static int dataPin = CPU.pin16;
··· static Uart txUart = new Uart ( Uart.dirTransmit, dataPin, Uart.dontInvert, Uart.speed2400, Uart.stop1 );
··· static StringBuffer buffer = new StringBuffer(7);
··· static void bufferMessage(){
··· adResult = 0xf0;
··· }
··· public static void serOut() {··························· ·· // Declares a new Method to handle serial out transmission.
······· if (adResult >=13);······································ // Not sure of how to send out values according to 'if' statement
······· else if (adResult <13 && adResult >3);··········· ·// i.e. if (adResult >=13) "send out value of 1"···· ?!?!?!?
······· else if (adResult <3);
··· }

····· public static void main() {

····· CPU.writePin(CPU.pin0, true);
····· CPU.shiftOut(CPU.pin3, CPU.pin0, 8, CPU.SHIFT_MSB, adResult <<8);
····· CPU.writePin(CPU.pin3, false);
····· CPU.writePin(CPU.pin3, true);
····· adResult = ((CPU.shiftIn(CPU.pin5, CPU.pin0, 8, CPU.PRE_CLOCK_MSB) >>8));
··· }
· }

Comments

  • bulkheadbulkhead Posts: 405
    edited 2005-10-04 05:00
    Just incase you didn't notice/know, if/else statements dont end in a semicolon -just letting you know, because I don't think I'm the only one who has spent an our searching for that extra semicolon at the end of a while loop... rolleyes.gif
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-10-04 07:32
    · import stamp.core.*;

    · public class PLC{
    ··· static int adResult;
    ··· final static int ClkAdc = CPU.pin0;
    ··· final static int CsAdc = CPU.pin3;
    ··· final static int AoutAdc = CPU.pin4;
    ··· final static int AinAdc = CPU.pin5;
    ··· final static int dataPin = CPU.pin16;
    ··· static Uart txUart = new Uart ( Uart.dirTransmit, dataPin, Uart.dontInvert, Uart.speed2400, Uart.stop1 );
    ··· static StringBuffer buffer = new StringBuffer(7);

    ··· static void bufferMessage(){
    ··· adResult = 0xf0;
    ··· }

    ··· public static void serOut() {··// Declares a new Method to handle serial out transmission.
    ········if (adResult <10 && adResult >=3) txUart.sendByte('1');
    ······· else if (adResult <3) txUart.sendByte('2');
    ··· }

    ··· public static void main() {
    ····· CPU.writePin(CPU.pin0, true);
    ····· CPU.shiftOut(CPU.pin3, CPU.pin0, 8, CPU.SHIFT_MSB, adResult <<8);
    ····· CPU.writePin(CPU.pin3, false);
    ····· CPU.writePin(CPU.pin3, true);
    ····· adResult = ((CPU.shiftIn(CPU.pin5, CPU.pin0, 8, CPU.PRE_CLOCK_MSB) >>8));
    ····· serOut();
    ····· while (true) ; //keep pin states and keep javelin running
    ··· }
    · }

    The above is according to the BS example. Note that I handle the case adResult==3
    which is not handled by the BS example.
    regards peter
  • JMLStamp2pJMLStamp2p Posts: 259
    edited 2005-10-05 01:03
    Peter, Thank you for your help as Allways !!!

    We re-wrote the code today as we are understanding a little more on how to structure the java code and it looks a little more like the code you posted... (allmost) :>)
    We added a couple more methode's and added a txUart constructor.

    P.S. Thank you for the semicolon note ... , we are Electricians learning Java as fast as we can :>)
    Thanks again !
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-10-05 05:14
    The post about the semicolon was not mine.

    Actually, the ; is a statement terminator, as is }.

    So

    if (a==1) ;

    else {b=1}

    is equivalent to
    if (a==1) {
    ·//do nothing
    }
    else {
    · b=1; //note: the semicolon is not needed here because of the closing }
    }

    and although technically right, it is a bad program style.
    The above is better written as:
    if (a!=1) b=1;

    regards peter
    ·
  • JMLStamp2pJMLStamp2p Posts: 259
    edited 2005-10-05 16:26
    Thank you Peter, we appreciate your help as allways !

    John Logan

    Action Electric Co.
  • JMLStamp2pJMLStamp2p Posts: 259
    edited 2005-10-05 16:41
    Peter, We have installed the Javlin in our PLC and run the program. We are transmitting and receiving fine but on our receiving end we are geting ASCII char.'s on the screen. I am going to paste the transmitting code and receiving code below so you can look a it ... I am a little confused about how to print int.'s to the screen on the receiving end. When we have the line (SpeechCode = 0xff) in the receiver code is the varible SpeechCode a pointer to the buffer ? Would you please explain the process of
    receiving data in the Javalin, as far as the buffer is concerned and how to print numbers to the screen. We are on a jobsite using the PLC and PDB along with a couple of high power tranceivers. Your input is invaliable and greatly appreciated.
    John Logan
    Action Electric Co.

    //Code for Transmitting


    import stamp.core.*;

    public class PLC{
    static int adResult;
    final static int ClkAdc = CPU.pin0;
    final static int CsAdc = CPU.pin3;
    final static int AoutAdc = CPU.pin4;
    final static int AinAdc = CPU.pin5;
    final static int dataPin = CPU.pin15;
    static Uart txUart = new Uart ( Uart.dirTransmit, dataPin, Uart.dontInvert, Uart.speed2400, Uart.stop1 );
    static StringBuffer buffer = new StringBuffer(7);

    static void bufferMessage(){
    adResult = 0xf0;
    }

    public static void serOut() { // Declares a new Method to handle serial out transmission.
    if (adResult <10 && adResult >=3) txUart.sendByte('1');
    else if (adResult <3) txUart.sendByte('2');
    }

    public static void main() {
    CPU.writePin(CPU.pin3, true);
    CPU.shiftOut(CPU.pin3, CPU.pin0, 8, CPU.SHIFT_MSB, adResult <<8);
    CPU.writePin(CPU.pin3, false);
    CPU.writePin(CPU.pin3, true);
    adResult = ((CPU.shiftIn(CPU.pin5, CPU.pin0, 8, CPU.PRE_CLOCK_MSB) >>8));
    serOut();
    while (true) ; //keep pin states and keep javelin running
    }
    }
    _______________________________________________________________________________
    //Code for the receiving end



    import stamp.core.*;
    public class Main_Receiver{

    // final static int dirReceive = 0;
    // final static int speed = 9600;
    // final static boolean dontInvert = true;
    // final static int stop1 = 0;

    final static int dataPin = CPU.pin4;
    static Uart rxUart = new Uart ( Uart.dirReceive, dataPin, Uart.dontInvert, Uart.speed2400, Uart.stop1 );
    static StringBuffer buffer = new StringBuffer(7);
    static int SpeechCode;

    static void bufferMessage(){
    SpeechCode = 0xff;

    //treat \r as newline

    while (SpeechCode != '\n'){
    if (rxUart.byteAvailable()){
    SpeechCode = (int)rxUart.receiveByte();
    if (SpeechCode == '\r') SpeechCode = '\n'; //easier for printing
    buffer.append(SpeechCode );
    System.out.println((int)SpeechCode ); //display incoming message in character format, provides an unlimited value
    }
    }
    }

    public static void main(){
    while (true) { //receive and display ALL messages
    buffer.clear(); //clear buffer
    bufferMessage(); //receive and display one incoming message
    }
    }
    }
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-10-05 17:20
    The javelin receives uart data in the background. When data is available,

    rxUart.byteAvailable() returns true. You add these bytes to your buffer

    until you receive a carriage return character.

    To display the entire received buffer use

    System.out.print(buffer);

    (not System.out.print(SpeechCode)[noparse];)[/noparse]

    All bytes in the buffer are then treated as ascii codes.

    To display an integer value i, you use

    System.out.print(i);

    Now if your buffer contents is made of different types, eg. text and binary numbers,

    then you must use more print statements, a collection of the two variations above,

    in effect, splitting the buffer into smaller fragments, each fragment being text or binary number.

    On the transmit javelin, only one '1' or '2' character is transmitted, then that javelin is waiting

    forever, doing nothing.

    regards peter
  • JMLStamp2pJMLStamp2p Posts: 259
    edited 2005-10-05 19:21
    Peter, when we try and use the statement System.out.print(buffer);
    We get an error message " No match found for method "print(java.lang.StringBuffer);
    Does the following constructor not create an instance for this? static StringBuffer buffer = new StringBuffer(7);
    Sorry, Im confused :>0
  • JMLStamp2pJMLStamp2p Posts: 259
    edited 2005-10-05 20:30
    Peter, I received an e-mail but one of my electricians deleted it before I could read it. Did you reply to my last post on the No match found for method already ?
    JMLStamp2p
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-10-05 21:03
    It seem print() does not support StringBuffer.

    Well, better to use a simple char array anyway.

    //Code for the receiving end



    import stamp.core.*;
    public class Main_Receiver{

    // final static int dirReceive = 0;
    // final static int speed = 9600;
    // final static boolean dontInvert = true;
    // final static int stop1 = 0;

    final static int dataPin = CPU.pin4;
    static Uart rxUart = new Uart ( Uart.dirReceive, dataPin, Uart.dontInvert, Uart.speed2400, Uart.stop1 );
    static char[noparse]/noparse buffer = new char[noparse][[/noparse]7];
    static int SpeechCode;

    static int bufindex=0;

    static void bufferMessage(){
    SpeechCode = 0xff;

    //treat \r as newline

    while (SpeechCode != '\n'){
    if (rxUart.byteAvailable()){
    SpeechCode = rxUart.receiveByte();
    if (SpeechCode == '\r') SpeechCode = '\n'; //easier for printing
    buffer[noparse][[/noparse]bufindex++] = (char)SpeechCode;
    }
    }
    buffer[noparse][[/noparse]bufindex]=0;

    bufindex=0;

    while (buffer[noparse][[/noparse]bufindex]!=0) System.out.print(buffer[noparse][[/noparse]bufindex++]); //display incoming message
    }

    public static void main(){
    while (true) { //receive and display ALL messages
    bufindex=0; //clear buffer
    bufferMessage(); //receive and display one incoming message
    }
    }
    }

    regards peter
    ·
  • JMLStamp2pJMLStamp2p Posts: 259
    edited 2005-10-06 15:15
    Peter, we are trying to reduce our code to its simplest form in order to understand instead of just asking questions. Our objective is to send one string out, the word "hello", receive it and print it·to the screen on the receiving end. When we used the code you made available to us last night we could not get anything to print to the screen (using the Transmit code that we have posted below).We also tried using txUart.sendByte·instead of txUart.sendString and all we would receive no·matter what numerical value we put in was·-1, -1, -4 .·Could you Please help us with the code?

    //Transmit Code

    import stamp.core.*;
    public class PLC_Test{

    final static int dataPin = CPU.pin15
    static Uart txUart = new Uart( Uart.dirTransmit, dataPin, Uart.dontInvert, Uart.speed2400, Uart.stop1 );
    static stringBuffer buffer = new stringBuffer();


    public static void serialOut(){
    txUart.sendString("hello");
    }

    public static void main(){
    serialOut();
    }
    }

    ____________________________________________________________________________________________________________________________________

    //Receiver Code

    import stamp.core.*;
    public class Main_ReceiverB{

    final static int dataPin = CPU.pin4;
    static Uart rxUart = new Uart( Uart.dirReceive, dataPin, Uart.dontInvert, Uart.speed2400, Uart.stop1);
    static StringBuffer buffer = new StringBuffer(7);
    static int SpeechCode;

    static void bufferMessage(){
    SpeechCode = 0xff;

    while(SpeechCode != '\n'){
    if( rxUart.byteAvailable()){
    SpeechCode = (int) rxUart.receiveByte();
    if( SpeechCode == '\r') SpeechCode = '\n';
    buffer.append(SpeechCode);
    System.out.println((int) SpeechCode);
    }
    }
    }

    public static void main(){
    while(true){
    buffer.clear();
    bufferMessage();
    }
    }
    }

    _________________________________________________________________________________________________________________________________________


    ____________________________________________________________________________________________________________________________________

    Post Edited (JMLStamp2p) : 10/6/2005 5:35:38 PM GMT
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-10-06 18:16
    Here is a little test program that transmits and receives using 1 javelin.

    It demonstrates the Uart·transmit and receive.

    Edit:

    Changed error in displaying message.

    changed to

    ····· while (bufrecv[noparse][[/noparse]index]!=0) System.out.print(bufrecv[noparse][[/noparse]index++]); //display received message

    regards peter




    Post Edited (Peter Verkaik) : 10/6/2005 6:58:54 PM GMT
  • JMLStamp2pJMLStamp2p Posts: 259
    edited 2005-10-06 18:21
    Thanks Peter ....
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-10-06 18:59
    see my previous post.
    I updated the test program.
    regards peter
    ·
  • JMLStamp2pJMLStamp2p Posts: 259
    edited 2005-10-10 22:08
    Peter, we finally changed out the Javalin in out Transmitting PLC to a BS2PE and everything is working great with a Javalin on our receiving end.
    We have a E-Mic speech chip also on the receiving end but had it working under the Stamp code ... We have made a stab at changing just the nessessary code
    to say (1) sentence over to the Javalin code but need some help from you !!! Below we have included the Receiver code witch is presently working. Under that we have posted our try at the Javalin code to make the chip talk. Im sure we have error's but tried our best :>)
    Thank you for your help, as allways ...
    JMLStamp2p



    import stamp.core.*;
    public class Main_Receiver{

    final static int dataPin = CPU.pin4;
    static Uart rxUart = new Uart ( Uart.dirReceive, dataPin, Uart.dontInvert, Uart.speed2400, Uart.stop1 );
    static StringBuffer buffer = new StringBuffer(128);
    static char SpeechCode;

    static void bufferMessage(){
    SpeechCode = 0xff;
    //treat \r as newline
    while (SpeechCode != '\n'){
    if (rxUart.byteAvailable()){
    SpeechCode = (char)rxUart.receiveByte();
    if (SpeechCode == '\r') SpeechCode = '\n'; //easier for printing
    buffer.append(SpeechCode);

    System.out.println((char)SpeechCode); //display incoming message
    }
    }
    }

    public static void main(){
    while (true) { //receive and display ALL messages
    buffer.clear(); //clear buffer
    bufferMessage(); //receive and display one incoming message
    }
    }
    }

    //__________________________________________________________________________________________________________

    //Peter, we would like to extend the main method to include the folloing called mehtods.

    soft_reset();
    Init();
    Say();
    //We will include the following varibles nd constants.
    final Tx = CPU.pin0;
    final Rx = CPU.pin1;
    final Busy = CPU.pin2;
    final Rst = CPU.pin3;
    final Aout = CPU.pin7;
    final static int baud = 396;
    final static int TmAdj = $100; //need equvalent in Javelin code
    final static int FrAdj = $100;
    final static int char;
    final static int eePntr;
    final static int EOM = 0xAA;
    final static int Say = 0x00;
    final static int Volume = 0x01;
    final static int Speed = 0x02;
    final static int Pitch = 0x03;
    final static int Reset = 0x08;

    EEPROM.write(0,(byte)(n&... //need help putting text string into EEPROM for E-Mic verbage

    SoftReset(){
    CPU.writePin(CPU.pin3, TRUE);
    CPU.delay(10);
    CPU.writePin(CPU.pin3, FALSE);
    }

    Init(){
    CPU.shiftOut(CPU.Tx, baud, 8, CPU.PRE_CLOCK_MSB, 0x01); //Volume
    Check_Busy();
    CPU.shiftOut(CPU.Tx, baud, 8, CPU.PRE_CLOCK_MSB, 0x02); //Speed
    Check_Busy();
    CPU.shiftOut(CPU.Tx, baud, 8, CPU.PRE_CLOCK_MSB, 0x03); //Pitch
    Check_Busy();
    }

    Say(){
    CPU.shiftIn(CPU.Rx, baud, 8, CPU.PRE_CLOCK_MSB, eePntr);
    CPU.shiftOut(CPU.Tx, baud, 8, CPU.PRE_CLOCK_MSB, char);
    eePntr = eePntr + 1 //need javelin equivalent
    Loop until(char = EON); //need javelin equivalent
    }

    Check_Busy(){
    CPU.delay(1000);
    While(CPU.readPin(CPU.pin2));
    }
  • JMLStamp2pJMLStamp2p Posts: 259
    edited 2005-10-11 16:00
    // Our objective is to send text strings and command codes from the Javelin to the E-Mic Speech Chip.
    // Our code is downloading with no syntax errors and the E-Mic chip is being reset correctly due to-
    // the soft_Reset method that we are calling.
    // Note: our main method also calls methods to receive serial data which is also working correctly.
    // We understand that the while statement in the main method will change at a later date.
    //We need to fully understand how to load text strings or equivalent values into EEPROM, then send them out to the-
    // E-Mic chip along with command codes.

    import stamp.core.*;
    public class Main_Receiver{

    final static int dataPin = CPU.pin4;
    static Uart rxUart = new Uart ( Uart.dirReceive, dataPin, Uart.dontInvert, Uart.speed2400, Uart.stop1 );
    static StringBuffer buffer = new StringBuffer(128);
    static char SpeechCode;
    //__________________________________________________________________

    final static int Tx = CPU.pin0;
    final static int Rx = CPU.pin1;
    final static int Busy = CPU.pin2;
    final static int Rst = CPU.pin3;
    final static int Aout = CPU.pin7;
    final static int baud = 396;
    final static int TmAdj = 100; //need equvalent in Javelin code
    final static int FrAdj = 100;
    // final static int char;
    final static int eePntr = 0x00;
    final static int EOM = 0xAA;
    // final static int Say = 0x00;
    final static int Volume = 0x01;
    final static int Speed = 0x02;
    final static int Pitch = 0x03;
    final static int Reset = 0x08;
    final static boolean buttonOn = false;
    final static boolean buttonOff = true;
    //_________________________________________________________________________

    static void bufferMessage(){
    SpeechCode = 0xff;
    //treat \r as newline
    while (SpeechCode != '\n'){
    if (rxUart.byteAvailable()){
    SpeechCode = (char)rxUart.receiveByte();
    if (SpeechCode == '\r') SpeechCode = '\n'; //easier for printing
    buffer.append(SpeechCode);

    System.out.println((char)SpeechCode); //display incoming message
    }
    }
    }

    static void soft_Reset(){
    CPU.writePin(CPU.pin3, false);
    CPU.delay(10);
    CPU.writePin(CPU.pin3, true);
    }

    static void Init(){
    CPU.shiftOut(CPU.pin0, baud, 8, CPU.PRE_CLOCK_MSB, 0x01); //Command to set volume
    CPU.shiftOut(CPU.pin0, baud, 8, CPU.PRE_CLOCK_MSB, 0x06); //Volume set at 06
    Check_Busy();
    CPU.shiftOut(CPU.pin0, baud, 8, CPU.PRE_CLOCK_MSB, 0x02); //Command to set speed
    CPU.shiftOut(CPU.pin0, baud, 8, CPU.PRE_CLOCK_MSB, 0x02); //Speed set at 02
    Check_Busy();
    CPU.shiftOut(CPU.pin0, baud, 8, CPU.PRE_CLOCK_MSB, 0x03); //Command to set pitch
    CPU.shiftOut(CPU.pin0, baud, 8, CPU.PRE_CLOCK_MSB, 0x03); //Pitch set at 03
    Check_Busy();
    }

    static void setEEProm(int n){ //have to chop n into bytes
    EEPROM.write(0,(byte)(n&0xFE));
    EEPROM.write(1,(byte)(n>>8));
    }

    static int getEEPROM(){
    int x;
    x=EEPROM.read(1);
    x=(x<<8)+EEPROM.read(0);
    return x;
    }



    static void Check_Busy(){
    CPU.delay(1000);
    // While(CPU.readPin(CPU.pin2));
    }


    public static void main(){
    soft_Reset();
    Init();
    Check_Busy();
    setEEProm(0x42); //this just loads the EEPROM with the decimal equivalent 65, which we want to be the ASCII "A"
    System.out.println("Bytes available in EEPROM:");
    System.out.println(EEPROM.size());
    System.out.println("The value you wrote to EEPROM:");
    System.out.println(getEEPROM());
    CPU.delay(1000);
    CPU.shiftOut(CPU.pin0, baud, 8, CPU.PRE_CLOCK_MSB, 0x00); //0x00 is a command for the E-Mic chip to Say( English text)
    CPU.shiftOut(CPU.pin0, baud, 8, CPU.PRE_CLOCK_MSB, 0x42); //Here we are trying to have the speech chip "say" the letter "A"
    while (true) { //receive and display ALL messages
    buffer.clear(); //clear buffer
    bufferMessage(); //receive and display one incoming message
    }
    }
    }
Sign In or Register to comment.