Trouble reading Serial signal

I am having difficulty with a project, full disclosure I'm not really great at Arduino or programming stuff at all. I have a device that normally is hooked up to a terrible thermal printer using a RS-232 connection. I want to have the Arduino instead act like the printer and read what is being sent and eventually write it to a text file.

The issue is I cannot seem to get the serial data to look like regular text, it is often just a bunch of squares and random letters. It should have things like "Door open" or "Starting up" With some numbers as well. I checked the baud rate the printer was set up to(9600) I set the arduino Uno R3 up to that and serial.begin() is set to 9600. I also double checked and the com port(USB on my computer) is also set to 9600. Maybe I have the pins in the wrong place on the board(they are in 0 and 1) or maybe I'm using the wrong functions to read the code. I've tried, all be it at random, a variety of different sketches trying to find something that worked. I thought I might try here if anyone had a suggestion, I apologize if this isn't the right forum. Below is a sample of my code, a picture of the board setup and a picture of the output.

And another quick question, is it possible to compile and upload the sketch while the board is still hooked up to the tx/rx pins? I always have to remove them before I can try out a new sketch.

/*
 Analog input reads an analog input on analog in 0, prints the value out.
 created 24 March 2006
 by Tom Igoe
 */

int analogValue = 0;    // variable to hold the analog value

void setup() {
  // open the serial port at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // read the analog input on pin 0:
  analogValue = analogRead(0);
  String incoming = "";

  // print it out in many formats:
  Serial.println(analogValue);       // print as an ASCII-encoded decimal
  Serial.println(analogValue, DEC);  // print as an ASCII-encoded decimal
  Serial.println(analogValue, HEX);  // print as an ASCII-encoded hexadecimal
  Serial.println(analogValue, OCT);  // print as an ASCII-encoded octal
  Serial.println(analogValue, BIN);  // print as an ASCII-encoded binary
  incoming = Serial.readString();
  Serial.println(incoming);
  // delay 10 milliseconds before the next reading:
  delay(10);
}

You should not connect 2 things (serial monitor and printer) to the same serial port. Save the hardware serial port (USB) for program upload, debugging and program and variable monitoring. I suggest that you use a software serial port for the printer.

What is the analogRead() for? You have nothing connected to the analog input 0 (A0).

The serial input basics tutorial may have information that you can use. You are trying to read from serial before you know that there is something to read (Serial.available()). The tutorial shows how to read the serial data as it becomes available.

It would be much better if you post images on the forum. People do not like to go to those sites to download your images.
How to post images so we don't have to download them and everyone can see them.
How to post an image.
Another page on posting images.

Thank you for the reply. I was under the impression that analogRead() would read anything from pin(0). Maybe that is Analog Pin 0, probably makes a lot more sense. But it is definitely reading something.

You should not connect 2 things (serial monitor and printer) to the same serial port

I'm not sure what you mean by this. The thermal printer is fully disconnected. I'm trying to read the signal from the machine that typically prints to the thermal printer. I did initially look through that tutorial but maybe I need to take a closer look.

I did in fact try to post the images using the tags but they never showed up in my post, do the forum website not like imgur? or maybe it doesn't have the .jpg at the end.

Turns out you need the .jpg at the end. :smiley:

But it is definitely reading something.

You are reading the value of the floating analog input 0, not pin 0 which is the hardware serial RX pin.

I'm not sure what you mean by this. The thermal printer is fully disconnected. I'm trying to read the signal from the machine that typically prints to the thermal printer. I did initially look through that tutorial but maybe I need to take a closer look.

OK, I misunderstood or misread what you meant. The fact remains that you can't have 2 devices connected to the same serial port and expect either device to communicate well. Keep the hardware serial port (pins 0 and 1) for the tasks mentioned above and use a software serial port for the other device.

do the forum website not like imgur?

The forum members do not like downloading from an external site. We end up with a bunch of crap files that we have to delete from our systems.

Okay, maybe I'm confusing how port is defined. I'm not sure which port is being double used. There are only four things attached to the arduino. The ground pin, rx, tx pins from the device that is sending the print message and then the usb connection from the arduino to my PC.

If I don't need to use the digital pins, since I'm using RX and TX already built into the board does software serial do anything for me.

OR are you saying that the USB connection is controlled BY the Chip's built in rx tx uhhhhh thing so that is screwing up the transmission?

Sorry, I'm trying to understand what the issues are so I can learn from them.

USB is connected to D0/D1 via the USB/Serial adapter chip near the USB connector.

Use Software Serial and 2 other pins to read/act as the printer.
https://www.arduino.cc/en/Reference/SoftwareSerial

From your image’s point of view, the connector pins appear wired in a non standard way.

See:

RS232 is ± voltage levels which is not compatible with Arduino CMOS logic levels.

larryd:

From your image’s point of view, the connector pins appear wired in a non standard way.

See:

RS232 is ± voltage levels which is not compatible with Arduino CMOS logic levels.

Ffffffffff.... Thank you. I was going off the diagram from the printers interface manual, it looks weirdly weird.

Oh, no, I thought that I could just read it. Damn. Can I make it work with one of those tiny shields with that so RS232 to TTL?

An RS232 to TTL converter is needed between the Arduino and your RS232 device.

I assume you only need the RX data at the Arduino i.e. the Arduino does not need to send TX data; if so, a simple NPN transistor inverter will convert DTE TX data to TTL (CMOS) Arduino levels.

Something like this:


Note DTE pin 3 is the TX pin, it would connect to the anode of the diode in the above schematic.

The diode can be a 1N914/1N4148, the 100Ω resistor can be changed to 1k.

You may have to assert DCD and/or CTS.

Simple.... Simple.... Sure. I'll trust you on that. I don't have any loose diodes hanging around so I'll grab one of those converter shields. I do only need the RX data at the Arduino.

Thank you everyone for your help. Hopefully I can get this all sorted out!

Since you failed to disclose the actual thermal printer or any technical data on it, why do you believe what is sent to the printer is ASCII text? Your printer could be printing dots and the serial information could be describing the parameters of each dot.

Paul

Hi Paul, I didn't fail to disclose it. There really isn't information on the internet regarding it, let's just say it is a very specialized piece of equipment but really just a stupid thermal printer at heart. I have a pdf but I sincerely doubt anyone wants to risk downloading it from me.

sryii:
Hi Paul, I didn't fail to disclose it. There really isn't information on the internet regarding it, let's just say it is a very specialized piece of equipment but really just a stupid thermal printer at heart. I have a pdf but I sincerely doubt anyone wants to risk downloading it from me.

Good! There are really very few thermal printers. Most devices with such printers use one of the standard Japanese printers.

ASCII has always been 7 bits. There is/were an extended version with 8 bits. Probably to emulate IBM EBDIC.

Paul