Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Arduino - BMSerial and SoftwareSerial librairies


#1

Hi everybody,

I need some help concerning a conflict between librairies. I work on an Arduino Uno R3.
Currently, my project consists on receiving data from a sensor, and control a motor with a Roboclaw 2x5A.
My main code with the control of the motor works great, using the BMSerial.h library.
Nevertheless, I need to integrate another code, which asks my sensor with the command “MSV?\r\n” to receive the data. This code worked great with the SoftwareSerial.h library but now, it’s not possible to send the command “MSV?\r\n”. And I need to make it works with BMSerial to include it in my main code.

My code with SoftwareSerial is :

[code]#include <SoftwareSerial.h>
String chaine1;
String test;
int data;
int i = 0;

SoftwareSerial Poids(7, 6); // RX, TX

void setup()
{
Poids.begin(38400);
Serial.begin(38400);
Serial.println(“Test AD104C - Initialisation”);
delay(5);
}

int HBM()
{ chaine1 = “”;
test = “”;
data = 0;

Poids.write("MSV?\r\n");

while (Poids.available() == 0) {} // bloque si pas de DATA / 0 = NULL

while (Poids.available() != 0) // on rentre dans la boucle si on a des DATA (10 = LF et 13 = CR)
{
data = Poids.read();
chaine1 += data;
}

test = chaine1.substring(16, 20); // on repere les erreurs rencontrées
if (test.equals(“1310”)) // taille trame conforme = on affiche pas si

{ chaine1 = chaine1.substring(0, 20); // on enleve les CR LF de fin de trame
int poids = 0;
poids = 1000000 * ((chaine1.substring(2, 4)).toInt() - 48) + 100000 * ((chaine1.substring(4, 6)).toInt() - 48) + 10000 * ((chaine1.substring(6, 8)).toInt() - 48) + 1000 * ((chaine1.substring(8, 10)).toInt() - 48) + 100 * ((chaine1.substring(10, 12)).toInt() - 48) + 10 * ((chaine1.substring(12, 14)).toInt() - 48) + 1 * ((chaine1.substring(14, 16)).toInt() - 48);
return poids;
}
}

void loop()
{
Serial.println(HBM());
}[/code]

My error is :

[code]

final_AD104C.ino: In function ‘float HBM()’:
final_AD104C:24: error: invalid conversion from ‘const char*’ to ‘uint8_t {aka unsigned char}’ [-fpermissive]
In file included from final_AD104C.ino:1:0:
/Users/coste/Documents/Arduino/libraries/BMSerial/BMSerial.h:110:17: error: initializing argument 1 of ‘virtual size_t BMSerial::write(uint8_t)’ [-fpermissive]
virtual size_t write(uint8_t byte);
^
invalid conversion from ‘const char*’ to ‘uint8_t {aka unsigned char}’ [-fpermissive][/code]

Thank you in advance for your help :slight_smile:


#2

Hello.

From the error message you are getting it looks like you are trying to pass a const char* (“MSV?\r\n”) to the BMSerial library’s virtual method write, which only takes uint8_t; not a series of bytes like the SoftwareSerial library’s write method. You might try using the print method instead of write. For example:

   Poids.print("MSV?\r\n");

- Amanda


#3

[quote=“AmandaS”]

   Poids.print("MSV?\r\n");
  • Amanda[/quote]

Hello Amanda and thank you for your answer !
Nevertheless, I tried it and it doesn’t work, I think that I really need to write “MSV?\r\n” on the serial to ask my sensor.

Is there another declaration of the write fonction to use it like a want ? Or do you think there is another method ? Thank you in advance for your help


#4

Hi

I tried a lot of things like modifying the BMSerial library to adapt it to my case but it doesn’t work …
Can somebody help me ? If I have the solution, I can completely finish my project … :frowning:


#5

Hi, Poly34.

Could you post the code of your original sensor program that does work (the one that uses SoftwareSerial)? Also, what version of the Arduino IDE are you using?

- Kevin


#6

Hi Kevin, thank you for you answer !

This is my code which works fine (you will remark that I only comment one or the other library, and if I use the SoftwareSerial, it works)

[code]//#include “BMSerial.h”
#include “SoftwareSerial.h”

//#pragma GCC diagnostic ignored “-Wwrite-strings”

String chaine1;
String test;
int data;
int i = 0;

// POIDS MAX = 1,68kg

//BMSerial Poids(7, 6); // RX, TX
SoftwareSerial Poids(7, 6); // RX, TX

void setup()
{
Poids.begin(38400);
Serial.begin(38400);
Serial.println(“Test AD104C - Initialisation”);
delay(5);
}

float HBM()
{ chaine1 = “”;
test = “”;
data = 0;
const char msg[] =“MSV?\r\n”;
//char *msg=“MSV?\r\n”;

Poids.write(msg);

// 77 83 86 63 92 114 92 110
// Poids.write(‘M’);
// Poids.write(‘S’);
// Poids.write(‘V’);
// Poids.write(’?’);

while (Poids.available() == 0) {} // bloque si pas de DATA / 0 = NULL

while (Poids.available() != 0) // on rentre dans la boucle si on a des DATA (10 = LF et 13 = CR)
{
data = Poids.read();
chaine1 += data;
}
//Serial.print(chaine1);
test = chaine1.substring(16, 20); // on repere les erreurs rencontrées
if (test.equals(“1310”)) // taille trame conforme = on affiche pas si

{ chaine1 = chaine1.substring(0, 20); // on enleve les CR LF de fin de trame
int poids = 0;
poids = 1000000 * ((chaine1.substring(2, 4)).toInt() - 48) + 100000 * ((chaine1.substring(4, 6)).toInt() - 48) + 10000 * ((chaine1.substring(6, 8)).toInt() - 48) + 1000 * ((chaine1.substring(8, 10)).toInt() - 48) + 100 * ((chaine1.substring(10, 12)).toInt() - 48) + 10 * ((chaine1.substring(12, 14)).toInt() - 48) + 1 * ((chaine1.substring(14, 16)).toInt() - 48);
return (float)(poids/10.0);
}
}

void loop()
{
Serial.println(HBM());
}[/code]

My version of Arduino is 1.6.5.
My Arduino is an Arduino Uno.

I tried to write this code in my BMSerial.h file, because it’s in the SoftwareSerial.h : "using Print::write;"
I don’t have errors when I compile with this, but it doesn’t work.

In fact, I just want to use the BMSerial write function as it is possible with SoftwareSerial.

Thank you for your help !


#7

Can you be more specific when you say “it doesn’t work” (e.g. the code does not compile, there is no response from the sensor)? It is difficult to help you without knowing what the problem you are seeing with the code is. Also, what sensor are you using? It would be helpful for you to post a link to its datasheet or user’s guide.

You could try sending the “MSV?\r\n” command byte by byte using the BMSerial library’s write method. From your most recent code, it looks like you tried to do that, however you are missing ‘\r’ and ‘\n’ in your byte series command to your sensor. Define Poids as a BMSerial class type and try the following segment of code:

   Poids.write('M');
   Poids.write('S');
   Poids.write('V');
   Poids.write('?');
   Poids.write('\r');
   Poids.write('\n');

- Amanda


#8

Thank you Amanda for your reply.

I tried to use this code :

Poids.write('M'); Poids.write('S'); Poids.write('V'); Poids.write('?'); Poids.write('\r'); Poids.write('\n');

But nothing happens and my sensor doesn’t give me a data.
My sensor is HBM PW6CC3MR rely with a AD104C transducer.
Please find enclosed a data sheet of the AD104C.
ad104c.pdf (423 KB)


#9

Has your sensor ever worked before with your Arduino Uno R3? It looks like, from the datasheet, your sensor communicates via RS-232 and RS-485 serial signals. RS-232 and RS-485 use + and - voltages that are not appropriate for direct connection to the TTL voltage levels used by an Arduino. (You can read this RS-232 vs. TTL Serial Communication tutorial to better understand their differences. You can search on the Internet to compare RS-485 to RS-232 and TTL serial standards.) You might consider using something like this RS-232 to TTL serial converter if you do not have one already to translate the signals between your sensor and Arduino.

If you try using a converter between your Arduino and sensor and still get no response from your sensor, can you simplify your code down to the simplest possible thing that should work but does not, and post the entire code here?

  • Amanda