Hi and welcome to the wonderful world of Arduino. It appears that you have picked-up Arduino with ease. Your program is almost there. Your wiring connection diagram looks to be correct.
When I am doing a project like this, I won't write my full application first. Instead I write a series of test programs to make sure each chunk is performing exactly as I want it to.
I would start by writing a program that just passes the serial information recieved from the Sound Level Meter and displays it on the serial monitor. Something similar to this:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup()
{
// Open USB serial communications
Serial.begin( 2400 );
Serial.println("USB Serial is up and running");
// set the data rate for the SoftwareSerial port
mySerial.begin( 2400 );
}
void loop()
{
if ( mySerial.available() )
{
// Write the serial from the S.L.M. to the USB (debug) serial port
Serial.write( mySerial.read() );
}
if ( Serial.available() )
{
// Write the serial from the USB (debug) serial port to the S.L.M.
mySerial.write( Serial.read() );
}
}
This program should be enough to communicate with the Sound Level Meter. But this leads me to my first concern - choice of serial monitor.
While the Arduino built in serial monitor is adequate it is only really good for sending/recieving text. (Plus it sends a CR/LF every time you hit enter.) I can see straight away that you require to send/recieve hex numbers. Hex numbers won't display well on the serial monitor.
Instead I prefer to use a terminal program by "der-hammer" called hterm. (Screenshot here). It lets you view the input in ASCII, hex, bin and decimal. Likewise it lets you send ASCII, hex, bin and decimal or a mixture of each. When sending information, you can configure it so that it finishes the data with either a line feed, a carriage return, both or none.
By the sound of it, you don't want a CR or LF at the end of the data you send. So in the "Input Options" set the "Send on enter" to "None".
For the recieved data, you need to view that in hex so make sure the "hex" box is checked just above the recieved data. (Uncheck "ASCII".)
You could create a loop-back test by removing the Sound Level Meter and instead link the RS232 Rx to the RS232 Tx. This will allow you to see on hterm exactly what the Sound Level Meter sees when you send data.
Issues with your program
if (mySerial.read() == 0x10) {
//mySerial.write(0x20);
Serial.println("Data available");
Serial.println(mySerial.read());
}
The section of code will only read one byte of data after it has detected 0x10
(See the Serial.Read() reference page). Anything more than that will get consumed by the next time the if
statement checks the serial line for 0x10
.
I notice in the line Serial.println(mySerial.read());
that you are reading the data in (which is in hex format) but then trying to write it out in ASCII by using the println()
function. This will not give you the output you desire.
Also, after detecting 0x10
it reads the software serial line for one more byte - meaning if there is nothing following the 0x10
then the mySerial.Read()
command will hang for 1s until it timeouts.
if (Serial.available()) {
mySerial.write(Serial.read());
}
As others have pointed out, using the serial monitor will send this as ASCII characters (and include a CR/LF character(s)) instead of the hex value that you want. Kudos is given to you, that you chose mySerial.write()
instead of mySerial.println()
.
My Take on Your Program
The follwoing code is untested.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(2400);
// The wait for serial port to connect is not needed for Arduino Uno
Serial.println("USB Serial is up and running");
// set the data rate for the SoftwareSerial port
mySerial.begin(2400);
}
void loop()
{
if ( mySerial.available() )
{
int recievedByte = mySerial.read();
Serial.write(recievedByte); // Send to USB serial - assuming using hterm in hex
if ( recievedByte == 0x10 )
{
mySerial.write(0x20);
}
}
// You could remove the following code - data must be in hex.
if ( Serial.available() )
{
mySerial.write(Serial.read());
}
}