-1

I have a device feeding Rx Tx data every minute. I connected it to the computer. I downloaded a Serial Port Monitor Software, and here is the data I captured using that software.

enter image description here

Now when I try to capture the same data using Arduino, it captures something else as you can see in the image below. I tried reading it as hex, char, byte, unsigned int, but it just doesn't read the data properly.

enter image description here

Here I have provided my code. Can someone please identify the problem for me? I've been stuck on this for 2 weeks at least, and it is just not a good feeling! Any help will be much appreciated.

Using Arduino Ethernet with microSD Card Slot

//#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>


static const int baud = 19200;
static const int chipSelect = 4;
static const int rx = 0;
static const int tx = 1;


File dataFile;
//SoftwareSerial serialPort = SoftwareSerial(rx, tx);


void setup() {

  Serial.begin(baud);     // opens serial port, sets data rate to 19200 bps
  while (!Serial) {
    ;
  }
  //serialPort.begin(baud);

 // pinMode(rx, INPUT);
 // pinMode(tx, OUTPUT);

   Serial.print("Initializing SD card...");

   if (!SD.begin(chipSelect)) {
       Serial.println("initialization failed!");
       return;
   }
   Serial.println("initialization done.");


   dataFile = SD.open("data.txt", FILE_WRITE);
   Serial.println("Data file created!");
   dataFile.close();
}

unsigned int incomingByte = 0;
void loop() 
{
  if (Serial.available() > 0) {
                // read the incoming byte:
     incomingByte = Serial.read();
     Serial.print(incomingByte, HEX);
    }

  dataFile = SD.open("data.txt", FILE_WRITE);
  dataFile.println(incomingByte, HEX);
  dataFile.close();
}

You can build the Serial Port Monitor software using C# Console App, here is the code for that:

using System;
using System.IO.Ports;
using System.Text;

class PortDataReceived
{
    public static void Main()
    {

        SerialPort mySerialPort = new SerialPort("COM3");

        mySerialPort.BaudRate = 19200;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;
        mySerialPort.RtsEnable = true;


        mySerialPort.Open();
        while (true)
        {
            string data = ReadData(mySerialPort);

            if (!string.IsNullOrEmpty(data))
            {
                Console.WriteLine(data);

            }
        }

    }

    public static string ReadData(SerialPort serial)
    {
        byte[] buffer = new byte[serial.BytesToRead];
        serial.Read(buffer, 0, buffer.Length);

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < buffer.Length; i++)
            sb.AppendFormat("{0:X2} ", buffer[i]);

        return sb.ToString();
    }
}
0

It's not a good idea to use pins 0 and 1 for SoftwareSerial they are usually connected to Serial

  • 1
    Not only that but they seem to be mixing and matching both hardware and software serial on the same pins. That's never going to be very nice at the best of times. In fact I suspect that all the software serial calls will fail since the pins are configured to talk to the UART peripheral. – Majenko Dec 13 '16 at 15:59
  • I updated the answer to explain why 0/1 are a bad idea :) – Visual Micro Dec 13 '16 at 16:10
  • Thank you for your quick response guys. What would be the fix for it? I want to read the Serial port. And only pins 0 and 1 can give me access to RX and TX lines. – JayB Dec 13 '16 at 16:18
  • 1
    Delete all references to software serial. Just use Serial. SoftwareSerial is for those times when you want to use pins OTHER THAN the TX/RX pins. – Majenko Dec 13 '16 at 16:19
  • Updated my code according to your suggestions. It is still giving the same hex values though! – JayB Dec 13 '16 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.