# Getting a UART to work

**URL:** <https://forum.arduino.cc/t/getting-a-uart-to-work/1011328>\
**Category:** 3rd Party Boards\
**Created:** [July 11, 2022, 7:04am UTC](https://forum.arduino.cc/t/getting-a-uart-to-work/1011328 "2022-07-11T07:04:28Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![howiel](https://avatars.discourse-cdn.com/v4/letter/h/f17d59/32.png) [@howiel](https://forum.arduino.cc/u/howiel)\
**Post date:** [July 11, 2022, 7:04am UTC](https://forum.arduino.cc/t/getting-a-uart-to-work/1011328/1 "2022-07-11T07:04:29Z")

</div>

My board is a Pro Micro with the on board UART as Serial1 (RX on 0, TX on 1) and the virtual UART as Serial on the USB. I am talking to the modem on a SIMCOM7000G. The modem is locked at 9600 8N1. TX on the modem is connected to RX (pin 0) on the board, and RX on the modem is connected to TX (pin 1) on the board.

I'm sending a short AT string to the modem and expecting a response, but not getting any.

This is a test sketch that I am using:

```cpp
#define MONITOR true
#define BAUD 9600

const char AT_ECHO_ON[] PROGMEM = {"ATE1\r\n"};

void uart_put_P(const uint8_t *s) {
  while (pgm_read_byte(s) != 0x00) {
    uint8_t uartByte = (pgm_read_byte(s++));
    Serial1.write(uartByte);
    if (MONITOR) {
      Serial.write(uartByte);
    }
  }
}

char recvText[161];
void uart_get() {
  uint8_t b = 0;
  if (MONITOR) Serial.print("Serial1.available:\t"); Serial.println(Serial1.available());
  while (Serial1.available() > 0) {
    uint8_t recvByte = Serial1.read();
    recvText[b] = recvByte;
    b++;
    if (MONITOR) {
      Serial.write(recvByte);
    }
  }
  recvText[b] = 0x00;
}

void setup() {

  if (MONITOR) {
    Serial.begin(BAUD); // Serial is the virtual UART via USB
    while (!Serial) {
      ; // we're going nowhere if we can't talk to the monitor when we need to
    }
  }

  Serial1.begin(BAUD); // Serial1 is the real UART connected to the modem RXD on 0, TXD on 1
  while (!Serial1) {
    ; // we're going nowhere if there is no connection to the modem
  }
  uart_put_P(AT_ECHO_ON);
  uart_get();
}

void loop() {
}
```

This is what I see on the Serial monitor:  
16:30:34.407 -\> ATE1  
16:30:34.407 -\> Serial1.available: 0

To check whether there is actually any output from the modem I then diverted the modem TX away from the Micro board and fed it into a separate monitor program. This is what I see in this monitor:  
[16:30:34:416] ATE1␍␍␊

[16:30:34:416] OK␍␊

This tells me that my input is being accepted by the modem, the command is being echoed, and there is an "OK" acknowledgement.

My question is: Why is the Serial1.available showing "0", hence blocking my uart\_get() function from not reading the modem output.

---

<div class="post-metadata">

**Author:** ![jfjlaros](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jfjlaros/32/716915_2.png) [@jfjlaros](https://forum.arduino.cc/u/jfjlaros)\
**Post date:** [July 11, 2022, 8:00am UTC](https://forum.arduino.cc/t/getting-a-uart-to-work/1011328/2 "2022-07-11T08:00:02Z")

</div>

> [@howiel](#):
>
> ```arduino
> uart_put_P(AT_ECHO_ON);
> uart_get();
> 
> ```

You might have to give the modem more time to respond.

```arduino
uart_put_P(AT_ECHO_ON);
while(not Serial.available()); // Or test with: delay(500);
uart_get();

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 11, 2022, 8:00am UTC](https://forum.arduino.cc/t/getting-a-uart-to-work/1011328/3 "2022-07-11T08:00:31Z")

</div>

You can’t expect the answer to be ready a few micro seconds after you sent the command - that’s why available() returns 0.

Your UART is an asynchronous protocol so you should not second guess timing. Wait until something is available and read it. Accumulate data until some sort of end of message is reached, analyze the answer and proceed.

I would suggest to study [Serial Input Basics](http://forum.arduino.cc/index.php?topic=396450.0) to better understand the concepts of dealing with a serial line

May be you’ll be interested in reading this too [Facing problem on sending SMS on different condition Using GSM module and Arduino mega - #18 by J-M-L](https://forum.arduino.cc/t/facing-problem-on-sending-sms-on-different-condition-using-gsm-module-and-arduino-mega/937020/18)

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [July 11, 2022, 1:41pm UTC](https://forum.arduino.cc/t/getting-a-uart-to-work/1011328/4 "2022-07-11T13:41:01Z")

</div>

> [@howiel](#):
>
> ```arduino
> while (!Serial1) {
> ; // we're going nowhere if there is no connection to the modem
> }
> 
> ```

Not helping but this doen's do what the comment says. The code will just fly through it, with or without modem connected.

---

<div class="post-metadata">

**Author:** ![howiel](https://avatars.discourse-cdn.com/v4/letter/h/f17d59/32.png) [@howiel](https://forum.arduino.cc/u/howiel)\
**Post date:** [July 12, 2022, 7:00am UTC](https://forum.arduino.cc/t/getting-a-uart-to-work/1011328/5 "2022-07-12T07:00:56Z")

</div>

Sure, if data comes from the modem and we're not connected the - squirrels, but If we're not connected to the modem then we're not going to be able to send data to the modem in the first place,

---

<div class="post-metadata">

**Author:** ![howiel](https://avatars.discourse-cdn.com/v4/letter/h/f17d59/32.png) [@howiel](https://forum.arduino.cc/u/howiel)\
**Post date:** [July 12, 2022, 7:05am UTC](https://forum.arduino.cc/t/getting-a-uart-to-work/1011328/6 "2022-07-12T07:05:11Z")

</div>

Putting the _uart\_Get_ and _show\_Data_ in the _loop_ does work because of the loop, but I'm trying to get a loop to work in the _setup_ and that is causing me grief - just can't get the logic right...🙂  
Actually I think the problem might be that there are two _\n_ characters - one before the OK and another after. The string that is returning is _\r\nOK\r\n_ so it looks like I might have to count up the _\r_

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 12, 2022, 8:10am UTC](https://forum.arduino.cc/t/getting-a-uart-to-work/1011328/7 "2022-07-12T08:10:17Z")

</div>

> [@howiel](#):
>
> Putting the _uart\_Get_ and _show\_Data_ in the _loop_ does work because of the loop

Yeah if you ask many times after a while the data will show up. But then you need to architect the code so that the loop keeps looping until you can issue the next AT command… you need some sort of state machine to see if you are waiting for the answer to a command or if you can Proceed

---

<div class="post-metadata">

**Author:** ![howiel](https://avatars.discourse-cdn.com/v4/letter/h/f17d59/32.png) [@howiel](https://forum.arduino.cc/u/howiel)\
**Post date:** [July 13, 2022, 11:14pm UTC](https://forum.arduino.cc/t/getting-a-uart-to-work/1011328/8 "2022-07-13T23:14:42Z")

</div>

I have actually now got some sensible responses from the uart y putting the initialisation AT statements into _loop_ and ensuring that it only runs once in there.  
My next problem looks to be the uart RX buffer size. I issue a _AT+CGNSINF_ to tell the modem to send the GPS data string. I know that this string is about 100 characters long but it looks as if I'm only getting 64 bytes. I tried sending a short delay and another _uart\_get()_ but nothing. Do I need to keep flogging the horse for about 10 iterations to see if anything more comes in.

---

<div class="post-metadata">

**Author:** ![howiel](https://avatars.discourse-cdn.com/v4/letter/h/f17d59/32.png) [@howiel](https://forum.arduino.cc/u/howiel)\
**Post date:** [July 13, 2022, 11:19pm UTC](https://forum.arduino.cc/t/getting-a-uart-to-work/1011328/9 "2022-07-13T23:19:02Z")

</div>

Bingo, that worked!!

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [July 14, 2022, 2:44am UTC](https://forum.arduino.cc/t/getting-a-uart-to-work/1011328/10 "2022-07-14T02:44:28Z")

</div>

> [@howiel](#):
>
> I know that this string is about 100 characters long but it looks as if I'm only getting 64 bytes. I tried sending a short delay and another _uart\_get()_ but nothing. Do I need to keep flogging the horse for about 10 iterations to see if anything more comes in.

The Serial class uses a buffer implemented in software that has a size of 64 bytes. You can study Robin's [Serial Input Basics - updated](https://forum.arduino.cc/t/serial-input-basics-updated/382007) to get some ideas how to work around that problem.

But seeing that you seem to have solved the issue, it will be a bit like putting the horse behind the cart 😉 But it ill be a useful read anyway.

> [@howiel](#):
>
> Bingo, that worked!!

It might help other people that encounter the same problem if you post the working code. It might also be useful to get some feedback if needed.

---

<div class="post-metadata">

**Author:** ![howiel](https://avatars.discourse-cdn.com/v4/letter/h/f17d59/32.png) [@howiel](https://forum.arduino.cc/u/howiel)\
**Post date:** [July 14, 2022, 3:46am UTC](https://forum.arduino.cc/t/getting-a-uart-to-work/1011328/11 "2022-07-14T03:46:08Z")

</div>

What I did was use a 'for' loop with a 50ms delay and 5 iterations of the uart\_get() function.

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [July 14, 2022, 3:48am UTC](https://forum.arduino.cc/t/getting-a-uart-to-work/1011328/12 "2022-07-14T03:48:06Z")

</div>

I suggest that you read Robin's tutorial. There is no need for _delay()_.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 14, 2022, 8:12am UTC](https://forum.arduino.cc/t/getting-a-uart-to-work/1011328/13 "2022-07-14T08:12:44Z")

</div>

> [@howiel](#):
>
> What I did was use a 'for' loop with a 50ms delay and 5 iterations of the uart\_get() function.

This is you trying to second guess the timing of an asynchronous process… it’s not robust.

The tutorial you are being referred to is the way to handle that : just accumulate the data while it’s coming and when it’s all there (end marker) deal with the command/data

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 10, 2023, 8:13am UTC](https://forum.arduino.cc/t/getting-a-uart-to-work/1011328/14 "2023-01-10T08:13:05Z")

</div>

Closed on January 10, 2023, 8:13am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
