I have a sensor that communicates over the RS232.
If I connect the sensor directly to the pc and then I use a terminal software to send simple commands ending with , like "get all" then I get the full answer from the sensor.
I would like to do the same with Arduino MEGA in order to store the answer from "get all" and send it over a network.
I did this wiring:
RX(sensor) -> TX1
TX(sensor) -> RX1
GND(sensor) -> GND(arduino)
and then I tried to use this simple code:
int i=0;
void setup() {
// initialize both serial ports:
Serial.begin(115200);
Serial1.begin(115200);
}
void loop() {
if(i==0){
Serial1.write("get all");
Serial1.write('\r');
}
// read from port 1, send to port 0:
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}
i=1;
}
However, I get nothing as answer.
I can't understand if the error is with the carriage return or with something else.
Each command must be sent to the sensor by ending it with a carriage return.
Hi,
What type and if possible link of your sensor?
How are you connecting your sensor to the PC?
Are you using some interface module?
If yes, which model?
Post a schematic (even if done by hand) of your connection with the PC and the arduino mega.
It's the same, I can't read anything.
However, I see the TX led on arduino blinking so it receives something for sure but I cannot see it in the terminal!
could you give details of the RS232 shield you are using to communicate with the sensor?
upload a schematic of the wiring showing how you connected the Mega to the RS232 shield and the shield to the sensor
to test the RS232 shield can you do a loopback test? connect pins 2 and 3 on the 9 pin Dtype connector - character transmitted from the Mega should echo back
check the voltages on pins 2 and 3 of the 9pin Dtype connector - should be -12 volt when communications are idle
I'm not using any RS232 shield, I'm directly using the RX1 and TX1 pins available on the Arduino MEGA as explained in the first post. Why should I use a shield in this case?
Because RS-232-C is +-12V and the Mega is only expecting 0-5V. You have to match baud rate (9600), format (8N1), AND voltage (0-5V vs +-12V) for it to work.
I assumed when post #1 indicated connecting a RS232 device to a Mega a RS232 shield or equivalent circuit would be used to convert the -12 to +12 volt RS232 signals to 5V to 0v for the Mega
connecting directly stands a good chance of destroying the Mega and possibly the RS232 device
I still haven't done the test, however, I'm planning to connect TXD, RXD and GND from the adapter to Arduino MEGA RXD, TXD and GND.
I do not know if I have to swap TXD and RXD on Arduino, but I can try this in case it doesn't work with the first configuration.
Mega Serial1 test program (change Serial1 baud rate to suit target)
// Arduino Mega serial1 test
// mega pin 18 is Tx
// pin 19 is Rx
// for loopback test connect pin 18 to pin 19
// for RS232 shield connect pin 18 to Tx and pin 19 to Rx
// for loopback test connect 9 pin D connector pins 2 and 3
unsigned long time;
void setup() {
Serial.begin(115200); // initialise serial monitor port
Serial1.begin(115200); // initialise Serial1
Serial.write("Arduino Mega Serial1 test - for loopback test connect pin 18 to pin 19\n");
}
void loop() {
if (Serial1.available()) // read from Serial1 output to Serial
Serial.write(Serial1.read());
if (Serial.available()) { // read from Serial outut to Serial1
int inByte = Serial.read();
//Serial.write(inByte); // local echo if required
Serial1.write(inByte);
}
}
a simple test is to connect RS232 9pin Dtype connector pins 2 and 3 (short with length of wire)
this forms a loopback test where characters entered on the serial monitor are echoed back to the display