Pages: 1 [2]   Go Down
Author Topic: Multiple Serial Communication On Arduino UNO  (Read 1147 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 6
Posts: 95
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What's about myserial1 "myserial1.begin(38400);"?
Logged

Offline Offline
Jr. Member
**
Karma: 6
Posts: 95
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

... and read an example here: http://arduino.cc/en/Reference/SoftwareSerialListen, You are trying to use same pins for  myserial and myserial1 and I'm afraid it is not possible.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Robin2 for replying. 

@Robin2  It's suppose to have a switch so that I can open the data log manually and off it manually by using a button. But that's abit too advance me for now.. So right now I just want to make it in a way that after 100 counts , the current serial device will be switch off and another serial device will be switch on.  I need the Serial.available() as my data log will only be activate if I type letter "I" with a carriage return on the serial monitor input bar. So for now I only can get the first one working ,so I'm not sure if I'm typing the code wrongly. As what I know YourSerial.listen() is a very strong and useful function that disable all the other serial device and listen to this serial device you activated.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Budvar10 for replying .

@Budvar10 I define the rx and tx pin seperately. The 2 serial device uses the same baud rate. I referred to that reference when I was making progress and my code is mostly based on that example as a structure.  As you can see down there they used the same baud rate for 2 serial device and different pins which I did the same for myserial is pin 2(rx) and 3(tx) while myserial1 is pin 4(rx) and 5(tx). I'm not sure if what my claimed is right... but if it's wrong could you enlighten me. Thanks smiley-grin I'm still new to programming so pardon me smiley-sad
Logged

UK
Offline Offline
Faraday Member
**
Karma: 101
Posts: 6244
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Responding to Reply #17

I can't spend time on it now so I will try to look at it again later today.

...R
Logged

UK
Offline Offline
Faraday Member
**
Karma: 101
Posts: 6244
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have now had a go at reorganizing the code into some sort of logical chunks. There seemed to be related pieces scattered all over the place. I may not have got everything in the right place. And I suspect further simplifications could be made.

It is not complete - for example I have introduced new variables without bothering to define them. Another mechanism is needed to replace a++ as a means of switching from myserial to myserial1.

Meanignful names for things would make the code much easier to follow compared with serial and serial1, for example.

I have dropped the various delays because I didn't know what they were for. It may be necessary to put them in, or it may be better to replace them with the technique in the Blink Without Delay example sketch.

My intention is to sharpen the focus on the different activities so that the interconnecting logic can be considered clearly without getting bogged down in the details.

I originally planned to mark all the changes but it would have been too much trouble.

I deleted all the comments just because they got in my way as most of them took up a whole line on the screen.

Code:
#include <SoftwareSerial.h>  
#define rxpin 2  
#define txpin 3

int ledPin = 13;

SoftwareSerial myserial(rxpin, txpin);
SoftwareSerial myserial1(rxpin, txpin);

char inputstring[30];          // changed
char sensorstring[30];         // changed
boolean input_stringcomplete = false;
boolean sensor_stringcomplete = false;
int a = 0;
byte indx = 0;

void setup(){  
  Serial.begin(38400);  
  myserial.begin(38400);

  pinMode(ledPin,OUTPUT);
}


//=====================

void loop(){
  readFromUSB();
  sendViaUsb();
  readSensor();
  readSensor1();
  
  // a++; // this isn't suitable as it may leave some data part read when it changes from 99 to 101

}

//=====================

void readFromUSB() {

  if (Serial.available > 0) {
      char inchar = (char)Serial.read();
      inputstring[indx] = inchar;
      indx += 1;
      if (indx > maxBufSize) {
         indx = maxBufSize;
      }    
      if(inchar == '\r') {
        input_stringcomplete = true;
        indx = 0;
      }
  }

}

//=====================

void sendViaUSB() {
 if (sensor_stringcomplete){
    Serial.print(sensorstring);
    sensorstring = "";
    sensor_stringcomplete = false;
  }
}

//=====================

void readSensor() {

  if (a > 100) {
    return;
  }
  
  if (input_stringcomplete){  
    myserial.print(inputstring);
    inputstring = "";
    input_stringcomplete = false;
  }
  
  // is some waiting time necessary ?
  
  myserial.listen();
  sensor_stringcomplete = false;
  if (myserial.available()) {
      char inchar = (char)myserial.read();
      sensorstring[sensIndx] = inchar;
      sensIndx += 1;
      if (sensIndx > maxSensBufSize) {
         sensIndx = maxSensBufSize;
      }    
      if (inchar == '\r') {
        sensor_stringcomplete = true;
        sensorstring[sensIndx] = 0;
      }
   }
}

//=====================

void readSensor1() {

  if (a < 100) {
    return;
  }
  
  myserial1.listen();
  sensor_stringcomplete = false;
  if (myserial1.available()) {
      char inchar = (char)myserial1.read();
      sensorstring[sens1Indx] = inchar;
      sens1Indx += 1;
      if (sens1Indx > maxSens1BufSize) {
         sens1Indx = maxSens1BufSize;
      }    
      if (inchar == '\r') {
        sensor_stringcomplete = true;
        sensorstring[sensIndx] = 0;
      }
   }
}

//=====================

...R
« Last Edit: July 11, 2014, 01:37:17 pm by Robin2 » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Robin2, I would try and study your code and give it a shot today ^^ Sorry was taking a short break this last few day as I can't digest all the coding information that I've learned so fast
« Last Edit: July 13, 2014, 08:41:50 pm by luckystar94 » Logged

UK
Offline Offline
Faraday Member
**
Karma: 101
Posts: 6244
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

was taking a short break this last few day as I can't digest all the coding information that I've learned so fast

Very wise.

...R
Logged

Pages: 1 [2]   Go Up
Jump to: