import stamp.core.*; public class SerialController { public int SerialPinInput = CPU.pin1; public int SerialPinOutput = CPU.pin2; public int SerialBaud = Uart.speed2400; public StringBuffer Buffer = null; public int recByte = 0; public int sendByte = 0; static Uart SerSen = null; static Uart SerRec = null; public Timer time = null; public boolean SerOut() { return SerOut(Buffer.toString()); } public boolean SerOut(int timeout) { return SerOut(Buffer.toString(), timeout); } public boolean SerOut(String value) { SerSen.setDirection(Uart.dirTransmit); SerSen.sendString(value); return SerSen.sendBufferEmpty(); } public boolean SerOut(String value, int timeout) { SerSen.sendString(value); time.mark(); while (!SerSen.sendBufferEmpty() && !time.timeout(timeout)); return SerSen.sendBufferEmpty(); } public boolean SerIn(int expects, int timeout) { Buffer.clear(); time.mark(); while (Buffer.length() < expects && !time.timeout(timeout)) { while (SerRec.byteAvailable()) { recByte = SerRec.receiveByte(); Buffer.append((char)recByte); //Buffer.append(recByte); } } if (Buffer.length() == 0) return false; return true; } public boolean SerIn() { Buffer.clear(); while (SerRec.byteAvailable()) { recByte = SerRec.receiveByte(); Buffer.append((char)recByte); //Buffer.append(recByte); } if (Buffer.length() == 0) return false; return true; } public void Dispose() { time = null; SerRec = null; SerSen = null; Buffer = null; } public SerialController(int ipin, int opin, int baud) { Buffer = new StringBuffer(255); SerialPinInput = ipin; SerialPinOutput = opin; SerialBaud = baud; time = new Timer(); SerSen = new Uart(Uart.dirTransmit,SerialPinOutput,Uart.invert,SerialBaud,Uart.stop1); SerRec = new Uart(Uart.dirReceive,SerialPinInput,Uart.invert,SerialBaud,Uart.stop1); } }