I am trying to communicate to a linear scale over an RS-485 interface. I am hoping to receive data back informing me of the position of the linear scale.
Here are the procedures simplified:
Send command (0x00) from Arduino Mega to linear scale to request position
It will be over 19200 baud
Within 15ms, the linear scale will respond with data package of 6 bytes
1 byte echoing command (0x00) and followed by 4 byte (position) and last a CRC byte.
This process will be set on a loop every 50ms
I tried similar set up suggested by [u]this example[/u] . I connected the data output wire and data dash wire to A & B in the RS-485 module respectively.
The baud rate was set to 19200, and I initiated the command (0x00) from Arduino Mega to linear scale etc.
Yet, when I print the value obtained to the serial monitor, I only obtain -1 or ÿ (I think they both represent that there is no data?).
Here is the code I used:
/*-----( Import needed libraries )-----*/
#include <SoftwareSerial.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define SSerialRX 10 //Serial Receive pin
#define SSerialTX 11 //Serial Transmit pin
#define SSerialTxControl 3 //RS485 Direction control
#define RS485Transmit HIGH
#define RS485Receive LOW
#define Pin13LED 13
/*-----( Declare objects )-----*/
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
/*-----( Declare Variables )-----*/
int byteReceived;
int byteSend;
int reqn = 0x00;
void setup() /****** SETUP: RUNS ONCE ******/
{
// Start the built-in serial port, probably to Serial Monitor
Serial.begin(19200);
//Serial.println("YourDuino.com SoftwareSerial remote loop example");
//Serial.println("Use Serial Monitor, type in upper window, ENTER");
pinMode(Pin13LED, OUTPUT);
pinMode(SSerialTxControl, OUTPUT);
digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver
// Start the software serial port, to another device
RS485Serial.begin(19200); // set the data rate (I have tried both 19200 and 4800 here)
}//--(end setup )---
void loop(){ /****** LOOP: RUNS CONSTANTLY ******/
// digitalWrite(Pin13LED, HIGH); // Show activity
(Serial.available());
{
byteReceived = Serial.read();
digitalWrite(SSerialTxControl, RS485Transmit); // Enable RS485 Transmit
RS485Serial.write(reqn); // Send byte to Remote Arduino
// digitalWrite(Pin13LED, LOW); // Show activity
delay(10);
digitalWrite(SSerialTxControl, RS485Receive); // Disable RS485 Transmit
}
(RS485Serial.available()); //Look for data from other Arduino
{
// digitalWrite(Pin13LED, HIGH); // Show activity
byteReceived = RS485Serial.read(); // Read received byte
Serial.write(byteReceived); // Show on Serial Monitor
// Serial.println(byteReceived);
delay(50);
// digitalWrite(Pin13LED, LOW); // Show activity
//Serial.println(HIGH);
//Serial.println(LOW);
}
delay(1000);
}//--(end main loop )---
Could you please point me to where I am going wrong.
Thank you.
RS485 (half duplex) is a three wire system A, B and Ground. The link you posted shows a classical example of how not to implement the physical interface. Such a system works because there is a common ground shared by the arduinjos being connected to a common power source.
Do you have a ground connection to the scale?
The communication parameters must match the scale, is the scale setup up for 8 bit, no parity or (as with lots of RS485 devices) is it setup for 9 bit (used for addressing) or maybe 7 bit with parity etc.
Your code does not match the sample you have given, it will hang waiting on input from the serial port (no the RS485 port).
Do you have a USB to rs485 converter? If so connect you scale directly to a PC and test it that way first.
Yes the scale has a ground connection and it is connected to the GND of the Arduino. The 5V power supply to the scale is also provided by the Arduino.
The scale is set up for "8 bit Non Parity Stop bit 1bit".
I have now attached the specification sheet for the linear scale with my original post.
asmallri:
Your code does not match the sample you have given, it will hang waiting on input from the serial port (no the RS485 port).
Are you referring to this part:
(RS485Serial.available()); //Look for data from other Arduino
Would I be able to read the input straight from the RS485?
I unfortunately do not have a RS485 USB converter. I have access to an oscilloscope, which I could use to verify if there is any response signal.
PaulS:
What do you think that bit of code is doing? Nothing useful is for sure.
Thanks for the reply Paul.
I tried Serial.read instead as well and there does not seem to be any response.
I will not have access to an oscillascope for few days, so is there anything else you recommend I can do in the meanwhile?
so is there anything else you recommend I can do in the meanwhile?
Yes, you can explain why you typed those two bits of code. It looks you took away part of an if statements, and just banged a ; on the end to make the compiler happy.
PaulS:
Yes, you can explain why you typed those two bits of code. It looks you took away part of an if statements, and just banged a ; on the end to make the compiler happy.
And that is why the code will then hang on the next line because it is waiting for non existing input
You have crappy code, too? You can't expect anyone to help you fix it if you don't post it here, AND deal with the comments about it being crappy code.