Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[AVR] Communicate weighing scale with Arduino using RS232

Status
Not open for further replies.

ChangeIsConstant

Newbie level 2
Joined
Mar 26, 2015
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
26
I am trying to obtain the weights from a weighing scale KERN ew220-3nm to Arduino Uno through RS232.
This is the code :

Code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(5, 4);  

void setup()  
{
  Serial.begin(9600);  
  mySerial.begin(9600);

}

void loop() 
{
 while (mySerial.available()){
   Serial.println(mySerial.read(),HEX);
}
 }

As per the device manual, the complete transmitted output consists of 15 words, 1 start bit, 8 data bits(except 2-9 words which are data bytes and have maximum 6 characters), no parity, 2 stop bits.

This is the output obtained :

Code:
6A
9F
7C
F1
86
C6
1A
C6
46
19
43
97
43
15
79
EB
0

I have a few doubts -

1. Do the 2 stop bits as mentioned in the device manual disturb my complete data or is it ignored?

2. Whenever I print the incoming byte, does it print only the last byte or does it print some data from before along with the incoming data(if it is not a complete byte)?

3. Is the data to be read inversely due to the RS232 protocol of sending the LSB first?

4. The results include 16 or sometimes 17 words (which is basically not possible). The first word is 2BH or 20H and the last words as specified by the manual are CR and LF. But I cannot detect any output corresponding to that. Does that mean all of my output is some garbage data or do I need to decode it somehow?

Please help me with this. Thank you.
 

1. The stop bits are ignored, but the arduino serial port should be configured for 2 stop bits (default settings are 8bit, no parity, 1 stop bit). See the part about the serial.begin parameters https://arduino.cc/en/Serial/Begin I see that you are using software uart for the sensor. I don't know if you can configure that for 2 stop bits, but seeing that. Maybe you should look it up or if not, use the hardware serial for your sensor (and find another way to print your outputs).

2. Only "complete" bytes are printed.

3.No, data is not read inversely.

4. I see that with your code you are simply trying to pass whatever your scale sends to the serial monitor. I suggest that you use serialevent for handling incoming data https://arduino.cc/en/Reference/SerialEvent and readbytesuntil because you know that your messages end in cr and lf https://arduino.cc/en/Serial/ReadBytesUntil
 

1. The stop bits are ignored, but the arduino serial port should be configured for 2 stop bits (default settings are 8bit, no parity, 1 stop bit). See the part about the serial.begin parameters https://arduino.cc/en/Serial/Begin I see that you are using software uart for the sensor. I don't know if you can configure that for 2 stop bits, but seeing that. Maybe you should look it up or if not, use the hardware serial for your sensor (and find another way to print your outputs).

2. Only "complete" bytes are printed.

3.No, data is not read inversely.

4. I see that with your code you are simply trying to pass whatever your scale sends to the serial monitor. I suggest that you use serialevent for handling incoming data https://arduino.cc/en/Reference/SerialEvent and readbytesuntil because you know that your messages end in cr and lf https://arduino.cc/en/Serial/ReadBytesUntil

@ernpao
Thank you for your reply.
1. The configuration and SerialEvent work only for Serial port and not the Software Serial port.
2. I have kept that in mind for use later but the problem here is I am not getting the exact bytes that I can use for the command 'ReadBytesUntil'.

I saw something about the Library AltSoftSerial and used it in my program instead of SoftwareSerial and I think I am getting better results. It is to do with the amount of time the libraries waste before recording the data using Interrupt.
I know that this library gives better results as the manual says that the serial data is made of 15 words (bytes) and I am getting exactly 15 bytes now, with constant 1st, 2nd last and last bytes, as expected. But still, the data does not match the one I require. I shouldn't be required to decode serial data, should I?

The 'new' code is
Code:
#include <AltSoftSerial.h>
AltSoftSerial mySerial;  
#define TwobitDelay 200

void setup()  
{
  Serial.begin(9600);  
  mySerial.begin(9600);
}
void loop() 
{
 while (mySerial.available()){
   Serial.println(mySerial.read(),HEX);
   delayMicroseconds(TwobitDelay);
}
}

And my results are :

HTML:
6A
C6
C6
86
C6
E3
C6
C6
86
13
E1
23
45
1E
3D

As mentioned earlier, 6A, 1E and 3D are obtained in every set of data. Also, this is for 0 gram, which could mean C6 or 86 is 0. But I do not think I should need to decode it this way.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top