Simple serial communication - need a guide to automate it

Hello,

I am a beginner. I have spent some time trying to read up on my goals. I'm attempting to communicate with a device using the TX and RX pins with limited success. I have read many serial communication tutorials. They mention similar things involving the serial monitor, but I cannot find any that help me to communicate with my device automatically.

The device: A PWM frequency generator with TXD and RXD pins. "XY-LPWM LCD signal generation module"
The product page has this,

...the serial port control (one-chip computer TTL level communication)

Communication Standard : 9600 bps Data Bits : 8
Stop Bits : 1
Parity : none
Flow Control : none

1, set the frequency of PWM

"F101": Setting frequency is 101 HZ (001 ~ 999)

"F1.05": Setting frequency is 1.05 KHZ (1.00 ~ 9.99)

" F10.5 ": Setting frequency is 10.5KHZ (10.0 ~ 99.9)

"F1.0.5": Setting frequency is 105KHZ (1.0.0 ~ 1.5.0)

2, set the duty cycle of PWM

"DXXX": set the duty cycle of PWM to XXX ; (001 ~ 100)

For example, D050 , set the PWM duty cycle is 50%

3, read the setting parameters

Send " read " string, read the set parameters.

Set successfully returned: DOWN ;

Setting failed to return: FALL .

I hooked up the device to an Arduino Nano clone. I used an empty sketch with a simple "Serial.begin(9600);". With this sketch I am able to type in appropriate commands into the serial monitor and it works, as described in the quote above section 1 and 2.

What I want to do is have Arduino send those commands automatically and be able to "read" as in section 3. I can do neither.

Regarding, sending the instructions of the form Dxxx and Fxx.x to the Arduino, I have tried code like this from reading many examples,

int incomingByte = 0;   // for incoming serial data

void setup() {
  Serial.begin(9600);
}

void loop() {

  //Attemps to send serial data

  Serial.println("D010");
  Serial.println('D010');
  Serial.write('D010');
  Serial.write("D010");
  delay(1000);

  //Attempts to read serial data
 
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();

    // say what you got:
    Serial.print("I received: ");
    Serial.println(incomingByte);
  }
}

It doesn't do what I want though. It sends those messages to the screen, not to my device. I can manually type in message to my device, how do I get Arduino to do it? I don't see anything that does that in the Serial commands here...
https://www.arduino.cc/reference/en/language/functions/communication/serial/

My main, simple question - How can I get my Arduino to send the command "D010" automatically as though it had been typed in?

My secondary request is for some help in reading the data as described in section 3. When I type "read" or "READ" nothing happens.

I hope I have been clear, thanks for your help!

You have three options: one is to connect the device to pins 0 and 1 of the Arduino, in which case you can't have output to the serial monitor (and there may be problems with uploading programs).

The second is to use an Arduino with more than one serial port, such as a Mega. Then you can have, for example, "Serial1.prints" go to the device.

The third is to use AltSoftSerial or another software serial library to add a second, more limited serial port on some other pins. That is what most people do.

You seem to have missed the important information - what is the device (post a link to its datasheet or user manual) and how is it physically connected to your Arduino Nano?

If your device is some software on a PC that expects to communicate over a USB serial connection then you cannot have that connected at the same time as you have Serial Monitor connected.

...R

The only data sheet is what I quoted above under device, "XY-LPWM LCD signal generation module"Link. That is from the seller and the chinese product page translated to english. I am able to manually do the commands described in section 1 and 2.

I have hooked it up to pins 0 and 1. That is how I am able to send commands to it from the keyboard. I want to automate this and don't know how. The commands I have tried are in my code sample. I have tried running the sketch with and without the USB hooked up, but the commands don't take either way.

I am only able to issue the Dxxx and Fxx.x commands via keyboard with serial monitor up, that works. I do not know how to get arduino to do this without me, that is without serial monitor and a keyboard.

Edit -I have read about soft serial, But I still wouldn't know what way to send the commands. "Serial.print" and "Serial.write" dont work even when USB is disconnected.

jremington:

The third is to use AltSoftSerial or another software serial library to add a second, more limited serial port on some other pins. That is what most people do.

I have just tried this specifically and no dice. This is the code I adapted from the example,

#include <AltSoftSerial.h>

// AltSoftSerial always uses these pins:
//
// Board          Transmit  Receive   PWM Unusable
// -----          --------  -------   ------------
// Teensy 3.0 & 3.1  21        20         22
// Teensy 2.0         9        10       (none)
// Teensy++ 2.0      25         4       26, 27
// Arduino Uno        9         8         10
// Arduino Leonardo   5        13       (none)
// Arduino Mega      46        48       44, 45
// Wiring-S           5         6          4
// Sanguino          13        14         12

AltSoftSerial altSerial;

void setup() {
  Serial.begin(9600);
  Serial.println("Hardware Serial");
  altSerial.begin(9600);
  altSerial.println("D010");
  altSerial.write("D010");
}

void loop() {
}

I use the pins it says too, 8 and 9. I try to send the command D010 automatically, but the device doesn’t respond. I cannot type it in when connected to the altSerial pins 8 and 9.

How can I send a command of “D010” via serial? It only seems to work when I key it into the keyboard using hardware serial on pins 0 and 1.

You would normally do

Serial.print("D010");

or, if you need the trailing /n/r (newline, carriage return) characters you do:

Serial.println("D010");

The Serial.write() command is to send a single character (or byte vaue) at a time.

So what it comes down to now is how have you done your wiring exactly? Mind that normally Tx of the Arduino goes to Rx of the device, and the other way around.

You said you have it working while typing characters on your keyboard - please explain in detail how that works. Arduino involved? If not, how is it all connected? Please post photos of the setup and clearly drawn wiring diagrams. That way we can see how you managed to do the communication, in order to be able to mimic that on the Arduino.

Make sure the device you connect to the Arduino is using TTL voltage levels, if it uses RS232 it will damage your Arduino.

What you type in serial monitor ends up on the RX pin of the Arduino; that pin is also connected to the RX pin of your device and hence it works from the PC.

If you want the Arduino to replace the keyboard, you will have to swap the wires on pin 0 and 1 so the TX of the Arduino goes to the RX of the device; see also reply #1.

PS
You can experience upload problems so disconnect the wires before uploading a sketch.

sterretje:
What you type in serial monitor ends up on the RX pin of the Arduino; that pin is also connected to the RX pin of your device and hence it works from the PC.

Where did you find that crystal ball?

I'd love to have one of those! Makes contributions to this forum so much easier than having to pull information out of people, which is often so hard that it makes pulling foot boot and all out of quicksand feel easy...

Interpretation or mis-interpretation of the opening post.

sterretje:
What you type in serial monitor ends up on the RX pin of the Arduino; that pin is also connected to the RX pin of your device and hence it works from the PC.

If you want the Arduino to replace the keyboard, you will have to swap the wires on pin 0 and 1 so the TX of the Arduino goes to the RX of the device; see also reply #1.

PS
You can experience upload problems so disconnect the wires before uploading a sketch.

Thank you. I tried this but it didn’t work. Bummer. What I tried specifically,

Uploaded this code below to the Nano with its TX1 and RX0 disconnected. This code is simply trying to send the command “D010”. (Note the attempts to send the newline and to not send the newline.)

void setup() {
  Serial.begin(9600);
}

void loop() {

  //Attemps to send serial data
  Serial.println("D010");
  Serial.println('D010');
  Serial.print("D010");
  Serial.print('D010');
  delay(1000);
}

Then I unplug the micro USB from the Nano. I connect my “XY-LPWM” device’s Gnd, TXD, and RXD to the Nano’s Gnd, RX0, and TX1 respectively. I connect the Nano’s Vin and Gnd to a 5V source. I hit reset on the Nano. My device, “XY-LPWM”, does not respond accordingly.

To make it work, which you seem to understand but I want to now clearly spell it out for others… I unhook the external Gnd and 5V source from the Nano. Switch the TX and RX pins such that the XY-LPWM’s TXD and RXD are connected to the Nano’s TX1 and RX0, respectively. This condition allows me to send serial commands through the IDE’s Serial Monitor. I see the stuff being printed from my sketch, I do not see the commands that I type in. The commands do take though in this fashion. (Thanks wvmarle for clarifying this situation’s pins).

I hope this clears it up some. It’s an external device called the XY-LPWM with Gnd, TXD, and RXD connections. The only “instructions” are what I quoted in my first post. My second post has the source of those instructions. I can control it by sending keyboard instructions through the Arduino IDE’s Serial Monitor. I simply key in D 0 1 0 (Using a simple sketch with Serial.begin(9600).)

Then in an attempt to have the Nano automate this, I switch the pins such that TXD->RX and RXD->TX as one would expect them to be. I unplug the USB so there is no serial conflict. I reset the Nano and the commands in my above sketch (ie, Serial.println(“D010”)) have no affect on my XY-LPWM device.

Thank you all, I have achieved a step of success. I have gotten AltSoftSerial to adjust my device settings automatically. Thanks to @jremington for suggesting it. I am still left puzzled about my hardware serial behavior... But hopefully I can form a beachhead with this knowledge and get it eventually.

When I tried AltSoftSerial above I used altSerial.println() and altSerial.write() where I should have used altSerial.print() (without the ln)... I was not thorough enough in checking that suggestion the first time. Thanks to @wvmarle for articulating the difference between Serial.write and Serial.print.

My next goal is to use the "read" command as described in the product data quoted in the original post.
It simply says,

3, read the setting parameters

Send " read " string, read the set parameters.

Can I send this read command via the keyboard using AltSoftSerial? Do I need arduino to send it via a altSerial.print() command? How can I read the output? I think the "Echo" example in AltSoftSerial library will help me out.

Thanks all again. :slight_smile: