Arduino Mega - sending commands via RS232

Hello,

I'm using Arduino Mega and I'm trying to communicate with device for measuring temperature, wind speed etc. via RS232. It's Hot wire anemometer TMA-21HW, which has USB mini port and I also found RS232 protocol in manual.
I made this connection: Anemometer -> USB cable -> adapter USB to RS232 -> RS232 to TTL adapter (MAX3232) -> Arduino
I tried following code:

void setup()
{
   Serial.begin(9600);   
   Serial2.begin(19200);  
}

void loop()
{
   byte n = Serial2.available();
   if(n != 0)
   {
       char x = Serial2.read();  
       Serial.print(x);                
   }
}

but I'm getting random numbers on Serial monitor..I also add to the code "Serial2.write("A") to send command "A" to the device and start communication but it didn't work. I'm new to stuff like this and I'm not sure if the code is problem or connection...I checked other posts on this forum and others but still nothing..

Wiring from RS232-TTL (MAX3232) adapter to Arduino is:
VCC -> 5V
GND -> GND
RXD -> TX2
TXD -> RX2

I also tried switching RX and TX between adapters and to Arduino but nothing works.

Here is RS232 protocol that I found:



Thank you for your help!


RS232_5

hi @Janii welcome to the forum.

I can't give specific sdvice just now, but a trick that can cut your work down a bit might help.

Figure out all the various settings for the "conversation" and see who says what to whom…

...all without involving any Arduino or code you wrote.

Do this using a terminal emulator like PuTTY or CoolTerm running on your desktop machine.

Use the same common sense that got you this far, it seems, and hook the device to be controlled directly to your desktop machine, and try, then succeed, to establish a connection where you can type stuff and see the responses.

This will make experiments go way faster. Baud rate, stop bits, line endings and so forth can all be tweaked just by playing around in CoolTerm (my pick) or PuTTY.

Once you have a working ability to talk and listen, and have run it through some good fraction of its paces, transfer that knowledge to an Arduino sketch just as simple as you can make it, like the one you have, and see if it doesn't also… work.

Then you can start having a better kind of fun.

HTH

a7

1 Like

Thank you for your advice :slight_smile: I know CoolTerm so I'm trying it right now...All I'm getting is something like this, but when I'm moving with the device it's changing so maybe it's sending some values about wind speed...or something else.

obrázok

did you adjust the terminal-software to baudrate 19200 8N1?
Do you have a 8 ch 24 MHz logic analyser?

Yes, I did...if you mean this:

No, I don't

change the terminal software to show hexadecimal values to see if you can find the values "02" and "03" which are the first and the last byte

bytes 2 to 26 are for driving some kind of LCD which seem to be NON-ASCII-coded

with these bytes it makes no sense to show them as ASCII-characters

Yes!

The device looks like this:
obrázok
On the LCD display it shows measured data..so I'm thinking if I "pick" right bytes, or those what I need and display them in correct format..It could work (maybe)

EDIT: Okay, on Byte 27,28 I can see right value of wind speed

So the next step is to read the serial input basics tutorial

which has a demo-code that shows how to use start- and endmarkers
which in your case are the "02" and the "03"
to receive the characters into an array and then you can pickout the bytes for further processing the data

1 Like

Thank you very much! I will try it and post results

Have you had any luck or tried sending commands to the device?

Does that 33 byte packet just keep coming unsolicited?

a7

I tried only the "B" command which lights up the display of the anemometer (it worked). I'm not sure if I will use commands (if I don't have to) to get those 2 or 3 values that I need. Maybe the way to do this is read all 33 bytes, save them and then pick those which I need...I will try both ways
It's almost midnight here so I will continue tomorrow and let you know how it went

OK, THX. good luck, though luck should not enter into it.

There seem to be other 02 bytes, but the 03 start byte may be unique.

I wonder if waiting to see 02, then 03 next woukd be enough to sync you up, then you can receive and count bytes ignoring them until you are going to receive bytes for the real data, at which point you could stash them in your variables.

The link to Robin2's tutorial would have worn out long ago if links did that. Wear out from so much use. :wink:

a7

Okay, so I read the tutorial, tried those codes a few times and then changed it to to this:

const byte numBytes = 33;
byte receivedBytes[numBytes];   

boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial2.begin(19200);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvWithEndMarker();
    showNewData();
}

void recvWithEndMarker() {
    static byte ndx = 0;
    const byte endMarker = 0x03;
    byte rc;
  
    if (Serial2.available() > 0) {
        rc = Serial2.read();

        if (rc != endMarker) {
            receivedBytes[ndx] = rc;
            ndx++;
            if (ndx >= numBytes) {
                ndx = numBytes - 1;
            }
        }
        else {
            receivedBytes[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        for (byte n = 0; n < numBytes; n++) {
            Serial.print(receivedBytes[n], HEX);
            Serial.print(", ");
        }
        Serial.println();
        newData = false;
    }
}

But all I get is: Arduino is ready
I'm wondering...maybe I'm wrong with the way how I define the endMarker when it's 03 ...I tried to write it also in a different way like "03", 03...

What do you think about the code?

EDIT: The USB cabel that goes from anemometer to reduction USB-RS232 is USB 2.0 and the reduction is USB 1.1 ...could it be problem? Between those two I also have reduction with 2 female USB ports so I can connect the USB cabel from anemometer and USB-RS232 reduction. Right know I had to connect it like this because I had no other choice

I'll look closer when I get to the lab.

Your franken-cable might be a problem, and you might be able to test it.

Connect the USB end which now goes to the device to you desktop machine.

At the distal end, where I think you have serial TTL RX and TX, wire TX to RX.

CoolTerm it up, and see if characters you send out come right back undamaged.

Obvsly until that works, nothing will.

a7

I have this one:


and from that I go to adapter RS232-TTL:
obrázok

I connected the USB-RS232 cabel to the notebook but CoolTerm didn't even find it..it says that there is no available serial port...So I think a will have to buy one with USB 2.0

Not necessarily. You may only be missing a driver for whatever in the final cable is doing the conversion.

But that may not be the next best step, driver diving.

We can probably test this in a different way.

Please hand draw the entire franken-cable that you are trying to use to go between the anemometer and the Arduino. Note any USB version, although I do not think that is a problem.

My understanding. Is wrong as you have stated you are gender bending the USB. Which seems odd. I've got everything you can think of around the lab and no USB gender correcting capability. Never needed it.

a7

Yes, the problem is driver..the cable is working ok.

Okay, so the connections look like this:

The reduction I use to connect USB male - USB male:

Maybe I'm missing something here, but where is the USB host device in this chain? I've used the USB-RS232 cable from post #16 in the past, but it needs a USB host (like a PC) to tell it what to do.

Take care for the data lines are connected per USB cable too.