Go Down

Topic: Sending serial commands to arduino with existing shield using the RX line (Read 1 time) previous topic - next topic

sjocorpe

Hi guys/girls

For the last few days Ive been trying to interface my Arduino with attached barcode reader shield to my computer. I'm trying to get it to activate the barcode reader remotely via the vb.net program that i have written.
I can receive the data scanned from the barcode reader plus any extra info that i want to send using Serial.write() but when i try to send data to the arduino either using my program or the serial monitor it does not receive it. It does how ever receive the data if the shield is disconnected.

Is there something that disconnects the RX line from the computer when the shield is plugged in and is there some was i can get around this in code?
Or will i have to come up with a physical system which is not a problem to do but I dont want to do this if it is not necessary.

Code waits for a Serial.available() i.e. input from the PC before starting.

Erdin

I can't find the barcode reader shield. Can you post a link to it ?

PaulS

Quote
Code waits for a Serial.available() i.e. input from the PC before starting.

What code does?

sjocorpe

This is a recreation of my code im currently unable to access my actual code.

Barcode Reader shield     http://arduino.com.au/Arduino-Shields-store.html

Code: [Select]

const int BeeperPin = 11;      // piezo beeper pin
byte serialin; // Serial data read in
byte linefeed=0; // for appending a linfeed so each barcode is on a new line
int Activate = 8;
 
void setup() // standard arduino setup initializing loop
{
  pinMode(BeeperPin, OUTPUT);   // initialize Beeper output
  Serial.begin(9600); // initialize the serial communication to 9600 baud
  Serial.println("Barcode Shield for Arduino v1.01"); // startup title
  Serial.println("www.arduino.com.au"); // website address
}

// Note that the barcode is aimed and triggered by a hardware switch
// this hardware switch has been connected to digital pin 8 to activate via code
void loop() // standard arduino loop
{
  if (Serial.available()) //signal from PC, problem is it does not register this due to the barcode shield. Works when the shield is disconnected
  {
    Serial.print("No Signal");
  Serial.print("\n\r");
  Serial.flush;
  digitalWrite(Activate, LOW);  //activates the barcode scanner
 
  while (Serial.available()) {   // check for data from barcode scanner
    serialin = Serial.read(); // read in barcode character at 9600 baud
    Serial.write(serialin);
    delay(1); // allow 1ms delay for next barcode character to be scanned in
    linefeed = 1; // set so that whole barcode has a linefeed appended
  }
 
  if (linefeed>0) { // barcode has been full received
    linefeed = 0;
    tone(BeeperPin,2500,20); // beep at 2.5kHz for 20ms   
    Serial.print("\n\r"); // send out a linefeed to separate barcodes
  }
  }
  else
  {
    Serial.print("No Signal");
  Serial.print("\n\r");
  }
}

Erdin

The RX and TX of the barcode shield are connected to the RX and TX of the board (pin 0 and 1).
In you sketch you have a mix of reading and writing to and from both the barcode shield and the serial monitor.
I think that mix is not going right.

You activate the barcode shield with pin 8, but is never deactivated.
There is also no pinMode() for pin 8, and it should be deactivated at start.

The received character is echoed back into the barcode scanner, I think you want it only to go to the serial monitor.

This would be too confusing for me. I would change the hardware.

  • You can rewire the RX and TX to other pins, and create a serial port on those pins with SoftwareSerial.

  • You can use an Arduino Leonardo. That board uses a software serial port for the USB bus (for the serial monitor on the PC) and has a normal hardware serial port on pin 0 and 1 that is not in use.



The delay of 1ms to wait for the next character is a little low.

After the Serial.flush(), you need to wait. I suggest 10ms wait.

The order for a new line is CarriageReturn-LineFeed, so it is "\r\n" and not "\n\r".

The code is going wrong.
Suppose it waits for the barcode scanner, if nothing is received, the loop() function is run from the start and that prints "No signal" to the barcode scanner.
If you want to continue with this code (please don't), make good indents and a good layout. You have already good comments. Seperate the blocks for the serial monitor and the barcode scanner very well.

Code: [Select]

// Example code, just a few pieces of code.

char buffer[40];

// --------------- RX, TX to Serial Monitor ------------------
digitalWrite(Activate, HIGH);  //activates the serial monitor
Serial.println("Barcode Shield for Arduino v1.01"); // startup title
Serial.flush();
delay(10);

// --------------- RX, TX to Barcode Scanner ------------------
digitalWrite(Activate, LOW);  //activates the barcode scanner
if( Serial.available() > 0)
{
  delay( 100);    // wait for all the characters to receive
  int i;
  while (Serial.available()) {
    buffer[i++] = Serial.read();
  }
  buffer[i] = '\0';           // terminate string for safety.
}

// --------------- RX, TX to Serial Monitor ------------------
digitalWrite(Activate, HIGH);  //activates the serial monitor
Serial.print("Code : ");
Serail.println(buffer);


PaulS

Code: [Select]
Serial.flush();
Why? The Serial.print() and Serial.write() functions will automatically block until there is room in the buffer for the data that they want to add. Why wait until the buffer is empty?

There are VERY few cases where flush() does anything useful.

sjocorpe

This is the actual code that im using for this the previous was just a quick recreation i made as i did not have the code on hand.
This code has been adapted from code provided by the suppliers of the barcode scanner shield

Code: [Select]

const int BeeperPin = 11;      // piezo beeper pin
byte serialin; // Serial data read in
byte linefeed=0; // for appending a linfeed so each barcode is on a new line
int Activate = 8;  //Used to activate scanner programically, Active LOW
int pos = 0;
int Trigger = 10;
int ManTrig = 12;
byte Run = 0;


void setup() // standard arduino setup initializing loop
{
 pinMode(Activate,OUTPUT);
 digitalWrite(Activate, HIGH);
 pinMode(Trigger,INPUT);
 pinMode(ManTrig,INPUT);
 pinMode(BeeperPin, OUTPUT);   // initialize Beeper output
 tone(BeeperPin,2500,50); // beep at 2.5kHz for 20ms    
 delay(100);
 tone(BeeperPin,2500,50); // beep at 2.5kHz for 20ms    
 Serial.begin(9600); // initialize the serial communication to 9600 baud
 Serial.println("Barcode Shield for Arduino v1.02"); // startup title
 Serial.println("www.arduino.com.au"); // website address
 
}

// Note that the barcode is aimed and triggered by a hardware switch
// this hardware switch has been connected to digital pin 8 to activate via code
void loop() // standard arduino loop
{
 if (Serial.available())    // checking for input from PC
 {
   Run = 1;
   Serial.write("Serial Signal");  //sent to PC
   Serial.print("\n\r");
   serialin = Serial.read(); //clears the PC input
 }
 else if (digitalRead(Trigger) == HIGH)
 {
   Run = 1;
   Serial.write("Trigger Signal");  //sent to PC
   Serial.print("\n\r");
 }
 else if (digitalRead(ManTrig) == HIGH)
 {
   Run = 1;
   Serial.write("Manual Signal");  //sent to PC
   Serial.print("\n\r");
   tone(BeeperPin,2000,20); // beep at 2.5kHz for 20ms  
 }
 else
 {
   Run = 0;
 }
 if (Run == 1)
 {


   digitalWrite(Activate, LOW);   //activate scanner
   delay(2000);
   digitalWrite(Activate, HIGH);   //deactivate scanner

   while (Serial.available()) {   // check for data from barcode scanner
     serialin = Serial.read(); // read in barcode character at 9600 baud
     Serial.write(serialin);  //sent to PC
     delay(10); // allow 1ms delay for next barcode character to be scanned in
     linefeed = 1; // set so that whole barcode has a linefeed appended
   }

   if (linefeed>0) { // barcode has been full received
     linefeed = 0;
     tone(BeeperPin,2000,20); // beep at 2.5kHz for 20ms    
     Serial.print("\n\r"); // send out a linefeed to separate barcodes
   }
   delay(1000);
 
}
else
{
Serial.write("No Signal");
Serial.print("\n\r");
}
delay(1000);
}

PaulS

Code: [Select]
    Serial.write("Serial Signal");  //sent to PC
    Serial.print("\n\r");

Why?

Code: [Select]
    Serial.println(F("Serial Signal"));  //sent to PC

Code: [Select]
    while (Serial.available()) {   // check for data from barcode scanner
Which is it? Is the hardware serial port being used to talk to the PC OR to talk to the barcode scanner? Both is the wrong answer.

sjocorpe

It was doing it for both, which is why I was asking if it was possible, and if there is a in code method that can be implemented to do receive from both the barcode reader and the PC.

Erdin


It was doing it for both, which is why I was asking if it was possible, and if there is a in code method that can be implemented to do receive from both the barcode reader and the PC.


Yes, I wrote this:
This would be too confusing for me. I would change the hardware.

  • You can rewire the RX and TX to other pins, and create a serial port on those pins with SoftwareSerial.

  • You can use an Arduino Leonardo. That board uses a software serial port for the USB bus (for the serial monitor on the PC) and has a normal hardware serial port on pin 0 and 1 that is not in use.


I think you will not get out of troubles, if you share the RX and TX pins. Or you could add a multiplexer chip.

Go Up