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
#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();
}
If you constructor isn't going to do anything you can omit it.
//________________________________________________
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:
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.
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.
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..
"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?
Hi Nick .
Thanks for replying my post
and I'm sorry for my post earlier..
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
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 (Gammon Forum : Electronics : Microprocessors : RS485 communications) not using SoftwareSerial, Arduino IDE can compile it properly.. nothing wrong..
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
I'm sorry that i'm still adapting to arduino, I've many things to learn, structure of data, syntax and so on..
I want to ask about how send array data like RFID tags..
The data comes like
2 52 66 48 48 65 57 55 70 57 51 48 69 13 10 3
i'm aware that there are header and stop bytes in order to calculate the checksum..what i need to do is send the value that i got to other arduino.. (I'm using Nick library ofcourse)
i want to add some "indicator" of the packet data (like header to whom i sent the data, if not match to other machine header, they will flush it)
What i'm supposed to do first? convert the entire array to string?
Nick already show the example using array of byte [1,2,level_of_potentiometer]
but i still have no idea..
If i get the message from serial into array char
char i = Serial.read();
.................
tag[bytesread] = i;
and then convert it to string String packet = tag , why i got additional junk packet?
original packet via serial monitor
4B00A97F930E
and the printed value from packet in serial monitor
I thought i can send directly my String variable using your non-blocking library..
I'm sorry that i dont understand arduino enough
How do i convert the packet to match with format from your library Nick
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"
test.ino: In function 'void SendingData()':
test:88: error: no matching function for call to 'RS485::sendMsg(String&, unsigned int)'
----\Arduino\libraries\RS485_non_blocking/RS485_non_blocking.h:87: note: candidates are: void RS485::sendMsg(const byte*, byte)
test:94: error: initializer fails to determine size of '__c'
test:95: error: expected `)' before ';' token
No you can't send String types directly because they involve memory allocation locally. You could try converting them to a string. I personally wouldn't use String.
Thank you for your reply..
Thats great ! thank you
Whoa i dont know there's a way to do that..
I'll find refference about that, i also not understand when saw some example code of declaring variable like
Thank you for your reply..
Thats great ! thank you
Whoa i dont know there's a way to do that..
I'll find refference about that, i also not understand when saw some example code of declaring variable like
char *somevariable [][] ;
what's the differences with
char somevariable[][];
Thank you very much !
The first is a 2D array of pointers to char, while the second is a 2D array of char.