Probably a really easy question...

Hi everyone,

I am a complete noob with both processing and arduino, but for my final year product design project I am going to need to use both. What I am trying to do is probably really simple, but I just cant seem to get it to work! I am wanting processing to send a simple “hello world” string to arduino via serial, and then for arduino to print it out from a receipt printer.

I have the receipt printer working from the Roo Reynolds using the code:

#include <SoftwareSerial.h>
#include <String.h>

#define rxPin 6
#define txPin 7

SoftwareSerial printer = SoftwareSerial(rxPin, txPin);

const byte command = 0x1B;

const byte fullcut = 0x69;
const byte partialcut = 0x6D;

const byte doubleprint = 0x47;
const byte flipchars = 0x7B;
const byte rotatechars = 0x56;

const byte commandBarcode = 0x1D;
const byte commandBarcodePrint = 0x6B;
const byte commandBarcodeWidth = 0x77;
const byte commandBarcodeHeight = 0x68;
const byte commandBarcodeTextPosition = 0x48;

const byte barcodeNarrow = 0x02;
const byte barcodeMedium = 0x03;
const byte barcodeWide = 0x04;

const byte barcodePrintNone = 0x00;
const byte barcodePrintAbove = 0x01;
const byte barcodePrintBelow = 0x02;
const byte barcodePrintBoth = 0x03;

// (0 = UPC-A, 1 = UPC-E, 2 = JAN13 (EAN), 3 = JAN 8 (EAN), 
//  4 = code 39, 5 = ITF, 6 = codabar, 7 = code 128)
const byte barcodeModeUPCA = 0x00;
const byte barcodeModeUPCE = 0x01;
const byte barcodeModeJAN13AEN = 0x02;
const byte barcodeModeJAN8EAN = 0x03;
const byte barcodeModeCODE39 = 0x04;
const byte barcodeModeITF= 0x05;
const byte barcodeModeCODEABAR= 0x06;
const byte barcodeModeCODE128 = 0x07;

 


void setup() {
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  printer.begin(9600);
  Serial.begin(9600); //open the USB connection too

  delay(1000);

  feed();
  
  println("Hello Again");
  println("\n");
  println("\n");
  println("\n");
  
  feed();
  
  /*setBarcodeHeight(50);
  setBarcodeWidth(barcodeNarrow);
  setBarcodeTextPosition(barcodePrintBelow);
  printBarcode("123456789012");
  
  feed();
  cut();
*/}

void print(char text[]) {
  printer.print(text);
}

void println(char text[]) {
  printer.println(text);
}

void printBarcode(char barcode[]) {
  printBarcode(barcode, barcodeModeUPCA);
}

void feed() {
  printer.println("");
  printer.println("");
  printer.println("");
}

void cut() {
  printer.print(command, BYTE);
  printer.print(fullcut, BYTE);
}

void partialCut() {
  printer.print(command, BYTE);
  printer.print(partialcut, BYTE);
}

void printBarcode(char barcode[], byte barcodeMode) {
  printer.print(commandBarcode, BYTE); //"[1D]h + [6B]h + n + Dn + [00]h"
  printer.print(commandBarcodePrint, BYTE); 
  printer.print(barcodeMode, BYTE); //barcode system. 
  printer.print(barcode);  //barcode value
  printer.print(0x00, BYTE); //end barcode sequence
}

void setBarcodeHeight(byte height) { //in dots. default = 162
  printer.print(commandBarcode, BYTE); //"[1d]H + [68]H + N"
  printer.print(commandBarcodeHeight, BYTE); 
  printer.print(height, BYTE); //barcode height in dots. default = 162
}

void setBarcodeWidth(byte width) { // 2, 3 or 4. default = 3
  if (width < 2) width = 2;
  if (width > 4) width = 4;
  printer.print(commandBarcode, BYTE); //"[1d]H + [77]H + N" (where N = 2, 3 or 4)
  printer.print(commandBarcodeWidth, BYTE);
  printer.print(width, BYTE); 
}

void setBarcodeTextPosition(byte position) {
  if (position < 0) position = 0;
  if (position > 3) position = 3;
  printer.print(commandBarcode, BYTE); // "[1d]H + [48]H + N"
  printer.print(commandBarcodeTextPosition, BYTE);
  printer.print(position, BYTE); // 0 = none, 1 = above, 2 = below, 3 = both
}

void setDoublePrintOn() {
  setDoublePrint(0x01);
}

void setDoublePrintOff() {
  setDoublePrint(0x00);
}

void setDoublePrint(byte mode) {
  printer.print(command, BYTE);
  printer.print(doubleprint, BYTE);
  printer.print(mode, BYTE);
}

void loop() {
  // while there are bytes to read from the computer, retransmit them
  if (Serial.available() > 0) {
    char string1 = Serial.read();
    if(string1 != "Stop") {
    byte inchar = Serial.read();
    printer.print(inchar);
    printer.println(string1);
    }
  }
}

and am using a simple processing code to send “hello world” on mousePressed:

import processing.serial.*;

Serial myPort;
int val;

void setup() {
  String portName = Serial.list()[0];
  myPort = new Serial (this, portName, 9600);
}

void draw() {
  if(mousePressed) {
  myPort.write("hello");
}
else {
  myPort.write("Stop");
  }
}

I know this is probably a really simple problem, but everything I try doesnt seem to work. I thought it might be a pointers problem, but have not been able to sort it out.

Sorry if this is really simple, but hopefully this will mean it can be solved quickly.

The Arduino resets when you open the serial port; this means it is not listening for a short while immediately after Serial.open().

Perhaps delaying for a half second after the open() before sending data will help.

-br
http://bitlash.net
http://entropymouse.com

What is the value of portName? Is it really the port that the Arduino is attached to?

SoftwareSerial printer = SoftwareSerial(rxPin, txPin);

SoftwareSerial is obsolete. Why are you still using it? Use NewSoftSerial, instead.

  if (Serial.available() > 0) {
    char string1 = Serial.read();
    if(string1 != "Stop") {
    byte inchar = Serial.read();
    printer.print(inchar);
    printer.println(string1);
    }
}

I'm amazed that you can develop an interface to external hardware but can't (or haven't) read simple documentation. http://arduino.cc/en/Reference/HomePage

You expect that the first character might be "Stop". Without even taking my shoes off, I can see that "Stop" is 4 characters, so I know that it can not possibly be stored in char variable, which can only hold one character.

Why do you switch between chars and bytes to hold the output of the Serial.read() function? Did you look at the type of value that Serial.read returns?

Several times a week the topic of how to read serial data comes up. In fact, there are several threads that have been going on this week on this topic. It's time to exercise your google-shu.

Tried again using the NewSoftSerial library. For some reason I can get it working using char but it wont work if I try to use string?! Have googled and googled, but can't work out where I am going wrong?

void loop() {
  // while there are bytes to read from the computer, retransmit them
  if (Serial.available() > 0) {
    char string1 = Serial.read();
    if(string1 != 'S') {
    byte inchar = Serial.read();
    printer.print(inchar);
    printer.println(string1);
    }
  }
}

if I change it to:

void loop() {
  // while there are bytes to read from the computer, retransmit them
  if (Serial.available() > 0) {
    String string1 = Serial.read();
    if(string1 != "Stop") {
    byte inchar = Serial.read();
    printer.print(inchar);
    printer.println(string1);
    }
  }
}

it just prints out 83?

http://arduino.cc/en/Serial/Read

Returns

the first byte of incoming serial data available (or -1 if no data is available) - int

From http://www.asciitable.com/, 83 = ‘S’.

The draw() function in Processing is like the loop() function in Arduino. It is called over and over, as fast as possible. You are flooding the Arduino with serial data.

On each pass through loop, you are reading one or two characters. If the first is not “Stop”, which it never will be, you read another one, as a byte. Then, you send the byte and the one-character String to the printer. The value of the byte should have been 116, and the value of string1 should have been “S” (the first time).

I’d recommend only sending something from processing if you have something to send, like the mouse was pressed.

When the mouse is pressed, send 1 character. Then, set a flag indicating that the byte has been sent.

On each pass through draw, check that flag. Only send if the flag is false. Also, on each pass, reset the flag is mouseRelease is true.

This way, each mouse press and release will send 1 character, not a steady stream of data.

When printing using the printer object, print char type variables.