Shop OBEX P1 Docs P2 Docs Learn Events
Serial Communication Difficulties — Parallax Forums

Serial Communication Difficulties

Tristan TTristan T Posts: 31
edited 2006-06-08 21:05 in BASIC Stamp
I am trying to establish a serial connection to my BS2px24 through a java application, and I am having well.... very inconsistant results.

Here is the test code on the BS:
' {$STAMP BS2px}
' {$PBASIC 2.5}
control VAR Byte
comma VAR Byte
dx VAR Byte
dy VAR Byte

SERIN 16, 14, [noparse][[/noparse]control]
SEROUT 16,14, [noparse][[/noparse]"THIS IS A TEST"]




and I have my serial port setup in java for 19200, 8 data bits, 1 stop bit, no parity

System.out.print("Send string to pic:  ");
        String input = in.readLine();
        
                SimpleSerial ss = new SimpleSerialNative(1,19200,8,0,0);
        
        ss.writeString(input);                // write a byte to the serial port
            
        //try { Thread.sleep(10); } catch (InterruptedException e) {}
        
           byte[noparse][[/noparse]] inputbytes = ss.readBytes(); // read any string from the serial port
            size = inputbytes.length;
            //System.out.println(size);
            for(i = 0; i < size ; i++){
                System.out.print((char)inputbytes[noparse][[/noparse] i ] + " ");
            }
            try { Thread.sleep(10); } catch (InterruptedException e) {}
            inputbytes = ss.readBytes();
            size = inputbytes.length;
            for(i = 0; i < size ; i++){
                System.out.print((char)inputbytes[noparse][[/noparse] i ] + " ");
            }
        
        ss.close();
        System.out.println(" ");
        System.out.println("Serial port closed, program ended");
    }




Whenever I send a string to the stamp, I get an echo of my input followed by some bits that I don't understand:

Send string to pic:  aa
Initing NATIVE port.  Com = \\.\com1, baud = 19200
A a ( ?  
Serial port closed, program ended





If I insert a DEBUG statement in the code instead of a SEROUT like
' {$STAMP BS2px}
' {$PBASIC 2.5}
control VAR Byte
comma VAR Byte
dx VAR Byte
dy VAR Byte
DO
SERIN 16, 14, [noparse][[/noparse]control]
DEBUG "WORK DAMNIT!"
LOOP




I get:
Send string to pic:  A
Initing NATIVE port.  Com = \\.\com1, baud = 19200
A ? : ? ? D A M N I T !  
Serial port closed, program ended




Can anyone shed some light on the issues i'm having here? It's some kind of timing issue... maybe I am just not using a good implementation of a serial read? Anybody with some experience in this?

Thanks,
Tristan

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2006-06-08 18:59
    Usually, this happens because you have the wrong baud-rate-selection value in the BS2. You definitely need to add the "Inverted" switch to the baud-rate-selection value, as the "port 16" uses 'Inverted' mode.

    Now, the 'echo' occurs because the design of the RS-232 circuitry on Port 16 of the BS2 works by using the TX line's voltage when it sends out the RX line. As a result, everything that comes in the TX line automatically gets echo'ed out the RX line. Any program that interfaces with the BS2 must therefore remove the echo before it can actually get the BS2's response.

    And naturally, the 'echo' is always at the correct baud rate, while the BS2's response may or may not be.

    Later:· Ok, having reviewed the Parallax docs, you need
    ·
    Main:
    · SERIN 16, 110 + 16384, [noparse][[/noparse]Control]
    · SEROUT 16, 110 + 16384, [noparse][[/noparse]"Hi there!", 13]
    · GOTO MAIN

    ' The above should spend most of its time in the SERIN, waiting for the byte

    Even Later:· And now I see you're using a "BS2px", and so I'm not sure WHAT the 'baud-mode' value should be.· The above should work for a BS2sx, though.

    Post Edited (allanlane5) : 6/8/2006 7:17:53 PM GMT
  • Tristan TTristan T Posts: 31
    edited 2006-06-08 20:50
    Thanks for the help. You were right, my baud rate was incorrect, but I did not need to add the inversion, just switching my baud constant to 188 fixed it. I don't know how I missed that. I just assumed that it was my java that was rusty.

    Explaining the echo response helped me too... I was confused how some bits were slipping through correctly and others not.

    -Tristan
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-06-08 21:05
    Hey, whatever works. I've heard that the Port 16 does it 'correctly', and either automatically puts in the Inversion or ignores your setting, whatever is 'correct'. Glad you got it working.
Sign In or Register to comment.