Close

Hardware Serial through and through

A project log for Open Source Cell Phone

A completely open hardware and open source software cell phone

hugh-darrowHugh Darrow 08/17/2014 at 03:550 Comments

8/16/2014

So I did some reading on the Mega and its extra serial ports, and the coding involded ect. and switching the software serial port to hardware is really quite easy. On the mega the extra serial ports are labeled such as Pin19 is RX1 and Pin18 is TX1. So first add the two jumper cables to the new pins like so 

What would be Pin7 needs to connect to Pin19/RX1 and Pin8 connects to Pin18/TX1

To change the software settings lets examine our arduino sketch, even the software changes are easy, I just used find and replace

//Serial Relay - Arduino will patch a
//serial link between the computer and the GPRS Shield
//at 4800 bps 8-N-1
//Computer is connected to Hardware UART
//GPRS Shield is connected to the Software UART

#include <SoftwareSerial.h>

SoftwareSerial GPRS(50, 8); //RX pin, TX pin
unsigned char buffer[64]; // buffer array for data recieve over serial port
int count=0; // counter for buffer array
void setup()
{
GPRS.begin(4800); // the GPRS baud rate
Serial.begin(4800); // the Serial port of Arduino baud rate.
}

void loop()
{
if (GPRS.available()) // if date is comming from softwareserial port ==> data is comming from gprs shield
{
while(GPRS.available()) // reading data into char array
{
buffer[count++]=GPRS.read(); // writing data into array
if(count == 64)break;
}
Serial.write(buffer,count); // if no data transmission ends, write buffer to hardware serial port
clearBufferArray(); // call clearBufferArray function to clear the storaged data from the array
count = 0; // set counter of while loop to zero


}
if (Serial.available()) // if data is available on hardwareserial port ==> data is comming from PC or notebook
GPRS.write(Serial.read()); // write it to the GPRS shield
}
void clearBufferArray() // function to clear buffer array
{
for (int i=0; i<count;i++)
{ buffer[i]=NULL;} // clear all index of array with command NULL
}

To setup extra serial ports just change a couple lines of code first we delete the lines

#include <SoftwareSerial.h>

SoftwareSerial GPRS(50, 8); //RX pin, TX pin

then replace the invocation of GPRS with Serial1, simple right? But don't upload the new code yet, first we need to tell the SIM900 module to start talking at the new speed. First open back up the program sscom32e.exe, and send the command

AT+IPR=19200

This will change the baud rate of the SIM900, after which change the baud rate used in the program, then close the port (CloseCom button) or close the program

Then upload the new sketch, open the sscom program, reopen the port and start sending commands

A new file has been uploaded to the github named GPRS_HardwareSerial_Test.ino with the modifications already made for the hardware serial, and the previous GPRS_Test.ino file has been named GPRS_SoftwareSerial_Test.ino

For more information on the Mega and the extra serial ports and how to use them refer to here.

http://arduino.cc/en/Tutorial/MultiSerialMega

Discussions