Serial1 print is working but serial1 write not working

on a 2560 my code outputs fine on serial1 print. basically 1's and 0's from digital inputs. but when i add the serial1 write there is not output from serial1. below is my code. this is connected via rs232 in a terminal window.


#define NUM_INPUTS 15  // Number of inputs

const int inputPins[NUM_INPUTS] = {22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36};

void setup() {
    Serial.begin(9600);   // Serial Monitor via USB
    Serial1.begin(9600);  // RS232 Output to AVL (via MAX3232)

    for (int i = 0; i < NUM_INPUTS; i++) {
        pinMode(inputPins[i], INPUT_PULLUP);
    }
}

void loop() {
    byte pinStates[NUM_INPUTS] = {0};  // Array to hold pin states
    String data = "INPUTS:";           // Start the message

    for (int i = 0; i < NUM_INPUTS; i++) {
        int sensorValue = digitalRead(inputPins[i]);
        pinStates[i] = !sensorValue;  // Invert the reading
        data += String(pinStates[i]); // Append state to string
        if (i < NUM_INPUTS - 1) data += ",";  // Add comma between values
    }

    Serial.println(data);           // Send to Serial Monitor (USB)
    Serial1.write(pinStates, NUM_INPUTS);  // Send binary data to AVL device via RS232

    delay(2000);  // Adjust as needed

Try printing the HEX values instead of write and see what is really being sent. Likely is is something the terminal cannot display.

You can easily verify for yourself that Serial1.write does in fact work by jumpering the Mega's Tx1 pin (18) to its Rx1 pin (19) and uploading the following sketch:

void setup() {
   char msg[] = "Hello, world!\r\n";
   
   Serial.begin(9600);
   Serial1.begin(9600);
   Serial1.write(msg, sizeof msg);
}

void loop() {
   if( Serial1.available() ) {
      char c = Serial1.read();
      Serial.write(c);
   }
}

Therefore logically your problem is not with Serial1.write but something else.

Also, while I have no doubt that Serial1.print also works, there is no Serial1.print call in your code to work or not.

1 Like

Yes, there is output. Connect the pin to an oscilloscope or logic analyser and you will be able to see it. But you can't see it on terminal window because the characters sent are not "printable" and so are ignored by the terminal window.

If you want the output to be visible on terminal window, you should be using Serial1.print(), not Serial1.write().

1 Like

Good idea.

Using that setup on this code works for me:

#define NUM_INPUTS 15  // Number of inputs

const int inputPins[NUM_INPUTS] = {22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36};

void setup() {
    Serial.begin(9600);   // Serial Monitor via USB
    Serial1.begin(9600);  // RS232 Output to AVL (via MAX3232)

    for (int i = 0; i < NUM_INPUTS; i++) {
        pinMode(inputPins[i], INPUT_PULLUP);
    }
}

void loop() {
    byte pinStates[NUM_INPUTS] = {0};  // Array to hold pin states
    String data = "INPUTS:";           // Start the message

    for (int i = 0; i < NUM_INPUTS; i++) {
        int sensorValue = digitalRead(inputPins[i]);
        pinStates[i] = !sensorValue;  // Invert the reading
        data += String(pinStates[i]); // Append state to string
        if (i < NUM_INPUTS - 1) data += ",";  // Add comma between values
    }

    Serial.println(data);           // Send to Serial Monitor (USB)
    Serial1.write(pinStates, NUM_INPUTS);  // Send binary data to AVL device via RS232
    
    while(Serial1.available()){
      Serial.print(Serial1.read());
    }

    delay(2000);  // Adjust as needed
}

I got:

INPUTS:1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
INPUTS:1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
100000000000000INPUTS:1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
100000000000000INPUTS:1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
100000000000000INPUTS:1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
100000000000000INPUTS:1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
100000000000000INPUTS:1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
100000000000000INPUTS:1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
100000000000000INPUTS:1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
100000000000000INPUTS:1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
100000000000000INPUTS:1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
100000000000000INPUTS:1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
100000000000000INPUTS:1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
100000000000000INPUTS:1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
100000000000000INPUTS:1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
100000000000000INPUTS:1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
100000000000000

(Note that I added a clause for printing out an echo, and that I also jumpered pin22 to GND to give a non-zero output)

2 Likes

Here is the output of Serial1 on an oscilloscope:
D25, D27 and D29 are connected to GND.

This is Serial1 and Serial:

and on a faster timebase:

2 Likes

I think the issue here is you are confusing these two:

Thing A:
β€˜0’, the character zero, which has an ASCII code of 48
Thing B:
The byte 0, which prints nothing on a terminal

Both of these are β€œzero”

The Print lends more toward you getting thing A

The Write gives you thing B because you are writing bytes that are later interpreted as ASCII for terminal printing. Only byte values 33 and up actually stand for printable characters. 0 and 1 will print nothing.

Try Writing the number 48. You will notice a zero gets printed in the terminal.

Then just work out a way to print what you are after, that properly takes into account the necessary formatting and conversion to ASCII, which print() does.

1 Like

thanks. I believe you are correct. when i do print i get what is expected. 1's and 0's. it was just the write i am confused. I wanted to see the byte values of my data. all i really have are two states. a switch on or off. The issue is that when i send the data to another device, my AVL(automatic vehicle locator) aka. vehicle tracking device which will wend the data via cellular to my server and i can view the switch states remotely . i dont see the message packet in he in the device when i hook it up to my terminal. hope that makes sense. thanks for your input

What you expect the "byte value" of the "1's and 0's" to be?

     The hexadecimal set:

     00 nul   01 soh   02 stx   03 etx   04 eot   05 enq   06 ack   07 bel
     08 bs    09 ht    0a nl    0b vt    0c np    0d cr    0e so    0f si
     10 dle   11 dc1   12 dc2   13 dc3   14 dc4   15 nak   16 syn   17 etb
     18 can   19 em    1a sub   1b esc   1c fs    1d gs    1e rs    1f us
     20 sp    21  !    22  "    23  #    24  $    25  %    26  &    27  '
     28  (    29  )    2a  *    2b  +    2c  ,    2d  -    2e  .    2f  /
     30  0    31  1    32  2    33  3    34  4    35  5    36  6    37  7
     38  8    39  9    3a  :    3b  ;    3c  <    3d  =    3e  >    3f  ?
     40  @    41  A    42  B    43  C    44  D    45  E    46  F    47  G
     48  H    49  I    4a  J    4b  K    4c  L    4d  M    4e  N    4f  O
     50  P    51  Q    52  R    53  S    54  T    55  U    56  V    57  W
     58  X    59  Y    5a  Z    5b  [    5c  \    5d  ]    5e  ^    5f  _
     60  `    61  a    62  b    63  c    64  d    65  e    66  f    67  g
     68  h    69  i    6a  j    6b  k    6c  l    6d  m    6e  n    6f  o
     70  p    71  q    72  r    73  s    74  t    75  u    76  v    77  w
     78  x    79  y    7a  z    7b  {    7c  |    7d  }    7e  ~    7f del

so i ended up changing the output to HEX and i think that will work for my parsing scripts on the server side where the data is being sent via the AVL. No im having problems with the avl recognizing the message from the arduino. the hex message is fine when i inspect it in a terminal program but the avl not seeing it. currently trying to figure that out. thanks for your suggestions

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.