LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with serial communication between LabVIEW and Arduino

Solved!
Go to solution

I have a problem with serial communication between LabVIEW and Arduino, i had three radio button in LabVIEW that suppose to send string e.g ("1" , "2" ) to Arduino via serial communication. After run the LabVIEW, i push the first button and arduino received the string but when i push the second button, the Arduino did not respond as it didn't receive any string from the serial communication. I had been stuck with this problem, please anyone help me.

 

Also below attached the Arduino code:

 

#include <SoftwareSerial.h>

// Define the GSM module's RX and TX pins
#define GSM_RX_PIN 10 // Connect to TX pin of SIM900A
#define GSM_TX_PIN 11 // Connect to RX pin of SIM900A

// Create a SoftwareSerial object for communication with the GSM module
SoftwareSerial gsmSerial(GSM_RX_PIN, GSM_TX_PIN);
char labview = 0;

// Phone number to call
#define PHONE_NUMBER "+601154157154" // Replace with the phone number you want to call

void setup() {
// Initialize serial communication with the Arduino IDE
Serial.begin(9600);

// Initialize serial communication with the GSM module
gsmSerial.begin(9600);

// Initialize digital pins
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);

// Wait for the GSM module to initialize
delay(100);

// Set the GSM module to text mode
sendATCommand("AT+CMGF=1");

// Wait for a moment
delay(100);

Serial.println("Setup complete. Waiting for commands.");
}

void loop() {
// Check if data is available from LabVIEW
if (Serial.available() > 0) {
labview = Serial.read();

// Echo received command for debugging
Serial.print("Received command: ");
Serial.println(labview);

// Perform action based on user input
switch (labview) {
case '1':
Serial.println("Executing command 1: Making a call");
makeCall(PHONE_NUMBER);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
break;

case '2':
Serial.println("Executing command 2: Making a call");
makeCall(PHONE_NUMBER);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
break;

case '5':
Serial.println("Executing command 5: Sending an SMS");
sendSMS(PHONE_NUMBER, "Hello from Arduino!");
digitalWrite(3, HIGH);
digitalWrite(2, LOW);
break;

default:
Serial.println("Invalid choice");
break;
}

// Clear the serial buffer to avoid overflow or missed commands
while (Serial.available() > 0) {
Serial.read();
}
}
}

// Function to send AT commands to the GSM module
void sendATCommand(String command) {
gsmSerial.println(command);
delay(50);
while (gsmSerial.available()) {
Serial.write(gsmSerial.read());
}
}

// Function to make a call
void makeCall(String phoneNumber) {
// Command to call a number
String callCommand = "ATD" + phoneNumber + ";";
sendATCommand(callCommand);
}

// Function to send an SMS
void sendSMS(String phoneNumber, String message) {
// Command to set the destination phone number
sendATCommand("AT+CMGS=\"" + phoneNumber + "\"");

// Wait for prompt '>'
delay(1000);
gsmSerial.print(message);
delay(100);
gsmSerial.write((char)26); // Send Ctrl+Z to end the message
delay(1000);
}

0 Kudos
Message 1 of 5
(492 Views)

Most serial communication issues can be solved by watching this video: VIWeek 2020/Proper way to communicate over serial

 

I also noticed you are not using the Error cluster, so you could be getting an error that would point you in the right direction but you can't because LabVIEW has no way to tell you there is a problem.

 

Also don't post pictures of your code, attach the VI in question so we can take a look at it.

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 2 of 5
(469 Views)
Solution
Accepted by topic author akimkhal

You are closing the port inside of the loop.  So your resource is no longer valid after the first message is sent.  Move the VISA Close to inside of the "stop" case.

 

I have other concerns, such as the waits in there, but I would need to have the full VI in order to give better advice.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 3 of 5
(462 Views)

OMG it works, thank you so much.

0 Kudos
Message 4 of 5
(436 Views)

Thank you for your recommendation, really appreciate it!!

0 Kudos
Message 5 of 5
(434 Views)