yeah that was what the issue was. We fixed it yesterday. Thank you for the recommendation, though.
Alright, back again. Apologies for any confusion that I may have caused with everything, I made a new post when I shouldn't have, apparently. I will post the contents of that new post here (not cross posting, just informing you all of the new issue). Apologies for violating forum rules, this was never my intent.
Anyways, here is the new problem:
Scale:
Outputs bytes that correspond to ASCII characters like so:
83 32 83 32 32 32 32 32 50 51 52 46 49 56 103
which converts to
S S 234.18g
This signal is sent via three cables to an adapter that converts the RS232 signal to a signal Arduino can interpret in its serial ports. All of the wiring is correct (and I know this because the Arduino IDE can successfully read the scale outputs on its serial monitor), so I do not see a need to post this right now, but if it is needed, just ask.
The code to read and interpret the scale's reading on the Arduino is this:
void setup() {
Serial.begin(115200); // initialise serial monitor port
Serial1.begin(19200); // initialise Serial1
}
void loop() {
static String buffer = ""; // Buffer to store characters
if (Serial1.available()) {
char receivedChar = Serial1.read();
if (isDigit(receivedChar) || receivedChar == '.' || receivedChar == ' ') {
// Append valid characters (digits, decimal point, space) to buffer
buffer += receivedChar;
}
else if (receivedChar == '\n') {
// If newline is received, print the buffer and clear it
Serial.println(buffer);
Serial1.println(buffer);
buffer = "";
}
}
}
Essentially, this code is taking the readout: 83 32 83 32 32 32 32 32 50 51 52 46 49 56 103 , converting it to: S S 234.18g, then appending digits, decimal points, and spaces to a string called buffer. There is also a newline character in the scale readout which is how the Arduino knows where to start a newline (as you can see, the Arduino detects the newline character, prints the string buffer in its current format (in this case it would be 234.18), then clears buffer and moves to another line (I think)).
We want to take the readout of " 234.18" and send this directly to MATLAB so that it is useful for the rest of our program. MATLAB is where we are building an application for a monitoring system, so yes, this is an essential step.
Now, to the current MATLAB code:
clear;
% Connect to Arduino board
a = arduino('COM7', 'Mega2560', 'Libraries', 'Serial');
% % Connect to Serial port 1 with baud rate 19200
scale_a = device(a, 'SerialPort', 1, 'BaudRate', 19200);
while true
disp(char(read(scale_a,1)))
end
This code establishes connection with the Arduino in the first line, then establishes the variable scale_a as the device that is assigned to the arduino with serial port "1" and a baudrate of 19200. It then gets the characters of the serial port 1 readout that the Arduino should have theoretically written (the " 234.18" but in ASCII) and converts them to characters that the user can see and displays them in the command window.
Right now, this code displays in the MATLAB command window every character that the scale puts out, i.e. the command window output looks like:
S
S
2
3
4
.
1
8
g
when I want it to be outputting:
234.18
So, I have multiple questions regarding this:
- How do I fix the Arduino code so that when I read serial port 1 on MATLAB, it reads exactly what the Arduino is writing instead of what the Arduino serial is reading? In other words, how do I make the Arduino read the serial input, convert the serial input to the desired string, then output the desired string to the serial output, then read the serial output in MATLAB and get the correct string?
- When I read serial port 1 in MATLAB, how can I get it so that each reading is a new line instead of it reporting each ASCII-converted character as a single line? (I understand this is not a MATLAB forum, so it may be more difficult to answer this question).
Some things we know/potentially important information:
- The baudrate on the serial monitor is 115200 and the baudrate of the serial port 1 is 19200. The baudrate of the scale is set to 19200. The baudrate of serial port 1 is set to 19200 in MATLAB. We tried also setting the serial monitor to 19200, but this did not change anything, so we decided that this was not the problem.
- The serial monitor is displaying exactly what we want the output to be in the MATLAB command window. In other words, the serial monitor output looks like:
234.18
234.18
234.18
234.18
...and so on...
Please let me know if any additional information is required, or if I have neglected any obvious solutions. I am familiar with MATLAB, not Arduino IDE, so forgive any stupid mistakes I may have made in explaining what the Arduino code does.
Thank you in advance for the help, I will be actively responding to messages as they come in.
Hello,
The basic gist of this project is that I am trying to take RS232 data from a Mettler Toledo scale (model XP6002S) and send it to my Arduino, which will then cut out unnecessary information from the scale readout, package it in a way that each new line is a new weight reading, and then send it via serial to MATLAB. Here is an example of a scale to MATLAB data process.
Scale:
Outputs bytes that correspond to ASCII characters like so:
83 32 83 32 32 32 32 32 50 51 52 46 49 56 103
which converts to
S S 234.18g
This signal is sent via three cables to an adapter that converts the RS232 signal to a signal Arduino can interpret in its serial ports. All of the wiring is correct (and I know this because the Arduino IDE can successfully read the scale outputs on its serial monitor), so I do not see a need to post this right now, but if it is needed, just ask.
The code to read and interpret the scale's reading on the Arduino is this:
void setup() {
Serial.begin(115200); // initialise serial monitor port
Serial1.begin(19200); // initialise Serial1
}
void loop() {
static String buffer = ""; // Buffer to store characters
if (Serial1.available()) {
char receivedChar = Serial1.read();
if (isDigit(receivedChar) || receivedChar == '.' || receivedChar == ' ') {
// Append valid characters (digits, decimal point, space) to buffer
buffer += receivedChar;
}
else if (receivedChar == '\n') {
// If newline is received, print the buffer and clear it
Serial.println(buffer);
Serial1.println(buffer);
buffer = "";
}
}
}
Essentially, this code is taking the readout: 83 32 83 32 32 32 32 32 50 51 52 46 49 56 103 , converting it to: S S 234.18g, then appending digits, decimal points, and spaces to a string called buffer. There is also a newline character in the scale readout which is how the Arduino knows where to start a newline (as you can see, the Arduino detects the newline character, prints the string buffer in its current format (in this case it would be 234.18), then clears buffer and moves to another line (I think)).
We want to take the readout of " 234.18" and send this directly to MATLAB so that it is useful for the rest of our program. MATLAB is where we are building an application for a monitoring system, so yes, this is an essential step.
Now, to the current MATLAB code:
clear;
% Connect to Arduino board
a = arduino('COM7', 'Mega2560', 'Libraries', 'Serial');
% % Connect to Serial port 1 with baud rate 19200
scale_a = device(a, 'SerialPort', 1, 'BaudRate', 19200);
while true
disp(char(read(scale_a,1)))
end
This code establishes connection with the Arduino in the first line, then establishes the variable scale_a as the device that is assigned to the arduino with serial port "1" and a baudrate of 19200. It then gets the characters of the serial port 1 readout that the Arduino should have theoretically written (the " 234.18" but in ASCII) and converts them to characters that the user can see and displays them in the command window.
Right now, this code displays in the MATLAB command window every character that the scale puts out, i.e. the command window output looks like:
S
S
2
3
4
.
1
8
g
when I want it to be outputting:
234.18
So, I have multiple questions regarding this:
-
How do I fix the Arduino code so that when I read serial port 1 on MATLAB, it reads exactly what the Arduino is writing instead of what the Arduino serial is reading? In other words, how do I make the Arduino read the serial input, convert the serial input to the desired string, then output the desired string to the serial output, then read the serial output in MATLAB and get the correct string?
-
When I read serial port 1 in MATLAB, how can I get it so that each reading is a new line instead of it reporting each ASCII-converted character as a single line? (I understand this is not a MATLAB forum, so it may be more difficult to answer this question).
Some things we know/potentially important information:
-
The baudrate on the serial monitor is 115200 and the baudrate of the serial port 1 is 19200. The baudrate of the scale is set to 19200. The baudrate of serial port 1 is set to 19200 in MATLAB. We tried also setting the serial monitor to 19200, but this did not change anything, so we decided that this was not the problem.
-
The serial monitor is displaying exactly what we want the output to be in the MATLAB command window. In other words, the serial monitor output looks like:
234.18
234.18
234.18
234.18
...and so on... -
I have been told before that it could be a good idea to package the data like so in Arduino before we send it over to MATLAB:
<1, 234.18> where "<" indicates start point to read, the number before the comma indicates the scale that the weight was read off of (we eventually want to have 5 scales (we understand we will need to get a second Arduino for this), and then the number after the comma is of course the weight read from that scale, and then ">" indicates a stop reading to MATLAB. I was also informed that it could also be possible to do this with a simple string like "1, 234.18 \n" so that MATLAB knows where to begin a new line after it gathers the data it needs. Either one of these formats (or an alternative string format) would be nice to export the data as packages to MATLAB. The only problem I am having is actually sending this data to MATLAB so that it reads it in the appropriate way. -
I am using an Arduino Mega 2560
Please let me know if any additional information is required, or if I have neglected any obvious solutions. I am familiar with MATLAB, not Arduino IDE, so forgive any stupid mistakes I may have made in explaining what the Arduino code does.
Thank you in advance for the help, I will be actively responding to messages as they come in.
If I could also get rid of the 5 or 6 spaces before the weight readout (make " 234.18" into "234.18") that would also be appreciated. I am assuming this would be as simple as removing the || receivedChar == ' ' from the "if" loop, but I also have no idea.
I would think changing
if (isDigit(receivedChar) || receivedChar == '.' || receivedChar == ' ') {
to
if (isDigit(receivedChar) || receivedChar == '.') {
would remove spaces
That's also what I thought. Any idea on the rest of it?
Please do not keep starting new threads. Cross post to
and
I understand these are related, however this is a different question. Please read the question before assuming this simply because I have had lots of questions on related topics recently.
I seem to recall that most of the weighing scales that output a reading generate a fixed length text string - usually with the weight right justified in the text string.
If you are using the String class, then it may be simpler to just read in the whole line of ASCII characters till you reach the newline. Then use the substring() function to extract a specific group of digits.
Of course this assumes that your scales output a fixed width text message.
Threads merged.
Thanks to all for responding. I think we are giving up on this method and simply doing the interpretation in MATLAB since we know how to do it there.
Thanks again
Use 115200 as this is the baud rate used between the arduino and the PC since you wrote
19200 is only useful between the scale and the arduino
So what does the below do? I'm not a matlab person but it feels like this reads one character and displays it on a new line. You're the matlab person so you should be able to figure that out
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.