Uart questions
eshieh
Posts: 5
Hi, I was having some problems with Uarts...
Actually initially I got it working, as in I would be able to receive data, but for some odd reason, now when I try to read data in through the Uart, it would not read the data in correctly, and it would also constantly spit out 0's.· I'm thinking something is wrong with the byteAvailable() function because it should not say that any byte is available...
· final static int txi = CPU.pin12;
· final static int rsi = CPU.pin11;
· final static int flowi = CPU.pin13;
· static Uart txiuart = new Uart(Uart.dirTransmit, txi, Uart.invert,
· Uart.speed9600,Uart.stop1);
· static Uart rsiuart = new Uart(Uart.dirReceive, rsi, Uart.invert,
· Uart.speed9600,Uart.stop1);
· public static void main(){
··· int w1;
··· int w2;
··· int i;
··· char c;
··· w1=2;
··· System.out.println("beginning");
··· //Communications stuff
··· CPU.writePin(flowi,false);
··· i=0;
··· while (i<3) {
····· if (rsiuart.byteAvailable()) {
······· c=(char) (rsiuart.receiveByte());
······· if (c != 0) {
······· System.out.println("char: "+(int)c);
········· i++;
······· }
····· }
······ //System.out.println("char: "+(int)c);
··· }
··· System.out.println("We received data");
So what actually happens, is that inside the while loop, it should wait in the loop, until 3 characters are read from the serial port.· However, when the serial port is not sending anything, for some reason rsiuart.byteAvailable() returns true...
Actually initially I got it working, as in I would be able to receive data, but for some odd reason, now when I try to read data in through the Uart, it would not read the data in correctly, and it would also constantly spit out 0's.· I'm thinking something is wrong with the byteAvailable() function because it should not say that any byte is available...
· final static int txi = CPU.pin12;
· final static int rsi = CPU.pin11;
· final static int flowi = CPU.pin13;
· static Uart txiuart = new Uart(Uart.dirTransmit, txi, Uart.invert,
· Uart.speed9600,Uart.stop1);
· static Uart rsiuart = new Uart(Uart.dirReceive, rsi, Uart.invert,
· Uart.speed9600,Uart.stop1);
· public static void main(){
··· int w1;
··· int w2;
··· int i;
··· char c;
··· w1=2;
··· System.out.println("beginning");
··· //Communications stuff
··· CPU.writePin(flowi,false);
··· i=0;
··· while (i<3) {
····· if (rsiuart.byteAvailable()) {
······· c=(char) (rsiuart.receiveByte());
······· if (c != 0) {
······· System.out.println("char: "+(int)c);
········· i++;
······· }
····· }
······ //System.out.println("char: "+(int)c);
··· }
··· System.out.println("We received data");
So what actually happens, is that inside the while loop, it should wait in the loop, until 3 characters are read from the serial port.· However, when the serial port is not sending anything, for some reason rsiuart.byteAvailable() returns true...
Comments
Do you use a TTL-RS232 level converter like MAX232?
I noticed you use invert rather than dontInvert.
The method byteAvailable() does return false if there is no data.
So your connection either receives data or it picks up
some noise that is mistakenly seen as a startbit.
regards peter
while (i<3) {
System.out.print(rsiuart.byteAvailable());
if (rsiuart.byteAvailable()) {
c=(char) (rsiuart.receiveByte());
System.out.print((int)c);
if (c != 0) {
System.out.println("char: "+(int)c);
i++;
}
}
}
but what happens, is that it reads in some bits, and then the byteAvailable() goes to false. So it starts off true. And what is weird is that I unplug the rs232/rs485 from the computer, and then reset the javelin stamp, it still reads some data in the beginning. This is what I get:
beginning
true0true0true0true0true0true0true0true0true0true0true0true0true0true0true0true-4char: -4
falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefals
And this is what the stamp outputs even when it is not connected to the PC anymore and the rs232 is unplugged from the computer.
Thanks a lot for your help!
RO, DI, /RE and DE (receive out, data in, read enable, data enable).
From your pin assignments I guess
txi connects to DI
rsi connects to RO
flowi connects to /RE and DE.
Some chips have internal feedback from the transmitter to the receiver
and by changing the DE signal this may cause an effect on RO.
Pleasy specify used RS485 chip. (and connections)
Normally I use RS485 in half duplex mode, connecting RO via 1k resistor
to DI and a Javelin I/O pin for bidirectional data. A second Javelin I/O pin
connnects to /RE and DE. Changing the direction takes time due to the
Uart direction change that must be applied, so I never experienced any
random reads. You could do the same by rejecting any received data
during a short delay time after changing to receive mode.
regards peter