Here's the system I want to work on:
- The weight controller device detects weight which can transfer data via RS232 serial connection
- I used RS232 converter to TTL serial module to connect the weight controller device to Arduino
- 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());
-
I have got the same problem, if you have already had the answer please help me. Thanks in advanceDeisson– Deisson2018年03月06日 15:32:00 +00:00Commented Mar 6, 2018 at 15:32
2 Answers 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.
-
1Yes, 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).Chris Stratton– Chris Stratton2017年03月02日 15:36:55 +00:00Commented Mar 2, 2017 at 15:36
-
that worked, atleast i m getting some data now but it's not what i expectedRadian Mass– Radian Mass2017年03月05日 01:38:20 +00:00Commented Mar 5, 2017 at 1:38
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()
.
-
I did as you said. However, the output is not what was desired.please see my latest commentRadian Mass– Radian Mass2017年03月05日 02:18:40 +00:00Commented 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()Chris Stratton– Chris Stratton2017年03月05日 03:19:48 +00:00Commented 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?Radian Mass– Radian Mass2017年03月05日 05:56:02 +00:00Commented Mar 5, 2017 at 5:56
Explore related questions
See similar questions with these tags.