1

Here's the system I want to work on:

  1. The weight controller device detects weight which can transfer data via RS232 serial connection
  2. I used RS232 converter to TTL serial module to connect the weight controller device to Arduino
  3. The TTL connections were made to respective pins of Arduino UNO Board (GND-GND, VCC-5V, RX-0, TX-1)

The serial print from serial connection should produce 17 character length string : ST,GS 1.58,kg

Why am I getting strange results at times?

// Read Serial (RS232) data from Weight Controller Device via RS232 to TTL Converter Module

// Ignore the include statement, the editor does not allow pasting certain characters

include SoftwareSerial.h

int RX_PIN = 2;
int TX_PIN = 3;
int BAUD_IDE = 9600;
int BAUD_WEIGHT_SENSOR = 9600;

// serial port from weight controller
SoftwareSerial mySerial = SoftwareSerial(RX_PIN, TX_PIN);

void setup() {   
  mySerial.begin(BAUD_WEIGHT_SENSOR);
  Serial.begin(BAUD_IDE);
}

void loop() {  
  while (mySerial.available()) {
    int inChar = mySerial.read();    
    if (inChar != '\n') {
      serialData += (char)inChar;
    }
    else {
      int dataSize = serialData.length();      
      Serial.print(serialData);
      Serial.print(" length:  "); 
      Serial.print(dataSize); 
      Serial.print("\n"); 
      delay(3000); 
      serialData = "";
    }    
  }
  serialData = "";

}

// this one works perfectly but i want to store as string to 
process
// information and use for other functions
/*void loop() {  
if (mySerial.available()){          
  Serial.print((char)mySerial.read());  

enter image description here

1
  • I have got the same problem, if you have already had the answer please help me. Thanks in advance
    – Deisson
    Commented Mar 6, 2018 at 15:32

2 Answers 2

2

You need to disconnect the Arduino`s RX and TX pins from any device before trying to upload a sketch via USB, because the Serial data is directly wired from the USB chip to these pins. It's as if you're trying to flash the thing behind your RS232 adapter.

2
  • 1
    Yes, if they were trying to use the hardware UART, but they are not - the root problem is that they placed the peripheral and their software UART on the hardware UART pins rather than on distinct pins, creating an unnecessary conflict with operation as well as flashing. Your suggestion will get the code uploaded, but not make it work (if they do see output, it would be output directly from the converter, not through the sketch). Commented Mar 2, 2017 at 15:36
  • that worked, atleast i m getting some data now but it's not what i expected Commented Mar 5, 2017 at 1:38
1

You are attempting to talk to your peripheral via a second serial UART implemented in software on arbitrary pins, which should leave the hardware UART free for uploading and debug or other runtime communication between the Arduino and a PC.

However, the "arbitrary pins" you choose to use to connect the peripheral are the same pins as used by the hardware UART.

Move your peripheral to two other pins, for example

int RX_PIN = 2;
int TX_PIN = 3;

and move the connections accordingly

Also consider if you really want Serial.println() which will add a line break after every character in the output, and will also print the received characters as their numeric ASCII codes, one per line, rather than printing the characters themselves. You probably want Serial.write().

3
  • I did as you said. However, the output is not what was desired.please see my latest comment Commented Mar 5, 2017 at 2:18
  • As I already said, serial.println() is not what you want. However it looks like the problem is more than just line breaks. As Serial.read() returns an integer, you are printing the ascii codes of your data rather than the data itself. Apparently to fix that you want Serial.write() Commented Mar 5, 2017 at 3:19
  • fixed it, still have minor bugs, please check my revised post. is the current bug related to baud rate? Commented Mar 5, 2017 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.