I'm trying to get my Arduino UNO to read data from my weight indicator model XK3190-A12(E) and display them on the Arduino IDE Serial Monitor, but a lot of the code scripts I've tried don't work. Here's two of our setups with and without a converter:
#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11
// Set up a new SoftwareSerial object
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
String Data = "";
void setup() {
// Define pin modes for TX and RX
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
Serial.begin(9600);
// Set the baud rate for the SoftwareSerial object
mySerial.begin(9600);
}
void loop() {
if (mySerial.available() > 0) {
Serial.println("mySerial is available");
delay(3000);
char character = mySerial.read(); // Receive a single character from the software serial port
Data.concat(character); // Add the received character to the receive buffer
if (character == '\n')
{
Serial.print("Received: ");
Serial.println(Data);
// Clear receive buffer so we're ready to receive the next line
Data = "";
}else{
Serial.println("No data read.");
}
}else{
Serial.println("mySerial is not available");
delay(3000);
}
}
Well, the first thing I must say is: if the device is RS232 you must use the RS232-TTL converter because Arduino has TTL levels. For more information, see THIS for example.
Then, the code. Before talking about communication data structure, you need to be able to get the data and at least print it over Serial Monitor.
If you do this:
if (mySerial.available() > 0) {
Serial.println("mySerial is available");
delay(3000);
char character = mySerial.read(); // Receive a single character from the software serial port
Data.concat(character); // Add the received character to the receive buffer
if (character == '\n')
{
Serial.print("Received: ");
Serial.println(Data);
// Clear receive buffer so we're ready to receive the next line
Data = "";
}else{
Serial.println("No data read.");
}
}else{
Serial.println("mySerial is not available");
delay(3000);
}
}
Ignoring those senseless 3-seconds delays, you'll get "No data read" every time you receive any character different than '\n', so the output you posted here shows you received "something". And avoid using "String" variables, you should learn using "C strings" (aka "char arrays").
Give this version a try (not tested, just drawn on the fly):
#include <SoftwareSerial.h>
#define rxPin 10
#define txPin 11
// Set up a new SoftwareSerial object
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
// Buffer
char Data[20];
byte pos = 0;
void setup() {
// NOT NEEDED!
// pinMode(rxPin, INPUT);
// pinMode(txPin, OUTPUT);
Serial.begin(9600);
// Set the baud rate for the SoftwareSerial object
mySerial.begin(9600);
}
void loop() {
if (mySerial.available() > 0) {
char character = mySerial.read(); // Receive a single character from the software serial port
// End of packet? ('\n' or LF, packet byte #15)
if (character == '\n')
{
Serial.print("Received: ");
Serial.println(Data);
// Clear receive buffer so we're ready to receive the next line
pos = 0;
Data[pos] = 0x0;
} else {
// Ignore '\r' (CR or 0x0d, packet byte #14)
if (character != '\r') {
// Add the received character to the receive buffer
Data[pos++] = character;
Data[pos] = 0x0;
}
}
}
}
The 3 second delays are mainly just for my eyes to keep up with tracking if it reads as available if ever. I tried the code with setup 2 since it had the Rs232 to TTL converter chip but it doesn't display anything.
The reason that the scale would not communicate with the Arduino, was that the GND pin inside the scale was connected to a different pin to that which was shown on the schematic.
It might pay you to look inside your scale, and check the connector wiring.
I'd check the source with a LED and series resistor. Does the weight indicator show anything with no weight on it? Surely the OP has something to be weighed.
Nothing at all? My first concern is when you started testing: if you connected RS232 directly to Arduino, the risk is an I/O pin damage... So DON'T.
Try adding a few more debug prints (and change the pins, just to make sure 10 and 11 aren't working anymore):
#include <SoftwareSerial.h>
#define rxPin 8
#define txPin 9
// Set up a new SoftwareSerial object
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
// Buffer
char Data[20];
byte pos = 0;
void setup() {
Serial.begin(9600);
// Set the baud rate for the SoftwareSerial object
mySerial.begin(9600);
Serial.println("STARTED");
}
void loop() {
if (mySerial.available() > 0) {
char character = mySerial.read(); // Receive a single character from the software serial port
Serial.print("Byte: "); Serial.println(character, HEX);
// End of packet? ('\n' or LF, packet byte #15)
if (character == '\n')
{
Serial.print("Received: ");
Serial.println(Data);
// Clear receive buffer so we're ready to receive the next line
pos = 0;
Data[pos] = 0x0;
} else {
// Ignore '\r' (CR or 0x0d, packet byte #14)
if (character != '\r') {
// Add the received character to the receive buffer
Data[pos++] = character;
Data[pos] = 0x0;
Serial.println("Stored.");
}
}
}
}
Remember to power the TTL converter with 5V, not 3.3V, and see what the Serial monitor reports (just in case, make a copy/paste here of the output).
But check also the weight indicator before, just to make sure it is set to 9600 baud: