Is it possible for me to see the fetched string from matlab in arduino serial monitor? I've been working this matlab to arduino interface and I am really having a hard time configuring the two. I need some advices. Thanks in advance!
This forum is for C/C++ programming problems when the program is running on the arduino. Posting java/perl /cobol/fortran/algol60(the mother of all modern programming languages)/algol68/matlab or any other bits of code on this forum is not going to get you any good result.
Think about you question. You have a program written in (see list above and add ....) which is using the coms port to read and write data. And then you want a second program to read the data sent by the arduino. What do you think the answer is?
holmes4 is right. If Matlab has the port open, you will not be able to open a Serial Monitor or a terminal program.
The good news is that you don't have to send with Matlab, if all you are wanting to do is to test your Arduino program. All you need to do is to send the string with the Serial Monitor, making sure it's exactly the same, including any end-of-line characters. You can then echo the string back in any format you like, or send back the result that would notmally go to Matlab, for manual checking by you. Additinally, if you are having any problems between sending the Matlab string, and getting the result string back, you can sprinkle a few Serial.print() statements around various parts of your Arduino code.
@lar3y Yes, I've made a code that can input the string in serial monitor and it echoed back in the serial monitor plus arduino was able to insert the string in my GSM code and it successfully send an SMS(code below). However, instead of typing the string in serial monitor, I want to do the input part in Matlab because I have my GUI in matlab.
M
String readString;
char charb[12];
char Rx_data[50];
unsigned char Rx_index = 0;
int i = 0;
char msg[160];
int sig;
void setup(){
  Serial.begin(9600);
}
void loop() {
Â
 if (Serial.available()) {
  char c = Serial.read(); //gets one byte from serial buffer
  if (c == ',') {
   //do stuff
   Serial.println(readString); //prints string to serial port out
   readString.toCharArray(charb, 12);
   initGSM();
   send_msg(charb, "Your sample has been tested. You may now get your result. Thank you.");
   readString=""; //clears variable for new input  Â
  }Â
  else { Â
   readString += c; //makes the string readString
  }
 }
}
void loopGSM() {
 //none
}
void send_msg(char *number, char *msg)
{
 char at_cmgs_cmd[30] = {'\0'};
 char msg1[160] = {'\0'};
 char ctl_z = 0x1A;
 sprintf(msg1, "%s%c", msg, ctl_z);
 sprintf(at_cmgs_cmd, "AT+CMGS=\"%s\"\r\n",number);
Â
 sendGSM(at_cmgs_cmd);
 delay(100);
 delay(100);
 delay(100);
 sendGSM(msg1);
 delay(100);
}
void sendGSM(char *string){
 Serial.write(string);
 delay(90);
}
void clearString(char *strArray) {
 int j;
 for (j = 100; j > 0; j--)
  strArray[j] = 0x00;
}
void send_cmd(char *at_cmd, char clr){
 char *stat = '\0';
 while(!stat){
  sendGSM(at_cmd);
  delay(90);
  readSerialString(Rx_data);
 Â
  stat = strstr(Rx_data, "OK");
 }
 if (clr){
  clearString(Rx_data);
  delay(200);
  stat = '\0';
 }
}
void initGSM(){
Â
 send_cmd("AT\r\n",1);
//Â send_cmd("ATE0\r\n",1); // Turn off automatic echo of the GSM Module
 send_cmd("AT+CMGF=1\r\n",1); // Set message format to text mode
 //Sucess
Â
 Serial.println("Success");
 delay(1000);
 delay(1000);
 delay(1000);
}
void readSerialString (char *strArray) {
Â
 if(!Serial.available()) {
  return;
 }
Â
 while(Serial.available()) {
  strArray[i] = Serial.read();
  i++;
 }
}
About the only way I can see to do that would be another Arduino connected to another COM port, running a Serial Monitor. You could use SoftwareSerial to transfer between Arduinos.
Thanks lar3y. That's the answer I really need. It is really impossible for matlab to send string to arduino. It needs two arduinos to make it possible. Thanks