Go Down

Topic: what I'm missing here (Read 878 times) previous topic - next topic

HugoPT

Hello folks

I'm trying to build a RS485 library and I'm testint it now.I'm using Visual Basic to inject some packets on the RS485 BUS and debug the outuput using a Saleae Logic Analiser.
I only have one function on the main loop for now but I should expect to see the answer from the microcontroller when is ID is called on the BUS right after he received the message and then answer back.It works most of the time but looking on the Saleae Software sometimes he ignores some stream packects and don't give the answer.For now I just have one slave on the BUS.
Here is the output from the software.
Channel 0 is connect to the RX atmega328 pin and the channel 1 is connect to TX Pin.Channel 3 is connect to the Pin 2 and 3 from the MAX485(when is low is on receive mode, when is High on seend Mode)
I donĀ“t figure out why is a gap in some answer as I can see on the image
Also here is my code and the library in working on
Code: [Select]
#include "RS485.h"

RS485 bus;
void setup()
{
Serial.begin(9600);
bus.begin(2,3); //Device address and hardware pin connected to MAX485 enable Pins 2 and 3
bus.rxMode();      //Set de Bus on receive mode
}
void loop()
{
bus.listen();
}
Debian,Mint,Ubuntu
Arduino Mega 2560
Arduino Nano
Arduino Duemilanove
MAC OS Montain Lion
Raspberry PI Model B

Nick Gammon

Code: [Select]

RS485::RS485(){}


If you constructor isn't going to do anything you can omit it.




Code: [Select]

//________________________________________________
void RS485::listen()
{
 
  if(Serial.read() ==  startByte)//Probe if first byte is the start Byte
  {
    //Serial.println("Recebido StartByte");
    while(Serial.available()<=6);//Waits for the remaining 7 bytes
    if(Serial.read() == slaveID)//Check the Slave ID
    {
     dataReceived[0] = startByte;
     dataReceived[1] = slaveID;
     for (uint8_t i = 0; i<= 5; i++) dataReceived[i+2] = Serial.read();
     byte tempCRC = dataReceived[7];
     dataReceived[7] = tempCRC;
     if(verify_checkSum(tempCRC,dataReceived))functionTable(dataReceived);//Go to the Function Table
    }
   else while(Serial.read() != -1);
  }
else while(Serial.read() != -1);
}   


I don't like this for a number of reasons. If you don't get a startByte you then keep reading until you get nothing. What if the startByte is the second byte? You have now skipped it. What if two packets come in quick succession, but there is noise in the very first byte? You have now skipped two packets. Also I prefer to test for if Serial.available rather than checking if you get -1 back.

I recently did a non-blocking RS485 library here:

http://www.gammon.com.au/forum/?id=11428

In that I use a "state machine" where you transition from "no packet" to "filling the packet" to "calculate the CRC". That code will work even if the bytes come at odd intervals.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

#2
Dec 30, 2012, 03:51 am Last Edit: Dec 30, 2012, 08:12 am by Protoneer Reason: 1
Hi Nick,

I found the above page you wrote about a week ago and its one of the best descriptions of implementing RS485 that I could find online.
Just want to thank you for the effort you have put into it, It is inspirational.
Delta 3D Printer with Auto Bed Leveling - http://www.kickstarter.com/projects/ttstam/openbeam-kossel-pro-a-new-type-of-3d-printer/posts
http://blog.Protoneer.co.nz

Nick Gammon

Glad you like it, and thanks for the complimentary remarks. :)
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

orthho

#4
Jan 22, 2015, 02:34 am Last Edit: Jan 22, 2015, 06:42 am by orthho
Hallo sir..
I'm sorry to disturb you and sorry if i'm not very good at English.

Since i new to arduino, and i have i project using RS-485 with 1 master and 2 slave..
I've already succeed communicate between two arduino with rs485..i just have a problem when i've to send structure message, so i try to using your library using RS485 non blocking sir..
but confused when  i encountered compile error..
i' ve trying to make chance the code to see whats wrong in the code..but i dont have any idea.. :smiley-sad-blue:

this is my code
Code: [Select]


#include <SoftwareSerial.h>
#include <RS485_non_blocking.h>

#define SerialControl     7
#define Transmit          HIGH
#define Receive           LOW
#define RX45              2
#define TX45              3

#define RXID              8
#define TXID              9

#define LED              13

SoftwareSerial RS485 (RX45, TX45);
SoftwareSerial RFID (RXID, TXID);


size_t fWrite (const byte what)
{
  return RS485.write (what);
}

int fAvailable ()
{
  return RS485.available();
}

int fRead ()
{
  return RS485.read();
}

RS485 myChannel (fRead, fAvailable, fWrite,20);

void setup()
{
  RS485.begin(9600);
  pinMode (SerialControl, OUTPUT);
  pinMode (LED, OUTPUT);
 
}
const byte msg [] = "Hello world";

void loop()
{
  RS485.sendMsg (msg,sizeof(msg));
  delay(1000);
}



  This report would have more information with
Code: [Select]

  "Show verbose output during compilation"
  enabled in File > Preferences.
Arduino: 1.0.6 (Windows 7), Board: "Arduino Uno"
sketch_jan22a:34: error: 'RS485' does not name a type
sketch_jan22a.ino: In function 'void loop()':
sketch_jan22a:47: error: 'class SoftwareSerial' has no member named 'sendMsg'



Could you give me idea why this happened?
could you give me an example?

thank you very much for your time..
God bless you

Nick Gammon

Please edit your post, select the code, and put it between [code ... [/code] tags.

You can do that by hitting the "Code" button above the posting area (It looks like a scroll with < > inside it).

It looks to me like the library is not installed properly or at all.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

orthho

#6
Jan 22, 2015, 06:49 am Last Edit: Jan 22, 2015, 07:25 am by orthho
Hi Nick .
Thanks for replying my post  :)
and I'm sorry for my post earlier..

Code: [Select]

sketch_jan22a:47: error: 'class SoftwareSerial' has no member named 'sendMsg'


i thought that something not suitable between library rs485 non blocking and SoftwareSerial..

Can you post some sketch RS485 non blocking and SoftwareSerial? :(
I'm confuse about this part

Code: [Select]

RS485 myChannel (fRead, fAvailable, fWrite,20);


Is it because the my library didnt installed correctly?
I've reinstall both of my arduino IDE and your library..but still same error appear.
But when i compile your example sketch in your web (http://www.mushclient.com/forum/?id=11428) not using SoftwareSerial, Arduino IDE can compile it properly.. nothing wrong..

I do make changes to my sketch, still error
Code: [Select]
#include <RS485_non_blocking.h>
#include <SoftwareSerial.h>

#define SerialControl     7
#define Transmit          HIGH
#define Receive           LOW
#define RX45              2
#define TX45              3

#define RXID              8
#define TXID              9

#define LED              13

SoftwareSerial RS485 (RX45, TX45);
SoftwareSerial RFID (RXID, TXID);


size_t fWrite (const byte what)
{
  return RS485.write (what);
}

int fAvailable ()
{
  return RS485.available();
}

int fRead ()
{
  return RS485.read();
}

RS485 myChannel (fRead, fAvailable, fWrite,20);

void setup()
{
  RS485.begin(9600);
  pinMode (SerialControl, OUTPUT);
  pinMode (LED, OUTPUT);
 
}
const byte msg [] = "Hello world";

void loop()
{
  myChannel.sendMsg (msg,sizeof(msg));
  delay(1000);
}


error message
Code: [Select]
  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.
Arduino: 1.0.6 (Windows 7), Board: "Arduino Uno"
sketch_jan22a:34: error: 'RS485' does not name a type
sketch_jan22a.ino: In function 'void loop()':
sketch_jan22a:47: error: 'myChannel' was not declared in this scope

Nick Gammon

#7
Jan 22, 2015, 07:41 am Last Edit: Jan 22, 2015, 07:43 am by Nick Gammon
What's this?

Code: [Select]
SoftwareSerial RS485 (RX45, TX45);

...

RS485 myChannel (fRead, fAvailable, fWrite,20);


RS485 isn't a type, it is an instance of a SoftwareSerial type.

You are using the same name for two purposes. This compiles:

Code: [Select]

#include <RS485_non_blocking.h>
#include <SoftwareSerial.h>

#define SerialControl     7
#define Transmit          HIGH
#define Receive           LOW
#define RX45              2
#define TX45              3

#define RXID              8
#define TXID              9

#define LED              13

SoftwareSerial otherArduino (RX45, TX45);
SoftwareSerial RFID (RXID, TXID);


size_t fWrite (const byte what)
{
  return otherArduino.write (what);
}

int fAvailable ()
{
  return otherArduino.available();
}

int fRead ()
{
  return otherArduino.read();
}

RS485 myChannel (fRead, fAvailable, fWrite,20);

void setup()
{
  otherArduino.begin(9600);
  myChannel.begin();
  pinMode (SerialControl, OUTPUT);
  pinMode (LED, OUTPUT);
  
}
const byte msg [] = "Hello world";

void loop()
{
  myChannel.sendMsg (msg,sizeof(msg));
  delay(1000);
}
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

orthho

Hi Nick !
Thank you so much for your time.. you replay any question so fast..
I'm sorry if i bothering you.

I wanna say thank you..thank you so much!

Your Library and your tutorial work like a charm !!
thanks..
 :)  :)  :) !


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy