Shop OBEX P1 Docs P2 Docs Learn Events
Serial com, reading one line at a time — Parallax Forums

Serial com, reading one line at a time

AcidshockAcidshock Posts: 7
edited 2005-10-17 06:28 in General Discussion
Hey guys,

Got a javelin here and I am using it to communcate with a modem via AT commands. Problem is·when using the Hypertermcom example I send a command, get back the result but if I call my routine to send the command and check the result again I get the first command. I am pretty sure this is due to the javelins internal buffer(from what I have read) but I am not sure the easiest way to approach. I have looked at Peter's Uart additions and noticed he has a clear buffer command. I was thinking of maybe using it to clear the buffer after every command sent to prevent this but I would like peoples opinions if there is a better way. Preferably I would like to get to the point that I would just read one line at a time and once the line is read I dont have to worry about it any more.


Best regards,
Drew


  import stamp.core.*;
  public class MDMCOM {
  //Com Port
    final static int SERIAL_TX_PIN = CPU.pin0; //DB9 2
    final static int SERIAL_RTS_PIN = CPU.pin1; //DB9 7
    final static int SERIAL_CTS_PIN = CPU.pin2; //DB9 8
    final static int SERIAL_RX_PIN = CPU.pin3;  //DB9 3
//Set pins directly for now for testing purposes
    static Uart txUart = new Uart( Uart.dirTransmit, CPU.pin0, Uart.dontInvert, Uart.speed9600, Uart.stop1 );
    static Uart rxUart = new Uart( Uart.dirReceive, CPU.pin3, Uart.dontInvert, Uart.speed9600, Uart.stop1 );
    static StringBuffer buffer = new StringBuffer(120);
    static char c;
    static void bufferMessage(){

    while (c != '\r'){
      if(rxUart.byteAvailable()){
        c = (char)rxUart.receiveByte();

        buffer.append(c);
        }
        }
  }

  public static void main(){
  System.out.print("Testing program\n\r");
 buffer.clear();
  txUart.sendString("AT\r");
  bufferMessage();
  System.out.print(buffer.toString());
  buffer.clear();
  txUart.sendString("AT+CREG\r");
  bufferMessage();
  if(buffer.toString() == "ERROR")
  {
  System.out.print("\nError found!\n\r");
  }
  else
  {
  System.out.print(buffer.toString());
  }
  }
}

Post Edited (Acidshock) : 10/17/2005 1:55:57 AM GMT

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-10-17 05:29
    Whenever you call rxUart.byteReceive(), the first available byte is removed

    from the uart buffer and returned by the call. So you don't need to clear the uart buffer.

    You can't send an AT command to hyperterminal. If you open a com port in hyperterminal

    that connects to the javelin, there is no way hyperterminal can relay any received command

    to a modem. You should connect a modem (via level shifter like max232) to the javelin.

    Most modems require more than just tx and rx signals.

    You then can visualize the modem<->javelin communication by System.out.print() calls.

    regards peter
  • AcidshockAcidshock Posts: 7
    edited 2005-10-17 06:28
    Thanks for the response Peter.

    I know I cant send AT commands to Hyperterm, just been using it to simulate my interaction with the device(sO I can see exactly what both sides are sending and receiving) Now I have the device hooked up and the device still doesnt clear the buffer, I am having to manually clear it via your addon. You mentioned byteReceive() however I am not seeing that anywhere in the Uart.java file. I do see receiveByte do you mean that by chance? I am currently using that but for some reason every time I use that method it does not seem to remove it. Here is the current function I am using to extract the data. Its working for me at the moment but its rather dirty and I much rather a cleaner solution.


    //bufferMessage:
    //This function will parse the internal Uart buffer and feed it into a string buffer
    //named "buffer."
    //The first value of the function is how long to wait until parsing the buffer.
    //The second value is what line of the receive buffer you want to start at.

    static void bufferMessage(int time, int startline)
    {
    buffer.clear();
    int currentline = 1;
    CPU.delay(time);
    while (rxUart.byteAvailable() == true){
    c = (char)rxUart.receiveByte();
    if(startline > currentline)
    {
    if(c == '\r' || c == '\n')
    {
    currentline++;
    }
    }
    else
    {
    if(c == '\r' || c == '\n')
    {
    if(currentline > (startline+1))
    {
    buffer.append(c);
    }
    else
    {
    }
    currentline++;
    }
    else
    {
    buffer.append(c);
    }
    }
    }
    rxUart.bufferClear();
    }
Sign In or Register to comment.