serial read problem

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

serial read problem

Postby didier_vandeputte » Wed Feb 01, 2012 6:19 am

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 :
Code: Select all
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:

Code: Select all
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 can`t be read by the controller and can`t 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 it`s not that the serial port is not working or such. And I don`t think there`s a problem with the datastream I`ve produced....

Thanks for your help!
didier_vandeputte
 
Posts: 9
Joined: Wed Feb 01, 2012 6:14 am

Re: serial read problem

Postby didier_vandeputte » Wed Feb 01, 2012 6:26 am

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...
didier_vandeputte
 
Posts: 9
Joined: Wed Feb 01, 2012 6:14 am

Re: serial read problem

Postby adafruit_support_bill » Wed Feb 01, 2012 6:40 am

Not sure I understand entirely. Maybe a diagram of your system would be helpful.

But it seems you are trying to have the Arduino hardware serial port connected to both your Arduino Master and the IDE/Serial monitor at the same time.
User avatar
adafruit_support_bill
 
Posts: 16644
Joined: Sat Feb 07, 2009 9:11 am

Re: serial read problem

Postby didier_vandeputte » Wed Feb 01, 2012 7:26 am

adafruit_support wrote:Not sure I understand entirely. Maybe a diagram of your system would be helpful.

But it seems you are trying to have the Arduino hardware serial port connected to both your Arduino Master and the IDE/Serial monitor at the same time.


Thx for your reply. 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 ...
didier_vandeputte
 
Posts: 9
Joined: Wed Feb 01, 2012 6:14 am

Re: serial read problem

Postby adafruit_support_bill » Wed Feb 01, 2012 7:41 am

It could be a number of things - including timing. Do you see any data at all coming in?

It might help to reduce the system to the simplest possible configuration to narrow down the problem. Maybe try without the RS-485 transceivers in the circuit.
User avatar
adafruit_support_bill
 
Posts: 16644
Joined: Sat Feb 07, 2009 9:11 am

Re: serial read problem

Postby didier_vandeputte » Wed Feb 01, 2012 7:58 am

adafruit_support wrote:It could be a number of things - including timing. Do you see any data at all coming in?

It might help to reduce the system to the simplest possible configuration to narrow down the problem. Maybe try without the RS-485 transceivers in the circuit.


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...
Last edited by didier_vandeputte on Wed Feb 01, 2012 8:19 am, edited 1 time in total.
didier_vandeputte
 
Posts: 9
Joined: Wed Feb 01, 2012 6:14 am

Re: serial read problem

Postby adafruit_support_bill » Wed Feb 01, 2012 8:19 am

So I might think it's a software issue with the serial reading on the slave...

Serial.read() is used by many applications. I think that if there were a problem with the standard Arduino Serial.read(), more people would be reporting it.
User avatar
adafruit_support_bill
 
Posts: 16644
Joined: Sat Feb 07, 2009 9:11 am

Re: serial read problem

Postby didier_vandeputte » Wed Feb 01, 2012 8:54 am

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??
didier_vandeputte
 
Posts: 9
Joined: Wed Feb 01, 2012 6:14 am

Re: serial read problem

Postby adafruit_support_bill » Wed Feb 01, 2012 9:41 am

You could try SoftwareSerial. That lets you use any pair of pins for serial communication.
If you are using version 1.0 of the IDE it is built in. For earlier version you would need to download the NewSoftSerial library.
User avatar
adafruit_support_bill
 
Posts: 16644
Joined: Sat Feb 07, 2009 9:11 am

Re: serial read problem

Postby didier_vandeputte » Thu Feb 02, 2012 5:21 am

adafruit_support wrote:You could try SoftwareSerial. That lets you use any pair of pins for serial communication.
If you are using version 1.0 of the IDE it is built in. For earlier version you would need to download the NewSoftSerial library.

Thanks for the tip with 'softwareserial'. I'll try that :-)
didier_vandeputte
 
Posts: 9
Joined: Wed Feb 01, 2012 6:14 am

Re: serial read problem

Postby didier_vandeputte » Thu Feb 02, 2012 9:53 am

It suddenly hits me I could use SPI also as the serial interface. 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)?
Maybe that would work or are there second thoughts on that? Hopefully thats a second good solution to the problem...
didier_vandeputte
 
Posts: 9
Joined: Wed Feb 01, 2012 6:14 am

Re: serial read problem

Postby adafruit_support_bill » Thu Feb 02, 2012 10:48 am

It's worth a try. Let us know how it works out.
User avatar
adafruit_support_bill
 
Posts: 16644
Joined: Sat Feb 07, 2009 9:11 am

Re: serial read problem

Postby didier_vandeputte » Mon Mar 19, 2012 3:38 am

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! :-)
didier_vandeputte
 
Posts: 9
Joined: Wed Feb 01, 2012 6:14 am


Return to Arduino

Who is online

Users browsing this forum: No registered users and 10 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [113]

Raspberry Pi[82]
 
FLORA[24]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[12]
Arduino[60]
 
NETduino[14]
 
BeagleBone[23]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[39]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[9]


 
Breakout Boards[35]
LCDs & Displays[49]
Components & Parts[70]
Batteries & Power[54]
EL Wire/Tape/Panel[52]
LEDs[112]
 
Wireless[16]
Cables[66]
 
Lasers[6]
Sensors/Parts[147]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[70]
 
iDevices[13]
Tools[71]
 
Wearables[41]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[25]


 
Stickers[41]
 
Skill badges[55]
 
Books[26]
 
Circuit Playground[7]
 
Gift Certificates[4]