Error reading uint8 values while trying to read serial information from a scale for mass-based flow calculation

Hello,

I am trying to read information from a scale that sends out a signal via an RS232 cable. I am reading this signal with a RS232 adapter that is connected to an Arduino Mega 2560. The connections are correct and we know this because we have gotten the code to run on multiple occasions.

The main issue is that the code occasionally will display the error message "failed to read "n" uint8 value(s) from the device" where "n" is the number of bits that the Arduino is trying to read from the scale. The baudrate on the scale and the code are both set to 19200, so we know this is not the issue.

Usually I use 63 for "n", but have also used 32 and other factors of 8. It has worked with 63, 32, and 56.

Here is the code I am using. I understand that this is MATLAB code and this is an Arduino forum, but I do not believe that the issue is with MATLAB, I believe the issue is with the Arduino in some way. Any guidance would be appreciated.

raw_data = zeros(3, 63);

g_finder = []; % Initialize as empty array

raw_weights = strings(1, 3); % Use strings to store raw weight data

for j = 1:4

if app.scale_list(j) ~= 0

raw_data(j, :) = read(Serial_Scale_list(j), 63);

g_finder = find(raw_data(j,:) == 103); % Assign new value to g_finder

% Process data for each scale

for k = 1:1

if numel(g_finder) >= 2 && g_finder(2) >= 8

raw_weights(k,1) = char(raw_data(k,(g_finder(2)-8):(g_finder(2)-1)));

% Convert the string to a number and append to mass_and_time_archives array

mass_archives(k, location) = str2double(raw_weights(k,1))/density_list(k);

time_archives(1, location) = (now - initial_time) * 24 * 3600; % Convert to seconds

% Calculate flow rate if there are at least two data points available

if location > 7

flowrate_archives(location, k) = (mass_archives(k, location) - mass_archives(k, location - 5)) / ...

((time_archives(1, location) - time_archives(1, location - 5)) / 60);

end

location = location + 1;

disp(raw_weights(k));

pause(1);

end

break; % Exit the loop after processing one set of data for this scale

end

end

end

Essentially, the scale outputs ASCII characters, that convert to something like the weight followed by a g, for example one output may be "1272 g" which has the raw ASCII value of "49 50 55 50 32 103 ". Since every weight has a "g" or a "103" at the end, I am finding in the output of the scale where the value 103 appears and reading a set amount of characters before that to get the weight output. Later the code determines the mass flow rate by taking the change in mass divided by the change in time.

This code is part of an app that I am building, although it returns the same issue when it is run independently (without the variables that are part of the rest of the app's code, of course), where it sometimes runs and sometimes does not while returning the same error message.

Like I said, the code occasionally works, which is why I am stumped as to what could be causing this problem. If anyone has any ideas they would be appreciated.

Thanks

1 Like

I am having this same issue I really need help too. Not sure why it keeps happening.

1 Like

well - then what's the code running on the Arduino ??

I am communicating with the Arduino through MATLAB, I have not written anything into the Arduino IDE. Is there a way to access the Arduino-specific code? I connect to the Arduino at the beginning of the app with the following code.

a = arduino('COM7', 'Mega2560', 'Libraries', 'Serial')
Serial_Scale_A = device(a, 'SerialPort', 1, 'BaudRate', 19200, 'DataBits', 8, 'StopBits', 1"

1 Like

We restarted the scale and the code began to work, but why would this fix it?

1 Like

So how can the Arduino be the problem? And why are you using an Arduino? You could just as well use a RS232-to-USB adapter and connect the scale directly to the PC.

To which pins did you connect the RS232 adapter? If the answer is 0 and 1, that is basically wrong because those pins are also connected to the serial-to-usb adapter for communication with the PC and if the current Arduino program sends data to the serial port it will confuse your matlab.

Those could be read directly using a very simple Arduino program, and analyzed on the Arduino. No need for Matlab.

can you detail how things are wired ? (seems indeed the arduino is useless)

Correct. I understand. We are using MATLAB for its data acquisition toolboxes and its app designer. Arduino IDE does not have the capability to perform these tasks to my knowledge. This is not a question regarding which interface is the best for our needs. We already know that MATLAB fits our needs. We are using the Arduino so that we can connect lots of sensors of differing input types to it all at once and read them on one device. The scale is not the only part of this project that we need, and we actually will need 5 of them when the project is finished. The fact that we have so many sensors that we need to collect data from is the reason we are using the Arduino.

Now, I will address the question about the wiring. On the back of the scale is a 9-pin female RS232 port. On the upper row of this port (the row containing 5 holes) to the far left is a blue wire that is connected to the left side of another RS232 female adapter that is connected to ground. On the middle hole and the hole to the right of the middle top are the data transmission holes. The middle hole is data in, I believe, and the hole to the right of it is the data out. These are then connected via wires to the same adapter as before to the respective in/out holes. In other words, the data out of the scale goes to the data in of the adapter and vice versa. The adapter is there to convert the output of the scale into something the Arduino can interpret. Again, the Arduino is a necessary part of this project because of other sensors that are not talked about here. Please do not tell me that the Arduino is useless in this scenario because there is more to this project than just this one scale. Out of the back of the adapter are four cables that connect to ground, 5V, RX, and TX. The serial port that we are using utilizes ports 18 and 19, not 0 and 1. I understand that 0 and 1 are the serial ports used for USB connection to the computer; this is why we are not using them.

Again, I understand that this could very easily be done in the Arduino program (which we did initially to test the connection), but now we need to get the information into MATLAB to connect it to the rest of our sensor information.

void setup()
{
  Serial.begin(115200);
  Serial1.begin(19200);
}

// fixed mistake
//void setup()
void loop()
{
  if(Serial1.available())
  {
   Serial.write(Serial1.read());
  }
}

This should be the simplest sketch to read the data from the scale and send it to matlab.

// Edit: Adjust the baud rates to your needs.

1 Like

That's almost as good as turning it off, then back on again.

Which we all know can fix a great number of problems.

a7

1 Like

We are saying that if there is no code on the arduino, then it's useless because it won't interpret anything.

it would make sense to have some code on the arduino dealing with all the sensors and building Serial frames with whatever protocol and send them to your MATLAB script.

Thank you for the helpful reply. This is where we began in the Arduino IDE before we moved over to MATLAB. It was very helpful for testing whether our connection was correct before we moved it over - which it was.

In the simplest terms possible, we only have this on MATLAB because the rest of our code is on MATLAB and this is going to be integrated into an application that constantly monitors a variety of sensors. We simply figured it would be best to keep the system to one program, rather than MATLAB and Arduino, but that the Arduino would be a good connection point between our sensors and our computer. I guess I understand that the Arduino is not really DOING anything, but we are using it to collect a wide variety of signals from different devices for cheap. Later we will try to use the Arduino to send voltage out to control several devices.

Can you think of any reasons as to why converting this code over into MATLAB may cause a reading error? If not that is fine, as I understand this is not the MATLAB forum (I tried posting there and it seems it is mostly dead).

Yes it happened to fix it for this one instance, but occasionally it does not work.

Can you elaborate a little more on that? Right now we basically just do all the signal interpretation in MATLAB. Would we have to upload our sketch in Arduino IDE to the Arduino MEGA to do what you are saying? Also, would this require that the Arduino code is run at the same time as our MATLAB code from the IDE or would it just be uploaded onto the board which would automatically interpret our signals into the signals we want before sending it to MATLAB whenever the MATLAB program is run?

so you connected the Serial link from the scale on Tx1/Rx1 of your Mega (Serial1)
you are right that pin 0 and 1 correspond to the USB to Serial connection

but unless you create in some way an "echo" program that reads what comes from Serial1 and copy it over Serial, then your PC won't receive anything.

you should upload the code from post #10 to the Arduino, that's what it does (Make sure MATLAB listens at 115200 bauds).

if you have tons of other sensors connected to the MEGA, then you probably have also some code for those sensors, in which case you need to find out what code was uploaded to arduino.

The weird part is that it does read on occasion. Only on some startup periods does the uint8 error message appear. When it does work, the PC receives all of the information from the scale.

Correct, but there would be no problem for the Arduino to present properly interpreted scale data to Matlab.

As you are discovering, Matlab is poorly equipped for such tasks, whereas Arduino is well equipped.

could it be that some sort of code was uploaded to the Arduino and you don't know about it ?

So are you saying that we can write a script for Arduino, upload it to the board, close the IDE, open MATLAB, and when we read from the serial port it will be in the format we want and no interpretation will be required by MATLAB? Just trying to understand.