Problem with entering ASCII code using Serial Monitor

Hi,
I recently encountered a problem with entering ASCII to a serial monitor to test a ID-12 RFID tag reader which i got from this website http://playground.arduino.cc/Code/ID12.

// RFID reader ID-12 for Arduino 
// Based on code by BARRAGAN <http://people.interaction-ivrea.it/h.barragan> 
// and code from HC Gilje - http://hcgilje.wordpress.com/resources/rfid_id12_tagreader/
// Modified for Arduino by djmatic
// Modified for ID-12 and checksum by Martijn The - http://www.martijnthe.nl/
//
// Use the drawings from HC Gilje to wire up the ID-12.
// Remark: disconnect the rx serial wire to the ID-12 when uploading the sketch


void setup() {
  Serial.begin(9600);                                 // connect to the serial port
}

void loop () {
  byte i = 0;
  byte val = 0;
  byte code[6];
  byte checksum = 0;
  byte bytesread = 0;
  byte tempbyte = 0;

  if(Serial.available() > 0) {
    if((val = Serial.read()) == 2) {                  // check for header 
      bytesread = 0; 
      while (bytesread < 12) {                        // read 10 digit code + 2 digit checksum
        if( Serial.available() > 0) { 
          val = Serial.read();
          if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { // if header or stop bytes before the 10 digit reading 
            break;                                    // stop reading
          }

          // Do Ascii/Hex conversion:
          if ((val >= '0') && (val <= '9')) {
            val = val - '0';
          } else if ((val >= 'A') && (val <= 'F')) {
            val = 10 + val - 'A';
          }

          // Every two hex-digits, add byte to code:
          if (bytesread & 1 == 1) {
            // make some space for this hex-digit by
            // shifting the previous hex-digit with 4 bits to the left:
            code[bytesread >> 1] = (val | (tempbyte << 4));

            if (bytesread >> 1 != 5) {                // If we're at the checksum byte,
              checksum ^= code[bytesread >> 1];       // Calculate the checksum... (XOR)
            };
          } else {
            tempbyte = val;                           // Store the first hex digit first...
          };

          bytesread++;                                // ready to read next digit
        } 
      } 

      // Output to Serial:

      if (bytesread == 12) {                          // if 12 digit read is complete
        Serial.print("5-byte code: ");
        for (i=0; i<5; i++) {
          if (code[i] < 16) Serial.print("0");
          Serial.print(code[i], HEX);
          Serial.print(" ");
        }
        Serial.println();

        Serial.print("Checksum: ");
        Serial.print(code[5], HEX);
        Serial.println(code[5] == checksum ? " -- passed." : " -- error.");
        Serial.println();
      }

      bytesread = 0;
    }
  }
}

A few days back, i figured out i could just bypass the RFID by typing the RFID card id into the Serial Monitor to test the code. So I use an ASCII to Hex converter i found on the website http://www.asciitohex.com/. The HEX code im trying to convert is

02 0C 00 06 A5 21 8E 0D 0A 03

I manage to convert the code to ASCII, and when i entered in the Serial Monitor, the arduino responded.
It was fine last week, but now the website doesnt translate to any ASCII character (empty). I tried using other ASCII converter on other website http://www.rapidtables.com/convert/number/hex-to-ascii.htm but it just convert into this box character : ¥!Ž

The first few time I was using the converter, there were NO box like that appearing.Now i cant get the sketch to work without the proper ASCII code to type on the serial monitor. Then, I tried using the website converter on my desktop at home, the converter was able to convert it without any “box” but i cant seem to get it to work on my laptop.

Then i came across an arduino example : ASCIITable
I change the initialize of thisByte to 0 instead of the original 33

// first visible ASCIIcharacter '!' is number 33:
int thisByte = 0;

Then I ran the sketch on my arduino. Below is the result from my Serial Monitor:

Question: Is it suppose to produce that box for that HEX code, cause it seem all of them are converted into the same “box”. I still havent yet tried this example on my desktop at home
I have tried searching for a solution and answer to my problem for a few days now and nothing i find and do is solving my problem and I was hoping I could get an answer here.
I’m sorry if i posted this in the wrong section of the forum.
Thank You

Then I ran the sketch on my arduino. Below is the result from my Serial Monitor:

Which sketch? The one that you provided for the reader? That sketch will print the numeric values of characters (so 02, 0C etc) so should not display funny characters.

If it’s another sketch, please provide that sketch.

The serial monitor is limited in what it can display; non-printable characters (most of the ones before 0x20) will give ‘strange’ results.

My reference for the ascii table: http://www.asciitable.com/

Hello, thank you for replying
This is the sketch i was running, its from Arduino>Example>Communication>ASCII table:

/*
  ASCII table

 Prints out byte values in all possible formats:
 * as raw binary values
 * as ASCII-encoded decimal, hex, octal, and binary values

 For more on ASCII, see http://www.asciitable.com and http://en.wikipedia.org/wiki/ASCII

 The circuit:  No external hardware needed.

 created 2006
 by Nicholas Zambetti
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 <http://www.zambetti.com>

 */
void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // prints title with ending line break
  Serial.println("ASCII Table ~ Character Map");
}

// first visible ASCIIcharacter '!' is number 33:
int thisByte = 0;
// you can also write ASCII characters in single quotes.
// for example. '!' is the same as 33, so you could also use this:
//int thisByte = '!';

void loop() {
  // prints value unaltered, i.e. the raw binary version of the
  // byte. The serial monitor interprets all bytes as
  // ASCII, so 33, the first number,  will show up as '!'
  Serial.write(thisByte);

  Serial.print(", dec: ");
  // prints value as string as an ASCII-encoded decimal (base 10).
  // Decimal is the  default format for Serial.print() and Serial.println(),
  // so no modifier is needed:
  Serial.print(thisByte);
  // But you can declare the modifier for decimal if you want to.
  //this also works if you uncomment it:

  // Serial.print(thisByte, DEC);


  Serial.print(", hex: ");
  // prints value as string in hexadecimal (base 16):
  Serial.print(thisByte, HEX);

  Serial.print(", oct: ");
  // prints value as string in octal (base 8);
  Serial.print(thisByte, OCT);

  Serial.print(", bin: ");
  // prints value as string in binary (base 2)
  // also prints ending line break:
  Serial.println(thisByte, BIN);

  // if printed last visible character '~' or 126, stop:
  if (thisByte == 126) {    // you could also use if (thisByte == '~') {
    // This loop loops forever and does nothing
    while (true) {
      continue;
    }
  }
  // go on to the next character
  thisByte++;
}

I tried running ASCII table sketch on my desktop and this is the result:
ASCII table on Desktop
While the result i get on my laptop is:
ASCII table laptop

My desktop is running on Windows 7 Home Premium while the laptop is running on Windows 10.

My reason for testing the RFID is to check its checksum code. I’m making a slave master communication for a panic button system. The slave arduino will be connected to the panic button while the master arduino is connected to the LED corresponding to the location of the panic button that is push. The communication is through serial com. Instead of just sending the number which button is pressed, I would like to wrapped it in with checksum code. This is the code for the slave ardunio:

//  Prepared by: 
//  Date       : 22/11/2016
//  Company    : 

  int Out[5][6]={
  {2,3,4,5,6,7},
  {22,23,24,25,26,27},
  {28,29,30,31,32,33},
  {34,35,36,37,38,39},
  {40,41,42,43,44,45}};  //Button pin
  
  int Catch[5][6];  //Place to store state of button
  byte data[6];     //Data to be send over serial
  char flag;
  byte chksum;
  byte pack[10]={0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x0D,0x0A,0x03};  // <<Hex
  //              0    1    2    3    4    5   6(CS)    7    8    9
  // packet structure, see ID-12 datasheet
  // STX AB CD EF GH IJ CS CR LF ETX
  //   2 -- -- -- -- -- -- 13 10 3    <<Decimal
  
void setup() {
  Serial.begin(9600);
  pinMode(2,INPUT_PULLUP);
  pinMode(3,INPUT_PULLUP);
  pinMode(4,INPUT_PULLUP);
  pinMode(5,INPUT_PULLUP);
  pinMode(6,INPUT_PULLUP);
  pinMode(7,INPUT_PULLUP);
  
//  pinMode(22,INPUT_PULLUP);
//  pinMode(23,INPUT_PULLUP);
//  pinMode(24,INPUT_PULLUP);
//  pinMode(25,INPUT_PULLUP);
//  pinMode(26,INPUT_PULLUP);
//  pinMode(27,INPUT_PULLUP);
//  
//  pinMode(28,INPUT_PULLUP);
//  pinMode(29,INPUT_PULLUP);
//  pinMode(30,INPUT_PULLUP);
//  pinMode(31,INPUT_PULLUP);
//  pinMode(32,INPUT_PULLUP);
//  pinMode(33,INPUT_PULLUP);
//  
//  pinMode(34,INPUT_PULLUP);
//  pinMode(35,INPUT_PULLUP);
//  pinMode(36,INPUT_PULLUP);
//  pinMode(37,INPUT_PULLUP);
//  pinMode(38,INPUT_PULLUP);
//  pinMode(39,INPUT_PULLUP);
//  
//  pinMode(40,INPUT_PULLUP);
//  pinMode(41,INPUT_PULLUP);
//  pinMode(42,INPUT_PULLUP);
//  pinMode(43,INPUT_PULLUP);
//  pinMode(44,INPUT_PULLUP);
//  pinMode(45,INPUT_PULLUP);

}

void loop() 
{
  if(Serial.available()>0)
  {
  flag=Serial.read();
  if (flag=='a')
  {
      for(int i=0; i<5; i++)
      {   
        for(int j=0; j<6; j++)
        {
          Catch[i][j]=digitalRead(Out[i][j]);     //Read all panic button status, store to array Catch
        }
      }
      delay(10);
      for(int i=0; i<5; i++)
      {
        for(int j=0; j<6; j++)
        {
          if(Catch[i][j]==LOW)     //Check if any button is pressed (LOW)
          {
            bitWrite(data[i],j,1);
          }
          else bitWrite(data[i],j,0);
        }
      }
      
      for (int i=0; i<5; i++)
      { chksum ^= data[i];
      }
      
      pack[6]=chksum;
      
      for (int i=1; i<6; i++)
      { pack[i]=data[i];
      }

      for (int i=0; i<10; i++)
      { 
//        Serial.write(pack[i]);
          Serial.println(pack[i]);
          delay(500);
      }   
  }  
  }
}

Continue next post

The state of each panic button is put in an array of variable “out[5][6]”, where 5 represent the floor and 6 represent the panic button which is pushed (6 button per floor). I will then combine this with variable “pack” which include the [STX] [data[5]] [checksum] [CR] [LF] [ETX] and send over serial com. To make sure the master arduino respond correctly, i will make a dummy code with its corresponding checksum (using Exclusive OR calculation) and type it over serial monitor to be sen

And this is the code for master arduino:

//  Prepared by:
//  Date       : 22/11/2016
//  Company    :
  
  
  int offset = 200;
  int NEWTIME;
  byte bytesread=0;
  byte data[6];
  byte chksum=0;
  byte val=0;
  int Catch [5][6]; 
  int Out[5][6]={
  {2,3,4,5,6,7},
  {22,23,24,25,26,27},
  {28,29,30,31,32,33},
  {34,35,36,37,38,39},
  {40,41,42,43,44,45}};
 

  int buzzer = 8;
  int strobe = 9;
  int buzzer_flag;
  
void setup() {
  Serial.begin(9600);
  pinMode(buzzer,OUTPUT);
  pinMode(strobe, OUTPUT);
  
  pinMode(13,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  
  pinMode(22,OUTPUT);
  pinMode(23,OUTPUT);
  pinMode(24,OUTPUT);
  pinMode(25,OUTPUT);
  pinMode(26,OUTPUT);
  pinMode(27,OUTPUT);
  
  pinMode(28,OUTPUT);
  pinMode(29,OUTPUT);
  pinMode(30,OUTPUT);
  pinMode(31,OUTPUT);
  pinMode(32,OUTPUT);
  pinMode(33,OUTPUT);
  
  pinMode(34,OUTPUT);
  pinMode(35,OUTPUT);
  pinMode(36,OUTPUT);
  pinMode(37,OUTPUT);
  pinMode(38,OUTPUT);
  pinMode(39,OUTPUT);
  
  pinMode(40,OUTPUT);
  pinMode(41,OUTPUT);
  pinMode(42,OUTPUT);
  pinMode(43,OUTPUT);
  pinMode(44,OUTPUT);
  pinMode(45,OUTPUT);
  

}

void loop(){
  Serial.write('a');
  delay(500);
  if(Serial.available()>0)
  {   if((val = Serial.read()) == 2)  // check for header 
      {   bytesread = 0; 
          while (bytesread < 12) 
          { if( Serial.available()) 
            { val = Serial.read();
            Serial.print("Val: ");
              Serial.println(val,HEX);
              if((val == 0x0D)||(val == 0x0A)||(val == 0x03) || (val == 0x02))  // if header or stop bytes before the 10 digit reading 
              { Serial.println("Dumped");
                break;                                    // stop reading
              }
              
              if ((val >= '0') && (val <= '9')) {
                val = val - '0';
                Serial.print("Val: ");
                Serial.println(val);
              } else if ((val >= 'A') && (val <= 'F')) {
                val = 10 + val - 'A';
                Serial.print("Val: ");
                Serial.println(val);
              }
              data[bytesread]=val;
             if (bytesread > 0 && bytesread <6)
              { chksum ^= val;    
            }
              Serial.print("Bytesread: ");
              Serial.println(bytesread);
              bytesread++;
            }
            
          }
          
       }
       
  }
  if (data[5] == chksum)
  {   data_process();
      delay(100);
  }
  
  else
  { Serial.print("Failed");
  }
}  
  

void data_process()
{
  buzzer_flag=0;
  for (int i=0; i<6; i++){
  Serial.print(data[i]);
  }
  Serial.println(' ');
  for(int i=0; i<5; i++){
    for(int j=0; j<6; j++){
      Catch[i][j]=bitRead(data[i], j);
    }
  }  
  for(int i=0; i<5; i++){
    for(int j=0; j<6; j++){
      if(Catch[i][j]==0)
      {
        digitalWrite(Out[i][j],HIGH); 
        Buzz_Beep();
      }
        else 
        {
          digitalWrite(Out[i][j],LOW);
          digitalWrite(buzzer, LOW);
        }
    }
  }


}

void Buzz_Beep()
{
  digitalWrite(buzzer, HIGH);
  delay(100);
  digitalWrite(buzzer, LOW);
  delay(100);
}

I do notice that even though its not shown by the serial monitor the ASCII code, it is still recognize by the sketch while the “box” is not recognize.

This is the result is get from the website http://www.rapidtables.com/convert/number/hex-to-ascii.htm for the code on my desktop:
02 0C 00 06 A5 21 8E OD OA 03
¥!Ž

While the result from the laptop is

I am confuse that this problem doesnt pop up until a few days later after i tried the method.

I am sorry for the lengthy explanation and I hope i could find the solution to this problem

The ¥! is 0xA5 followed by 0x21. I suspect that your code goes out-of-sync because you don't read a complete message (you break on CR or LF or STX or ETX) and you assume that all bytes will be available in time.

What happens if you e.g. get 6 bytes followed by 4 bytes? You break from the while and have garbage.

Ps what is the reader giving you? Binary or ascii?

Hello, thank you. I should have mention that i dont have any reader, and just merely sending the ASCII code that i translated from the website to serial monitor thinking i could bypass the hardware and just test the sketch without the hardware, since last time its the only thing that gets the code to run past this point:

Serial.println(code[5] == checksum ? " -- passed." : " -- error.");

I got the code from this datasheet on page 4: Datasheet

I thought this method would work. But later I'm unable to repeat the same result because I cant get the right ASCII code to enter on the serial monitor.

The RFID sketch isnt the one i was working on, its just merely to grasp and know what kind of data is send over serial com to the arduino from the RFID reader, so that I could test it on my master side of my Panic Button Project.

I'm open to any suggestion I could use to checksum the data send from slave arduino (Panic Button) to master arduino (LED) is valid and not cause by any fault in the circuit that could triggered the slave to send false, inaccurate alarm to the master. Thank You.

EDIT: Manage to get it to work, but the ASCII i entered and the 1 displayed on Serial Monitor is different Thank You.

Hello, thank you for the support and help. I think I may have found a solution to my problem. Instead of thinking about those unseen ASCII character, i should have just hook up both my slave and master arduino together on different serial line and test my sketch. Since Arduino Mega have more than 1 serial com, I am able verify the code send by the slave to the master is equal. Thank You

Just a follow up

You can simulate the data using serial monitor, but you need a different approach (and don’t need the ascii table).

Just send the characters 02 0C 00 06 A5 21 8E OD OA 03 (without spacing).

Collect two characters and convert them to a byte that they represent. In simplistic for

for(int cnt=0;cnt<10;cnt++)
{
  char tmp[3];
  char *endptr;
  tmp[0] = serialRead();
  tmp[1] = SerialRead();
  tmp[2] = '\0';
  data[cnt]=strtol(tmp, &endptr, 16);
}

This will collect two received characters in the tmp array and next convert and store in data. See man strtol or do search for strtol examples.

A far simpler way would be to use a real terminal program (e.g. RealTerm) that supports sending binary data. Type 0x02 and send, type 0x0C and send; etc. It also allows to send binary data from a file; use a hex editor (e.g. frhed) to create the file.

This assumes you’re using Windows; find the equivalent for other operating systems.