Go Down

Topic: Serial communications with XY-LPWM using Serial1 (Read 820 times) previous topic - next topic

dougrider

This is my first time posting on here, so if I screwed up anything in this post let me know and I will try to fix it.

I bought an XY-LPWM PWM generator to use for work. It can generate PWM signals as high as 30V. The frequency and duty cycle of the generator can be set using serial control. I am trying to get it to work on Serial1 of my Mega 2560, but I cannot. If I connect the RX & TX to Pins 0 and 1 I can open the serial monitor and send commands to it which set it properly

Code: [Select]
String setPWM;

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

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available() >0) {
    setPWM = Serial.readString();
    Serial.println(setPWM);
    Serial1.println(setPWM);
   
  }
}


 but if I try using it on Serial1

Code: [Select]
String setPWM;

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

void loop() {
  setPWM = "F105"; 
  Serial1.println(setPWM);   
  delay(5000);
}


it does not change the settings of the generator. I have tried it with the generator RX connected to the Mega RX and the generator TX connected to the Mega TX and also with RX-TX and TX-RX, but neither way worked. Any thoughts on this issue would be appreciated.

Thanks

holmes4


Robin2

You have not explained how you have connected everything in the two different scenarios. Please make some simple pencil drawings of the connections in the two cases and post photos of the drawings. See this  Simple Image Guide


And you have not told us exactly what the Arduino receives in the first case.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

TomGeorge

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html

Have you got the Tx of the generator connected to Rx of the Mega, and Rx of the generator connected to Tx of the Mega?

Have you got gnd of the generator and Mega connected together?

Tom.. :)
Everything runs on smoke, let the smoke out, it stops running....

dougrider

Initially I had the generator connected to Serial. Then I ran this code. With this code running I then opened the Serial Monitor. I could then type a command ("F101") and the PWM frequency was set to 100. So, I know that the PWM Generator communication is working.



Code: [Select]
String setPWM;

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

void loop() {
  if(Serial.available() >0 {
    setPWM = Serial.readString();
    Serial.println(setPWM);
  }
}



As I stated in the original post I then tried connecting the generator to Serial1 I tried it with the Gen TX to Mega TX & Gen RX to Mega RX first (Serial1.jpg).



Using the code below this did not work, so I flipped it to Gen TX to Mega RX & Gen RX to Mega TX (Serial1Flipped.jp).



That did not work either. In both cases the Gen ground was connected to the Mega ground.

Code: [Select]
String setPWM;

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

void loop() {
  setPWM = "F105";  
  Serial1.println(setPWM);    
  delay(5000);
}

dougrider

Initially I had the generator connected to Serial. Then I ran this code. With this code running I then opened the Serial Monitor. I could then type a command ("F101") and the PWM frequency was set to 100. So, I know that the PWM Generator communication is working.



Code: [Select]
String setPWM;

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

void loop() {
  if(Serial.available() >0 {
    setPWM = Serial.readString();
    Serial.println(setPWM);
  }
}



As I stated in the original post I then tried connecting the generator to Serial1 I tried it with the Gen TX to Mega TX & Gen RX to Mega RX first (Serial1.jpg).



Using the code below this did not work, so I flipped it to Gen TX to Mega RX & Gen RX to Mega TX (Serial1Flipped.jp).



That did not work either. In both cases the Gen ground was connected to the Mega ground.

Code: [Select]
String setPWM;

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

void loop() {
  setPWM = "F105";  
  Serial1.println(setPWM);    
  delay(5000);
}

horace

could it be a problem with the end of line?  try
Code: [Select]
setPWM = "F105\r\n";

bos1714

Hi there!

I see you tried Serial1 connections both with Tx-Tx and Tx-Rx, but with Serial it seems you only did Tx-Tx. Can you try Serial with Tx-Rx or have you already done that?
Time line? Time isn't made out of lines. It is made out of circles. That is why clocks are round.

dougrider

I tried it with setPWM = "F105\r\n";. That did not work either.

One more thing to be clear. I need to be able to communicate with this board using Serial1 because I need Serial to be open for communication with my PC.

Having said that, I did try to send commands to the generator using Serial and code (no Serial Monitor). It did not work with either Tx-Tx, Rx-Rx of Tx-Rx, Rx-Tx.

bos1714

I tried it with setPWM = "F105\r\n";. That did not work either.

One more thing to be clear. I need to be able to communicate with this board using Serial1 because I need Serial to be open for communication with my PC.

Having said that, I did try to send commands to the generator using Serial and code (no Serial Monitor). It did not work with either Tx-Tx, Rx-Rx of Tx-Rx, Rx-Tx.
That seems very strange. I did some research and found this Arduino Forum article that may give some clues as to what can solve your issue.
Time line? Time isn't made out of lines. It is made out of circles. That is why clocks are round.

dougrider

Quote
That seems very strange. I did some research and found this Arduino Forum article that may give some clues as to what can solve your issue.
Unfortunately I have read all of that several times trying to find some help there, but no.

TomGeorge

Hi,

Quote
Initially I had the generator connected to Serial. Then I ran this code. With this code running I then opened the Serial Monitor. I could then type a command ("F101") and the PWM frequency was set to 100. So, I know that the PWM Generator communication is working.
I hope you realise that the code in the Mega had nothing to do with this occurring.
You were sending serial commands directly to the generator from the Serial Monitor because they were connected to the same Serial port terminals.

Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

TomGeorge

Hi,
Look at this link;
https://www.arduino.cc/en/Tutorial/MultiSerialMega

Especially the use of  Serial.write instead of Serial.print

Tom.. :)
Everything runs on smoke, let the smoke out, it stops running....

dougrider

Thanks to everyone for your input.
I got it working now.
I needed to convert my string to a char array and then write (not print) the array to Serial1.

Thanks again.

Robin2

#14
Jun 17, 2018, 08:53 pm Last Edit: Jun 17, 2018, 08:54 pm by Robin2
can you help me with code? my arduino with module doesnt work
What module?

What code have you tried?

Exactly what happens when you run your program?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up