I have a square wave generator that had serial programmable frequency and duty cycle. It only knows 2 commands
F150 (Means Frequency 100 hz)
D050 (Means 50% duty Cycle)
You can also send a read and it will answer with the current frequency and duty cycle.
If I send those commands from the serial monitor tool the board works as advertised. I loaded an example of talking to 2 serial ports (The closest I could find) and tried about a million combinations. The serial monitor screen looks like I get the correct results but the device just sits there. When I embedded the read command in the code I just got he word "read" in the serial monitor, I did not get the values in the device.
Help would be appreciated. Surely I am close. I am a hardware guy. Debugging code is stressful.
*/ #include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("F150");
// set the data rate for the SoftwareSerial port
Serial.begin(9600);
Serial.println("D090");
}
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
}
Got rid of the line about the USB. I am using a nano. It looks like the first step assigns a logical name of mySerial to SoftwareSerial so I tried talking to mySerial. I am not writing to the monitor any more. That explains why it looked okay but didn't work. It still isn't talking to the circuit card though. I wish I knew what command the monitor sends to the nano. That works.
*/ #include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
// Open serial communications and wait for port to open:
mySerial.begin(9600);
mySerial.print("F100");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
mySerial.println("D040");
}
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
}
Well, now you've made the opposite mistake in setup. You turned both begin statements into mySerial
So look, you've got two serial interfaces, one called Serial that talks to your Serial montior and when you type into your serial monitor and hit send it receives from that. You've got another called mySerial (a pretty useless name obviously copied from an example - you should give it a name that tells something about what it is connected to or something) and that one goes to your device.
It looks to me like you need the two begin statements in setup but make one for Serial and one for mySerial and ditch the two print lines.
Upload that and open up the serial monitor and type in "F100" (without the quotes) and send it and see what happens. If that's really what the device wants (in ascii?) then it will work.
I'm taking a step back. You have a device that you can hook up to a PC and control from Serial Monitor. The first question would be how you connected it to the PC? Using a FTDI cable? Using a RS232 cable?
Thank you so much. That cleaned it up and now I semi-understand. I have tis device wired to d10 and 11 which is a virtual serial port. Yes the device only knows ASCII
I was really getting frustrated. My next step is to walk the duty cycle of the signal between 10 and 90 and back down in a loop. That is going to be tricky for me. I know how to do that with a number and a loop but I have to figure out how to get that number turned into ASCII. Its a challenge I don't mind right now. I was just lost and needed you folks to get me on track. Thanks again.
Quick question,
I need several serial ports. If I understand I can really just use (10 and 11) and (12 and 13) for software serial ports. The TX and RX on pins 1 and 2 are tied to the USB port, correct? I have a CD 4053 I can use to construct a couple of more ports. I could actually use 9 serial ports.
The risk with multiplexers is that if the devices send data without data being requested and you switch while the devices transmit, you will get corrupted data (e.g. a few bits from one device followed by a few bits from another device).