# Read from RX/TX Pins

**URL:** https://community.particle.io/t/read-from-rx-tx-pins/26375
**Category:** Hardware
**Created:** [October 7, 2016, 12:32am UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375 "2016-10-07T00:32:45Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![skasat](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@skasat](https://community.particle.io/u/skasat)
#### Post date: [October 7, 2016, 12:32am UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375/1 "2016-10-07T00:32:45Z")

</div>

Hello,

I have been working on the Sparkfun Photon WeatherShield and I have attached a Gieger Counter to the USART pins. I know that I have to use Serial 1 to see the data sent. But what do I write so that I can read from that pin.  
I have written-

```cpp
Serial.begin(9600);
Serial1.begin(9600,SERIAL_8E1);

if(Serial1.available()){
    client.publish("IFF ","IF"); 
    sensorstring=(char)Serial1.read();
    client.publish("Radiation: ", sensorstring);
 }
 //snprintf(payload, sizeof(payload), "Radiation : %f ", radiation);
 client.publish("Radiation: Second Try", sensorstring );

```

Can somebody please help me out!!  
I need this to be done fast.

---

<div class="post-metadata">

### Author: ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)
#### Post date: [October 7, 2016, 5:08am UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375/2 "2016-10-07T05:08:52Z")

</div>

> [@skasat](#):
>
> I need this to be done fast.

Then you should probably provide as much info as possible to start with.  
Omitting what your actual trouble with above code is and posting a contextless code snippet makes things slower for yourself.

---

<div class="post-metadata">

### Author: ![nrobinson2000](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nrobinson2000/32/15624_2.png) [@nrobinson2000](https://community.particle.io/u/nrobinson2000)
#### Post date: [October 7, 2016, 1:38pm UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375/3 "2016-10-07T13:38:30Z")

</div>

You need a way for the Photon to read strings properly from Serial:

Have a look a this:

```cpp

String readString = "";

void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
}

void loop()
{

  while (Serial1.available()) // While receiving characters over serial...
  {
    delay(3); // Necessary delay
    char c = Serial1.read(); // Read the character
    readString += c; // Add the character to the string
  }

  readString.trim();

  if (readString.length() > 0) // If a string has been read...
  {
    Serial.println("Received: " + readString); // Send the parsed string to Serial for debugging
    parseCommand(readString); // Do something with the string...
    readString = ""; // Clear the string
  }
}

```

---

<div class="post-metadata">

### Author: ![skasat](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@skasat](https://community.particle.io/u/skasat)
#### Post date: [October 7, 2016, 7:33pm UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375/4 "2016-10-07T19:33:04Z")

</div>

Hello,

Thank you for your response-

This is the data which is coming via the RX pin. I have read this via my terminal but even after following your method, I am not able to read anything. The data is empty.

Please help me out!

Data coming in-  
[mightyohm.com](http://mightyohm.com) Geiger Counter 1.00

> **[Geiger Counter](http://mightyohm.com/blog/products/geiger-counter/)**
>
> Introducing the MightyOhm Geiger Counter kit! Assembly Instructions Case Assembly Instructions Design Files Source Code Usage Instructions Support Features: 100% Open Source Hardware! LED and piezo…

CPS, 2, CPM, 2, uSv/hr, 0.01, SLOW

CPS, 0, CPM, 2, uSv/hr, 0.01, SLOW

CPS, 1, CPM, 3, uSv/hr, 0.01, SLOW

---

<div class="post-metadata">

### Author: ![skasat](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@skasat](https://community.particle.io/u/skasat)
#### Post date: [October 7, 2016, 7:39pm UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375/5 "2016-10-07T19:39:11Z")

</div>

I had given all the code which I had written for the RX/TX pins-

This is the new code which I am running but I am still having the data as empty/null.

Do we have to initialize RX/TX pins before reading from Serial1? Or does the data automatically transmit to Serial1?

```cpp
while (Serial1.available()) // While receiving characters over serial...
  {
    delay(3); // Necessary delay
    char c = Serial1.read(); // Read the character
    readString += c; // Add the character to the string
    //snprintf(payload, sizeof(payload), "Radiation Speed: ", readString);
    client.publish("TRY1: ", readString);
  }

  readString.trim();
  
  if (readString.length() > 0) // If a string has been read...
  {
    Serial.println("Received: " + readString); // Send the parsed string to Serial for debugging
    
    client.publish("TRY2: ", readString);
    readString = ""; // Clear the string
  }

```

---

<div class="post-metadata">

### Author: ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)
#### Post date: [October 7, 2016, 8:08pm UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375/6 "2016-10-07T20:08:12Z")

</div>

> [@skasat](#):
>
> The data is empty.

What does that mean? What _data_ is empty?  
You say

> [@skasat](#):
>
> This is the data which is coming via the RX pin. I have read this via my terminal but even after following your method, I am not able to read anything.

Did you now read _this data_ or are you _not able to read anything_?  
That's rather confusing 😕

I'd say that `delay(3)` in that loop is rather long.  
You could go with [`Serial1.readStringUntil()`](https://docs.particle.io/reference/firmware/photon/#readstringuntil-) instead, since you know what data you're looking for.

* * *

> [@skasat](#):
>
> I had given all the code which I had written for the RX/TX pins-

That's assuming that there is no issue burried in supposedly unrelated areas of your code.

> [@skasat](#):
>
> Do we have to initialize RX/TX pins before reading from Serial1?

That's done via `Serial1.begin()`.

> [@skasat](#):
>
> Or does the data automatically transmit to Serial1?

That depends on your wiring and your datasource (sensor).  
Some sensors transmit unasked others need a request command.

---

<div class="post-metadata">

### Author: ![skasat](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@skasat](https://community.particle.io/u/skasat)
#### Post date: [October 7, 2016, 8:11pm UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375/7 "2016-10-07T20:11:25Z")

</div>

Okay, so 2 scenarios-

1. When I connect my Geiger counter to my computer via a FTDI I am able to get information transmitted on Putty.
2. When I connect my Geiger Counter to Photon via the RX pin, I am trying to read that data, it is coming as null/empty.

What can I do?

---

<div class="post-metadata">

### Author: ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)
#### Post date: [October 7, 2016, 8:19pm UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375/8 "2016-10-07T20:19:08Z")

</div>

You’ve got TX of the sensor hooked up to RX on the Photon and vice versa?

Try this most simple sketch

```cpp
void setup() {
  Serial.begin(115200);
  Serial1.begin(9600);
}

void loop() {
  static uint32_t ms;
  while(Serial1.available()) {
    Serial.write((uint8_t)Serial1.read());
  }

  if(millis()-ms > 1000) {
    ms = millis();
    Serial.print(".");
  }
}

```

and see what your serial terminal gives you then.

BTW, why were you using 8E1 for `Serial1` while the sensor seems to require `8N1` (which is default for `Serial1` anyway)?

> **[Usage Instructions](http://mightyohm.com/blog/products/geiger-counter/usage-instructions/)**
>
> How to use it! Easy, turn on the switch and listen for clicks! Watch the LED, it will flash every time a beta particle or gamma ray hits the tube! (Note: The tube that comes with the kit is not sen…

---

<div class="post-metadata">

### Author: ![skasat](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@skasat](https://community.particle.io/u/skasat)
#### Post date: [October 10, 2016, 7:14pm UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375/9 "2016-10-10T19:14:20Z")

</div>

Hello,

Thank you for the reply. I tried those things and I get these two errors. Please help me out. And yes I made it to the default for Serial1.  
weather\_station.cpp:127:17: error: cannot convert ‘USARTSerial::available’ from type ‘int (USARTSerial::)()’ to type ‘bool’  
// attach external interrupt pins to IRQ functions  
weather\_station.cpp:128:40: error: invalid conversion from ‘int’ to ‘const char\*’ [-fpermissive]  
attachInterrupt(RAIN, rainIRQ, FALLING);  
^

---

<div class="post-metadata">

### Author: ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)
#### Post date: [October 10, 2016, 7:30pm UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375/10 "2016-10-10T19:30:28Z")

</div>

I forgot the `()` at the end of `Serial1.available` - it should be

```cpp
  while(Serial1.available()) 

```

And the other one needs a type cast

```cpp
 Serial.write((uint8_t)Serial1.read());

```

* * *

I’ve updated the code above accordingly

---

<div class="post-metadata">

### Author: ![skasat](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@skasat](https://community.particle.io/u/skasat)
#### Post date: [October 10, 2016, 7:45pm UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375/11 "2016-10-10T19:45:49Z")

</div>

Thanks for the quick reply.  
Getting this-  
weather\_station.cpp:130:11: error: ‘unit8\_t’ was not declared in this scope  
delay(10000);  
^

---

<div class="post-metadata">

### Author: ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)
#### Post date: [October 10, 2016, 7:47pm UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375/12 "2016-10-10T19:47:57Z")

</div>

Because it’s `uint8_t` and not `unit8_t` 😉

But here the error message was quite clear about the reason

---

<div class="post-metadata">

### Author: ![skasat](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@skasat](https://community.particle.io/u/skasat)
#### Post date: [October 10, 2016, 10:36pm UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375/13 "2016-10-10T22:36:55Z")

</div>

Thank you that worked, but I am still not receiving what I am expecting-  
Input-

static uint32\_t ms;  
String res="";  
while(Serial1.available()) {  
res+=(uint8\_t)Serial1.read();

}  
client.publish(“TRY”, res);  
if(millis()-ms \> 1000) {  
ms = millis();  
Serial.print(".");  
}

Output-  
TRY 000000000000000000000000000000000000000000000000000000000000000

Whereas I should have got-  
TRY [Mightyohm.org](http://Mightyohm.org)…(it goes on)

---

<div class="post-metadata">

### Author: ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)
#### Post date: [October 11, 2016, 4:57am UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375/14 "2016-10-11T04:57:11Z")

</div>

The reason why I suggested `Serial.write()`ing the received data over `client.publish()`ing it was to cut out one possible cause of that issue, especially since I’m missing the info what that `client` actually is. You only revealed part of your code and not the bit that actually shows where you instantiate the `client` object.

---

<div class="post-metadata">

### Author: ![skasat](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@skasat](https://community.particle.io/u/skasat)
#### Post date: [October 14, 2016, 8:26pm UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375/15 "2016-10-14T20:26:47Z")

</div>

I tried using serial.write() but it is giving me non-supported characters but the periods appear. What do you think I should do? Is there any other method to read from serial1?

---

<div class="post-metadata">

### Author: ![nrobinson2000](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nrobinson2000/32/15624_2.png) [@nrobinson2000](https://community.particle.io/u/nrobinson2000)
#### Post date: [October 14, 2016, 8:47pm UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375/16 "2016-10-14T20:47:14Z")

</div>

What if you use `Serial.print()` instead?

---

<div class="post-metadata">

### Author: ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)
#### Post date: [October 15, 2016, 6:20am UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375/17 "2016-10-15T06:20:53Z")

</div>

`Serial.write()` gives you back what you receive, `Serial.print()` will “translate”.  
So when you expect readable characters via `Serial1.read()` but get gibberish back via `Serial.write()` I’d say you’ve either set the wrong protocol parameters or your wiring is messing things up.

Can you post your current code again?

---

<div class="post-metadata">

### Author: ![skasat](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@skasat](https://community.particle.io/u/skasat)
#### Post date: [October 15, 2016, 7:09am UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375/18 "2016-10-15T07:09:24Z")

</div>

Serial.print() doesn’t seem to work either.

---

<div class="post-metadata">

### Author: ![skasat](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@skasat](https://community.particle.io/u/skasat)
#### Post date: [October 15, 2016, 7:10am UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375/19 "2016-10-15T07:10:37Z")

</div>

I checked everything again and everything looks good on the wiring part. Here is the code and different options that I have tried using. Nothing is working.

```cpp
static uint32_t ms;
  while(Serial1.available()) {
    Serial.write((uint8_t)Serial1.read());
  }

  if(millis()-ms > 1000) {
    ms = millis();
    Serial.print(".");
  }
  /*Serial1.println("TEST"); 
  static uint32_t ms;
  String res;
  int cnt=0;
  char chr;
  while(cnt<100){ 
  while(Serial1.available()) {
    chr=Serial1.read();
    res+=(String)chr;
    //client.publish("TRY", res);
// Serial.println(Serial1.read());
    cnt++;
  }
  }
  client.publish("TRY", res);
  res="";
  if(millis()-ms > 1000) {
    ms = millis();
    Serial.print(".");
  }*/
  
  
  /*String part1,part2,part3,part4,part1val,part2val,part3val;
 while (Serial1.available()){
     char c= Serial1.read();
     if (c=="\n"){
         part1="CPS";
         part1val;
         part2="CPM";
         part2val;
         part3="uSv/hr";
         part3val;
         part4;
        
        int comma=res.indexOf(",");
        part1val= res.substring(comma+1,res.indexOf(", CPM"));
        part2val= res.substring(res.indexOf(part2)+4,res.indexOf(part2)+6);
        part3val= res.substring(res.indexOf(part3)+7,res.indexOf(part3)+11);
        part4=res.substring(res.indexOf(part3)+11,res.length());
        res="";
        
     }
     else{
         res+=c;
     }
     */
     
    /*
    char res;
    int chr;

   while (Serial1.available()){
        res=Serial1.read();
        Serial.print("--");
        Serial.println(res);
        //res+=(String)chr;
    }
    */
    
    
    /*
    char storage[7];
    int count=0;
    String str="";
    while ((str = strtok(res, ",", &res)) != "\n"){ // delimiter is the comma
        storage[count]=str;
        count++;
    }*/

```

---

<div class="post-metadata">

### Author: ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)
#### Post date: [October 15, 2016, 7:38am UTC](https://community.particle.io/t/read-from-rx-tx-pins/26375/20 "2016-10-15T07:38:52Z")

</div>

That’s only a part of your code, I’d like to see all of it - including `setup()` and all definitions/declarations.

[Next page](https://community.particle.io/t/read-from-rx-tx-pins/26375.md?page=2)
