serial read problem

Hello,

Like a few others I saw on the forum, I also have a problem with reading a bit stream from the serial port on the arduino.
My purpose is that an arduino master is sending data to an arduino slave (reading data) and the communication between it is with RS485 because the distance in between the master and slave can be a couple of meters.
I would like to test the functionality of reading from the RX pin from the arduino slave with the example code that arduino provides :

int incomingByte = 0;	// for incoming serial data

void setup() {
	Serial.begin(9600);	// opens serial port, sets data rate to 9600 bps
}

void loop() {

	// send data only when you receive data:
	if (Serial.available() > 0) {
		// read the incoming byte:
		incomingByte = Serial.read();

		// say what you got:
		Serial.print("I received: ");
		Serial.println(incomingByte, BIN);
	}
}

The data that is being send is a repitetive bytestream (with a duration of 1,6 sec and a pause of 1 sec, voltages are measured with a scope : 0V<=>5V). The purpose of the send data is adressing 60 arduino slaves and the bytestream is send trough pin D12. Before you send the data the RS485 transceiver is set in transmit mode. The code is displayed below:

int transmit_data = 12; // pin D12
int TX_RX = 13; // pin D13 
void setup() {
  pinMode(transmit_data, OUTPUT); pinMode (TX_RX, OUTPUT);
  Serial.begin(9600); 
}

void loop(){ 
   digitalWrite(TX_RX, LOW); delay (250); digitalWrite(TX_RX, HIGH); // TX data = 1, RX data = 0
  
      
   // Slave 1 : 0 00 000000 1
   for (int i = 0; i < 9; i++){digitalWrite(transmit_data,LOW); delay(1);}; digitalWrite(transmit_data,HIGH); delay(1); Serial.println("Slave 1  : 0 00 000000 1");
       
   // Slave 2 : 0 00 000001 1
   for (int i = 0; i < 8; i++){digitalWrite(transmit_data,LOW); delay(1);}; for (int i = 0; i < 2; i++){digitalWrite(transmit_data,HIGH); delay(1);}; Serial.println("Slave 2  : 0 00 000001 1");
      
   // Slave 3 : 0 00 000010 1
   for (int i = 0; i < 7; i++){digitalWrite(transmit_data,LOW); delay(1);}; digitalWrite(transmit_data,HIGH); delay(1); digitalWrite(transmit_data,LOW); delay(1); 
   digitalWrite(transmit_data,HIGH); delay(1); Serial.println("Slave 3  : 0 00 000010 1");
      
   // Slave 4 : 0 00 000011 1
   for (int i = 0; i < 7; i++){digitalWrite(transmit_data,LOW); delay(1);}; for (int i = 0; i < 3; i++){digitalWrite(transmit_data,HIGH); delay(1);}; Serial.println("Slave 4  : 0 00 000011 1");

   // Slave 5 : 0 00 000100 1
   for (int i = 0; i < 6; i++){digitalWrite(transmit_data,LOW); delay(1);}; digitalWrite(transmit_data,HIGH); delay(1); for (int i = 0; i < 2; i++){digitalWrite(transmit_data,LOW); 
   delay(1);}; digitalWrite(transmit_data,HIGH); delay(1); Serial.println("Slave 5  : 0 00 000100 1");
 
   // ...
      
   // Slave 55 : 0 00 110110 1
   for (int i = 0; i < 3; i++){digitalWrite(transmit_data,LOW); delay(1);}; digitalWrite(transmit_data,HIGH); delay(1); for (int i = 0; i < 3; i++){digitalWrite(transmit_data,HIGH); delay(1); 
   digitalWrite(transmit_data,LOW); delay(1); digitalWrite(transmit_data,HIGH); delay(1);}; Serial.println("Slave 55 : 0 00 110110 1");
      
   // Slave 56 : 0 00 110111 1
   for (int i = 0; i < 3; i++){digitalWrite(transmit_data,LOW); delay(1);}; for (int i = 0; i < 2; i++){digitalWrite(transmit_data,HIGH); delay(1);}; digitalWrite(transmit_data,LOW); delay(1);
   for (int i = 0; i < 4; i++){digitalWrite(transmit_data,HIGH); delay(1);}; Serial.println("Slave 56 : 0 00 110111 1");
      
   // Slave 57 : 0 00 111000 1
   for (int i = 0; i < 3; i++){digitalWrite(transmit_data,LOW); delay(1);}; for (int i = 0; i < 3; i++){digitalWrite(transmit_data,HIGH); delay(1);}; for (int i = 0; i < 3; i++)
   {digitalWrite(transmit_data,LOW); delay(1);}; digitalWrite(transmit_data,HIGH); delay(1); Serial.println("Slave 57 : 0 00 111000 1");
      
   // Slave 58 : 0 00 111001 1
   for (int i = 0; i < 3; i++){digitalWrite(transmit_data,LOW); delay(1);}; for (int i = 0; i < 3; i++){digitalWrite(transmit_data,HIGH); delay(1);}; for (int i = 0; i < 2; i++)
   {digitalWrite(transmit_data,LOW); delay(1);}; for (int i = 0; i < 2; i++){digitalWrite(transmit_data,HIGH); delay(1);}; Serial.println("Slave 58 : 0 00 111001 1");
      
   // Slave 59 : 0 00 111010 1
   for (int i = 0; i < 3; i++){digitalWrite(transmit_data,LOW); delay(1);}; for (int i = 0; i < 3; i++){digitalWrite(transmit_data,HIGH); delay(1);}; for (int i = 0; i < 2; i++)
   {digitalWrite(transmit_data,LOW); delay(1); digitalWrite(transmit_data,HIGH); delay(1);}; Serial.println("Slave 59 : 0 00 111010 1");
      
   // Slave 60 : 0 00 111011 1
   for (int i = 0; i < 3; i++){digitalWrite(transmit_data,LOW); delay(1);}; for (int i = 0; i < 3; i++){digitalWrite(transmit_data,HIGH); delay(1);}; digitalWrite(transmit_data,LOW); 
   delay(1); for (int i = 0; i < 3; i++){digitalWrite(transmit_data,HIGH); delay(1);}; Serial.print("Slave 60 : 0 00 111011 1");  
      
   digitalWrite(TX_RX, LOW); delay (750);   
}

But this code is not the problem because the bytestream that is send is correct out of the arduino and at the beginning of the master RS485 transceiver, and also at the end of the slave RS485 transceiver and RX pin on the arduino slave.

So the problem is that the data that is placed on the RXpin of the arduino slave cant be read by the controller and cant be shown on the screen with the example code. That so weird because examples allways are supposed to work. I also used an different arduino board so its not that the serial port is not working or such. And I dont think theres a problem with the datastream Ive produced....

Thanks for your help!

Maybe problems with the serial library that is used (standard one)? Or maybe using serial.peak in stead of serial.available?? I really have no other idea's...

I really have no other idea's...

I don't either, and that code is impossible to read. There are conventions, like one statement per line, for a reason. Follow the conventions if you expect others to help you debug your code.

Below folows a 'diagram' of the hardware modules with their wiring :


Arduino Master
DATA pin D12 | control RS485 pin D13
| |


| |
V V
| |


RS485 sender (& receiver) IC
DATA


|
V
|


Arduino Slave
Serial date receive RX pin D0


And it's the purpose of reading the data from the RX pin on the arduino slave and showing that received data on the serial monitor screen ...

And with the RS485 transceiver IC's is nothing wrong : the data that is send out of pin D12 (master) is exactly the same as on the receiving pin RX (D0, Slave), measured with a oscilloscope. But to be perfectly knowing that's not the problem, I've overwired the RS485 IC's and made a direct connection between master (pin D12) and slave (pin D0) and its the same. I've even measured the same correct signal on the controller ATMEGA328 pin 2 so the controller is definitely getting the right signal inside!
Maybe there is indeed something wrong with the timing when reading data from the softwarebuffer on the slave. So I might think it's a software issue with the serial reading on the slave...

Maybe I'm not allowed to send an external datastream on the RX pin because that is also connected with the RX of the USB communication (everytime I adjust the code and upload it to the arduino, I`m forced to disconect the wire with the datastream from that pin because otherwise the new software can't be uploaded completely and then afterwards reconnecting the wire back on). The only other serial data port on the arduino's microcontroller ATMEGA328 is analog pin A5 : SDA. So maybe I could send the data trough that pin and read it with Serial.read() ?? Or other sugestions maybe??

// Slave 58 : 0 00 111001 1
   for (int i = 0; i < 3; i++){digitalWrite(transmit_data,LOW); delay(1);}; for (int i = 0; i < 3; i++){digitalWrite(transmit_data,HIGH); delay(1);}; for (int i = 0; i < 2; i++)
   {digitalWrite(transmit_data,LOW); delay(1);}; for (int i = 0; i < 2; i++){digitalWrite(transmit_data,HIGH); delay(1);}; Serial.println("Slave 58 : 0 00 111001 1");

Have you been introduced to the concept of the functions with parameters?
It would make code a whole lot:

  1. shorter
  2. easier to read
  3. easier to debug
  4. easier to maintain.
int incomingByte = 0;	// for incoming serial data

void setup() {
	Serial.begin(9600);	// opens serial port, sets data rate to 9600 bps
}

void loop() {
	// send data only when you receive data:
	if (Serial.available() > 0) {
		// read the incoming byte:
		incomingByte = Serial.read();

		// say what you got:
		Serial.print("I received: ");
		Serial.println(incomingByte, BIN);
	}
}

If Serial.available() > 0 then read one byte from Serial and... print a debug statement on Serial ? Or maybe I don't understand your code...
HTH

didier_vandeputte:
Maybe I'm not allowed to send an external datastream on the RX pin because that is also connected with the RX of the USB communication (everytime I adjust the code and upload it to the arduino, I`m forced to disconect the wire with the datastream from that pin because otherwise the new software can't be uploaded completely and then afterwards reconnecting the wire back on). The only other serial data port on the arduino's microcontroller ATMEGA328 is analog pin A5 : SDA. So maybe I could send the data trough that pin and read it with Serial.read() ?? Or other sugestions maybe??

Arduino UNO (ATmega328P) uses Arduino pins 0 & 1 (chip pins 2 & 3) for hardware serial. I see you using Arduino 12 & 13.

You maybe should look at the Software Serial Library and use that.
http://arduino.cc/en/Tutorial/SoftwareSerialExample

mromani:

int incomingByte = 0;	// for incoming serial data

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

	// say what you got:
	Serial.print("I received: ");
	Serial.println(incomingByte, BIN);
}

}




If Serial.available() > 0 then read one byte from Serial and... print a debug statement on **Serial** ? Or maybe I don't understand your code...
HTH

It's a public arduino example code thats on the site so I didn't write it so to be sure that this code would work. It's reading available bits in the serial buffer and then showing them on the screen serial monitor. And the problem is that my datastream that my master arduino produced, isn't shown on the screen and constantly showing '0' in stead. But I think that the arduino is constantly reading the datastream between the USB and the controller in stead of my externally placed data upon it. It's seems that one has priorty above the other. Maybe with 'softwareserial' I can read the input stream on an other digital pin... Hopefully that works on the arduino uno boards (with ATmega328 microcontroller)...

GoForSmoke:

didier_vandeputte:
Maybe I'm not allowed to send an external datastream on the RX pin because that is also connected with the RX of the USB communication (everytime I adjust the code and upload it to the arduino, I`m forced to disconect the wire with the datastream from that pin because otherwise the new software can't be uploaded completely and then afterwards reconnecting the wire back on). The only other serial data port on the arduino's microcontroller ATMEGA328 is analog pin A5 : SDA. So maybe I could send the data trough that pin and read it with Serial.read() ?? Or other sugestions maybe??

Arduino UNO (ATmega328P) uses Arduino pins 0 & 1 (chip pins 2 & 3) for hardware serial. I see you using Arduino 12 & 13.

You maybe should look at the Software Serial Library and use that.
http://arduino.cc/en/Tutorial/SoftwareSerialExample

Yes If you look at the wiring diagram above, master arduino sends data out with pin 12 and the slave arduino (supposed to) read, receives it with pin 0.
And thanks for the tip with 'softwareserial'. I'll try that :slight_smile:

And thanks for the tip with 'softwareserial'. I'll try that :slight_smile:

SoftwareSerial is your only option if you want the arduino to receive serial data from another board and at the same time print information on the serial monitor on the pc (unless you use a mega, of course, which has 4 hardware serial ports...)

There is the hardware supported high speed Serial Peripheral Interface that uses UNO pins 11, 12 and 13. As Mega to Mega communication it should beat the pants off the regular serial connect. SPI is serial.

Ooops, you're right, of course... :roll_eyes:

GoForSmoke:
There is the hardware supported high speed Serial Peripheral Interface that uses UNO pins 11, 12 and 13. As Mega to Mega communication it should beat the pants off the regular serial connect. SPI is serial.

So in stead of using the 4 wired SPI protocol with its library, I could use on the Arduino Uno only p.11= MOSI (using for SENDING data from the master to slave) and p.12= MISO (using for RECEIVING data from the master to slave)?

You could use those pins for software serial using the software serial library and be slow (less than hardware serial) or you could use the SPI library and 4 wire and be fast (mega bps). That's your call. SPI hardware support doesn't let you run without SCK (serial clock) and select.

Later then I found out what the solution was that I couldn't read serial data: I had to sent ASCII data from the masterTXpin wich is also asynchronous, and then I could receive that data on the slave RXpin. So the key was sending data wich has a readable ASCII representative. But still thx for all the hints and tips dudes! :slight_smile: