Hello all.
I'm working with a pic 16f877A circuit that gives me back on is Serial Port a String with 6 bytes long containing a number.
I was using MEGA to work with it but since I need a small circuit I decid to convert the sketch to a arduino mini pro.
Well arduino mini pro just have 1 hardware serial soo I add to the skect NewSoftSerial to give me more virtual serials.Now I have one working and another not.
All I do was creat the virtual Serial ports but the output from each are diferent
I cant figure out what is the problem.Perhaps it could be a buffer problem?
any help
here are both sketchs
Woking ok
int i = 0;
char counter[7];
void setup() {
// initialize both serial ports:
Serial.begin(9600);
Serial1.begin(9600);
}
void loop()
{
Serial.println(get_counter());
delay(1000);
}
String get_counter()
{
Serial1.print("R");//To seend an order to pic give me back the counter number It will ripply with 6 bytes
for (int i = 0;i<=5;i++){
counter[i] = Serial1.read();
//Serial.print(counter[i]);
}
counter[6] = '\0'; //Terminate the String element on the array
return counter;
}
This code works fine
I convert it to this using the SoftSerial
#include <NewSoftSerial.h>
NewSoftSerial pic(2, 3); //RX2 TX3
NewSoftSerial pc(4, 5); //RX4 TX5
int i = 0;
char counter[7];
void setup() {
// initialize both serial ports:
Serial.begin(9600);
pc.begin(9600);
pic.begin(9600);
}
void loop()
{
Serial.println(get_counter());
delay(1000);
}
String get_counter()
{
pic.println("R");//To seend an order to pic give me back the counter number It will riply with 6 bytes
for (int i = 0;i<=5;i++){
//Serial.print(i);
//Serial.print("=");
counter[i] = (char)pic.read();
//Serial.print(counter[i]);
}
counter[6] = '\0'; //Terminate the String element on the array
return counter;
}
This function stores data in a global character array, properly NULL terminating. Then, it converts that global array to a String variable to return. The only that that happens with that String object is printing.
The Serial.print() function is perfectly capable of printing the global character array.
So, why are you dragging in the overhead of the String class?
NewSoftSerial pc(4, 5); //RX4 TX5
What is this instance for?
All I do was creat the virtual Serial ports but the output from each are diferent
Thanks for your fast answer
The reason of creating this is because i need to get what is in the pic side and store it on a string to then seend it via SMS.The modem waits for a string on the message field.
Like I say in my first sketch the output is fine, it will be something like 13 but the first elements of the array will be empty because the pic always give 6 bytes.When I change it to the skecth using softserial for the same value on the pic the output is like
If I understand you correctly you say that the first sketch works.. ?
Why aren't you using .available()?
Why are you returning a char array where you say you will be returning a string?
Did you connect the cables correctly/specify them correctly?
// initialize both serial ports:
Serial.begin(9600);
pc.begin(9600);
pic.begin(9600);
I count three serial initializations..
Also, if you read the documentation for softwareserial you will notice the following:
Only one software serial port can listen at a time; data that arrives for other ports will be discarded.
ok I will try to explain better what I need to do and what I have.I have a pic 16f887A that is counting pieces on a machine and when received an "R" on is uart port it reply the number of pieces already counted in a form like " 23".The pic give me 6 bytes always!! In this case just the byte number5 (2) and the byte6(3) are filled, the byte 1,2,3,4 are with space.
Since I had this configuration I decid to collect all the bytes the pic give me back and fill it on an array with 8 elements to then convert it on an simple string. Like I say I have sucssefuly do this on hard serial using the first sketch but I need to use virtual serial because I want to interface the pic with a arduino mini pro that just have 1 serial and I need 2
I just add the newsoftserail library and convert all the skectk but it does not have the same behavier and the output is diferent.
I cant see why the library does not work.
If I cant do this i will be forced to give up on the mini pro and put a Mega on is place using the hardware serial ports.
I also try to add the pic.listen() but the output is the same.
I just received garbage in the output
#include <icrmacros.h>
#include <SoftwareSerial.h>
SoftwareSerial pic(2, 3); //RX2 TX3
//NewSoftSerial pc(4, 5); //RX4 TX5
int i = 0;
char counter[7];
void setup() {
Serial.begin(9600);
//pc.begin(9600);
pic.begin(9600);
}
void loop()
{
pic.listen();
Serial.println(get_counter());
delay(1000);
}
String get_counter()
{
pic.println("R");//To seend an order to pic give me back the counter number It will riply with 6 bytes
while(pic.available()>0){
for (int i = 0;i<=5;i++){
Serial.print(i);
Serial.print("=");
counter[i] = pic.read();
Serial.print(counter[i]);
}
counter[6] = '\0'; //Terminate the String element on the array
return counter;
}
}
If you are not using IDE 1.0 (which I believe now has buffering) then all that printing to Serial will kill the receiving of characters because Serial.print will block.
Upping the debug printing to 115200bps might help. But as you are getting a fixed 6 chars why not just do something like
void get_counter()
{
pic.println("R");
while(pic.available() < 6) {};
for (int i = 0; i < 6;){
counter[i++] = pic.read(); // save char in array and move i to next position
counter[i] = '\0'; // null terminate after the char
}
Serial.println (counter);
}
hello Rob
In fact your code is more efective.I will try it just now but I'm trying to understand what will happen on your for loop
counter[i++] = pic.read();
counter = '\0'; [/quote] Doing this does the array position will not get overwritten by the last intrution pic.read?? Any way I will try it on the arduino I'm just asking because I had never seen a for loop like that incrementing i that way thanks for your fast answer I will post the result later
hello again
Your code works very well.Now the output is fine, I also discover that the problem was not about the array it self or with the string.The main problem was pic.println("R");
I discover this just now, the println was affecting the way the pic respondes.Now I change it to just pic.print("R") and the code works fine.Finally I can read the counter from pic
Many thanks for all
You really help me solving this ichue, that was bugging me for about 2 day, all because a println :0