user86067: I am using an Arduino Leonardo and the DFrobot RS232 shield to try and communicate with a flux meter. I am trying to write "UNITS?", which would have the fluxmeter send back a single byte that would translate to which units it is using.
The problem is, I can see from the Arduino side it is writing those 6 bytes, but at the if (Serial1.available()), there is nothing there. I have the switch on the shield to on, and I did the tutorial on the DFrobot wiki to make sure my shield works.
I think it has to do with how the Serial1.write() is done. I don't think it is receiving the command correctly, so it does not respond.
Shown below is my code:
#include <Keyboard.h>

// Define pins, so you only need to change them in one spot
#define BUTTON 7
          
bool runner = 0;

void setup() {
  // Begin Serial communication at 9600 baud rate
  Serial1.begin(9600);
  pinMode(BUTTON, INPUT);
  // create an interrupt that will call send_command() whenever the button pin has a rising edge
  attachInterrupt(digitalPinToInterrupt(BUTTON), send_command, RISING);
  // start the Keyboard process
  Keyboard.begin();
}

// called when button is clicked
// will use Serial.write() to send commands
// then will write the data with the keyboard
void send_command() {
  runner = 1;
  return;
}

void loop() {
  if (runner) {
    char pkneg, pkpos;
    // This next section writes the commands to the 480,
    // then reads the data and prints it to the Serial Monitor
    int sent = Serial1.write("UNITS?");
    Keyboard.println(sent);
    if (sent <= 0) {
      Keyboard.print("Fort Myers we got a problem\n");
    }
    //Keyboard.print(marker);
    delay(5000);

    if (Serial1.available()) {
      Keyboard.print("Here!\n");
      pkpos = Serial1.read();
  }
  else pkpos = 'x';
  pkneg = 'x';

  char pkneg1[10], pkpos1[10];
  // dtostrf(pkpos, 4, 3, pkpos1);
  // dtostrf(pkneg, 4, 3, pkneg1);

  Keyboard.print(pkneg);
  Keyboard.print('\t');
  Keyboard.print(pkpos);
  Keyboard.print('\n');

  // for testing

  runner =0;
  }
}


user4143: You are lacking the line terminators from your serial writing. As it says in the manual:


Terminators are fixed to CRLF.


And in the example code (the Quick Basic is probably the easiest to follow):
CMD$ = CMD$ + TERM$
PRINT #1, CMD$; 

(where TERM$ is "\r\n").
So changing your Serial1.write to include the terminators should help the device to recognize that you have finished sending the command and it should respond to it:
Serial1.write("UNITS?\r\n");

By the way, testing the return value of the Serial1.write is pointless. It will always return the number of characters in the string you supplied - serial data over the UART pins is always sent even if there is nothing there to receive it.

86067: <p>I am using an Arduino Leonardo and the DFrobot RS232 shield to try and communicate with a flux meter. I am trying to write &quot;UNITS?&quot;, which would have the fluxmeter send back a single byte that would translate to which units it is using.</p>
<p>The problem is, I can see from the Arduino side it is writing those 6 bytes, but at the <code>if (Serial1.available())</code>, there is nothing there. I have the switch on the shield to on, and I did the tutorial on the DFrobot wiki to make sure my shield works.</p>
<p>I think it has to do with how the <code>Serial1.write()</code> is done. I don't think it is receiving the command correctly, so it does not respond.</p>
<p>Shown below is my code:</p>
<pre class="lang-cpp prettyprint-override"><code>#include &lt;Keyboard.h&gt;

// Define pins, so you only need to change them in one spot
#define BUTTON 7
          
bool runner = 0;

void setup() {
  // Begin Serial communication at 9600 baud rate
  Serial1.begin(9600);
  pinMode(BUTTON, INPUT);
  // create an interrupt that will call send_command() whenever the button pin has a rising edge
  attachInterrupt(digitalPinToInterrupt(BUTTON), send_command, RISING);
  // start the Keyboard process
  Keyboard.begin();
}

// called when button is clicked
// will use Serial.write() to send commands
// then will write the data with the keyboard
void send_command() {
  runner = 1;
  return;
}

void loop() {
  if (runner) {
    char pkneg, pkpos;
    // This next section writes the commands to the 480,
    // then reads the data and prints it to the Serial Monitor
    int sent = Serial1.write(&quot;UNITS?&quot;);
    Keyboard.println(sent);
    if (sent &lt;= 0) {
      Keyboard.print(&quot;Fort Myers we got a problem\n&quot;);
    }
    //Keyboard.print(marker);
    delay(5000);

    if (Serial1.available()) {
      Keyboard.print(&quot;Here!\n&quot;);
      pkpos = Serial1.read();
  }
  else pkpos = 'x';
  pkneg = 'x';

  char pkneg1[10], pkpos1[10];
  // dtostrf(pkpos, 4, 3, pkpos1);
  // dtostrf(pkneg, 4, 3, pkneg1);

  Keyboard.print(pkneg);
  Keyboard.print('\t');
  Keyboard.print(pkpos);
  Keyboard.print('\n');

  // for testing

  runner =0;
  }
}
</code></pre>
 4143: <p>You are lacking the line terminators from your serial writing. As it says in the manual:</p>
<blockquote>
<ul>
<li>Terminators are fixed to CRLF.</li>
</ul>
</blockquote>
<p>And in the example code (the Quick Basic is probably the easiest to follow):</p>
<pre><code>CMD$ = CMD$ + TERM$
PRINT #1, CMD$; 
</code></pre>
<p>(where <code>TERM$</code> is &quot;\r\n&quot;).</p>
<p>So changing your <code>Serial1.write</code> to include the terminators should help the device to recognize that you have finished sending the command and it should respond to it:</p>
<pre><code>Serial1.write(&quot;UNITS?\r\n&quot;);
</code></pre>
<p>By the way, testing the return value of the <code>Serial1.write</code> is pointless. It will always return the number of characters in the string you supplied - serial data over the UART pins is always sent even if there is nothing there to receive it.</p>
