Communications protocol over newsoftserial

I am using newsoftserial8 at 9600 baud on a 328 under v15; this loop is supposed to read a character from an applet on the xport and reply; instead it goes into an infinite loop and keeps receiving characters when no more are being sent.

void loop() { if (commport.available() != 0) { c = (char)commport.read(); input[counter++] = c; } commport.flush(); counter = 0; if (input[0] == '1') { Serial.print("received "); Serial.println(input); } else Serial.println("got garbage"); commport.print("1"); commport.print("2"); }

Characters are added to the buffer using this...

input[counter++] = c;

But your program only reads the very first character sent...

if (input[0] == '1')

This may not explain the problem you're having but it definately look suspicious.

Good luck, Brian

Thanks for the reply, but that is innocuous and definitely not the problem.

Oh, I see where mikalhart knows that newsoftserial doesn't work right under v15. Need to wait, I guess.

Mike,

I wouldn't expect there to be any trouble at 9600 baud, even with Arduino 0015. Perhaps an occasional corrupted byte at 14.4K or higher, but certainly not continuous flow of bytes. Can you describe your problem in a little more detail?

You realize, of course, that you are sending a '1' and a '2' to the soft serial port every iteration through the loop, whether or not a character is received?

Mikal

I guess this loop member takes a little getting used to; I managed to get things rational with this code:

void loop()
{
if (commport.available() != 0)
{
c = (char)commport.read();
input[counter++] = c;
commport.flush();
counter = 0;
if (input[0] == ‘1’)
{
Serial.print("received ");
Serial.println(input);
commport.print(“1”);
commport.print(“2”);
input[0] = 0;
}
else
Serial.println(“got garbage”);
}
}

I will play with this code and see were the problem was exactly - I don’t see why sending 1 and 2 out should affect input counts in available(). The applet code is:

header[0] = ‘1’; // send request for ROM value
gtp.send(header);
header = gtp.receive();
if (header[0] == ‘1’)
{
System.out.println(“got 1 back”);
msgbody = gtp.receive();
msgbodystring = new String(msgbody);
System.out.println(“got " + msgbodystring + " after that”);
address.setText(msgbodystring);
}
else
System.out.println(“got garbage”);

where gtp is a Java socket i/o handler and is called only once.

Ooops - spoke too soon: now I don’t get the ‘2’ back from newsoftserial.

This was fixed by a) reducing speed to 2400 and b) removing hardware handshaking. Conclusion is that newsoftserial cannot support hadware handshaking.