Serial Communication Difficulties
Tristan T
Posts: 31
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:
and I have my serial port setup in java for 19200, 8 data bits, 1 stop bit, no parity
Whenever I send a string to the stamp, I get an echo of my input followed by some bits that I don't understand:
If I insert a DEBUG statement in the code instead of a SEROUT like
I get:
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
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
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
Explaining the echo response helped me too... I was confused how some bits were slipping through correctly and others not.
-Tristan