Hi everyone hope u are fine.
i connect my arduino uno with an rs232 to ttl converter than with my energy meter, and i want to know the response of a frame(FE FE FE FE 68 99 99 99 99 99 99 68 23 0A 60 00 34 12 78 56 BC 9A F0 DE 2B 16) so i do this code but the result is just 000000000
considering that this frame has a frame response in the datasheet of this energy meter
#include<SoftwareSerial.h>
//SoftwareSerial pins
#define pinTX 2
#define pinRX 4
byte data[47];
SoftwareSerial RS232(pinRX, pinTX);
static byte trame1[] = {0xFE,0xFE,0xFE,0xFE,0x68,0x99,0x99,0x99,0x99,0x99,0x99,0x68,0x23,0x0A,0x60,0x00,0x34,0x12,0x78,0x56,0xBC,0x9A,0xF0,0xDE,0x2B,0x16};
// FE FE FE FE 68 99 99 99 99 99 99 68 23 0A 60 00 34 12 78 56 BC 9A F0 DE 2B 16
void setup() {
RS232.begin(9600);
Serial.begin(9600);
Serial.println("Starting");
}
void loop() {
for(int i=0;i<26;i++){
RS232.write(trame1[i]);
}
while (RS232.available()){
RS232.readBytes(data, 18);
}
delay(1000);
for (int i=0;i<47;i++){
Serial.print(data[i]) ;
}
Serial.print("\n") ;
delay(5000);
}
do you have an oscilloscope to look at the signals on the RS232 serial lines?
check the Rx and Tx signal levels - should be around -12volts when idle rising to +12V when signals are present - if you don't have a oscilloscope a voltmeter would give you some idea if levels are correct
you should see your Tx data and if there is any response
i think you didn t inderstood me,
when i send this frame via an rs232 cable with my laptop i can get the response with docklight like (FE FE FE FE 68 99 99 99 99 99 99 68 23 02 60 80 6B 16) but now i want to get the response via arduino so i did this connection
Usually, I do in the following way with an understanding that follows and the trick works.
byte n = Serial.available();
if( n != 0)
{
Serial.readBtes(myData, 18);
}
It checks that there is atleast one data byte in the Serial Buffer. After that, the control enters into the execution of readBytes() which is a blocking code. The code line keeps reading characters from the Serial Buffer as they arrive and makes an exit when 18 charcaters/data bytes are read or timeout (default 1-sec) occurs.
Therefore, the following codes of the OP are equivalent to my code and should work also:
while (RS232.available())
{
RS232.readBytes(data, 18);
}
Firstly as pointed out you are not checking that 18 characters are available before reading them - not going to work reliably (though readBytes() uses timed-out reads so it may appear to work sometimes.
Secondly readBytes() returns the actual number of bytes read and you are ignoring the result - always check it.
Thirdly you risk loop reading repeatedly reading without using the results. There is no reason to use a while loop here.
I think what you actually need is more like:
if (RS232.available () >= 18) // guard the read by ensuring it will complete
{
int bytes_read = RS232.readBytes (data, 18) ;
if (bytes_read == 18) // make doubly sure
{
... do something with the data ...
}
}
it may be simpler to move to an Arduino Mega (if available) which has hardware serial ports
I try to avoid doing serious work using SoftwareSerial or similar bitbash code - recently had to use bitbash I2C when hardware designer misconfigured pins on a PIC24 !
Excellent suggestion. The following setup with MEGA works; but, I am not happy with output message in the Serial Monitor as it shows an unsyncronized message.