Show Posts
Pages: [1]
1  Using Arduino / Interfacing w/ Software on the Computer / Re: Java and Arduino on: May 14, 2011, 03:45:24 am
I had the same problem recently. This happens because Arduino understands ASCII decimals and when try to print them it prints characters not readable. From your java code you need to convert your value into a string and then call the getBytes() method and store into byte array. Then send the byte array. This works for me. I think you do not need the second line begore the end (byte[] bytes = ByteBuffer.allocate(4).putInt(sByteTobeSent).array()smiley-wink

For example:
Code:
String command = "w";
byte[] bytes= command.getBytes();
write(bytes);
2  Using Arduino / Interfacing w/ Software on the Computer / Re: Char array to integer on: May 14, 2011, 03:31:05 am
It worked!!! The range is 0-255 and not 0-1024 as I thought. This was my confusing due to my little experience.
Thank you for your help.
3  Using Arduino / Interfacing w/ Software on the Computer / Re: Char array to integer on: May 13, 2011, 04:35:31 pm
I've increased the delay but still the same problem. The baud rate is at 9600. Something else is the cause that I cannot figure out because when I enter L200 and then L500 then the motor goes quicker than before. How can be sure that the integer value is the right one? I've puted a println function in order to see the value but it doesn't make sense. The 300 speed is lower than the 200 which is not normal.
4  Using Arduino / Interfacing w/ Software on the Computer / Char array to integer on: May 13, 2011, 12:50:16 pm
Hi,
I'm trying to send a command through serial in the format of "L1023" in order to move a motor to the left direction and 1023 speed. My approach is to store the command it to a char array, get the first element of array and then the remaining elements as integer using the atoi function but the results are not the expected. When I enter a sequence of "L100", L200, L300 the last one suppose to run faster but it doesn't. Do you know why? Its getting me crazy this.
Code:
void loop(){
  index = 0;
  i = 0;
 
  //Get the serial command and store it to the char array
  while (Serial.available()){ 
    cmd[index] = Serial.read();
    delay(1);
    index++;
  }
  cmd[index] = 0;
  int i = atoi(cmd+1);
 
  if (index > 0){
    Serial.println(i);
    motorControl(cmd[0], i);
  }
}

void motorControl(char dir, int spd){ 
    if (dir == 'L'){
      //turn left or counterclockwise at spd speed
      digitalWrite(pinA, LOW);
      digitalWrite(pinB, HIGH);
      analogWrite(pinB, spd);
    }
    else if (dir == 'R'){
      //turn right or clockwise at spd speed
      digitalWrite(pinA, HIGH);
      digitalWrite(pinB, LOW);
      analogWrite(pinA, spd);
    }
    else{
      digitalWrite(pinA, LOW);
      digitalWrite(pinB, LOW);
    }
5  Using Arduino / Interfacing w/ Software on the Computer / Re: Serial communication strange problem - Please help. on: February 24, 2011, 06:27:10 pm
It works!!!
Now I can continue with the other commands.
Thanks a lot guys.
6  Using Arduino / Interfacing w/ Software on the Computer / Re: Serial communication strange problem - Please help. on: February 24, 2011, 07:44:13 am
Ok, I will try to remove the delay.
Sorry for my begginer questions smiley-red
7  Using Arduino / Interfacing w/ Software on the Computer / Re: Serial communication strange problem - Please help. on: February 24, 2011, 07:31:32 am
...
  while (Serial.available()){//check for incoming message

    delay(CMD_SIZE);// Each character does 1ms in order to transfer
    cmd[index] = Serial.read();//Gets 1 character and decreases the buffer
    delay(2);
    //Serial.print(cmd[index], BYTE);
    index++;
  }
  cmd[index] = 0;
...

The last line would only set 0 to the last character. Do I need to set 0 all the array elements?

Code:
  while (Serial.available()){//check for incoming message
    delay(CMD_SIZE);// Each character does 1ms in order to transfer
If there is a character in the serial buffer, wait for it to get there. Makes (no) sense to me.

1ms at what baud rate? The time it takes to receive a character at 300 baud and at 115200 baud will be different.
The baud rate is 9600. How can I calculate this?
8  Using Arduino / Interfacing w/ Software on the Computer / Serial communication strange problem - Please help. on: February 23, 2011, 03:22:08 pm
Hi,
I'm trying to send recieve commands through serial.
Here is my code:
Code:
void setup(){
  //Initialize location
  //her.Position(LAT, LON);
  //her.TimeZone(TIME_ZONE * 60);
  //her.Sidereal(date_time, true);

  Serial.begin(9600);

}

void loop(){
  int index = 0;//the incoming message length

  while (Serial.available()){//check for incoming message

    delay(CMD_SIZE);// Each character does 1ms in order to transfer
    cmd[index] = Serial.read();//Gets 1 character and decreases the buffer
    delay(2);
    //Serial.print(cmd[index], BYTE);
    index++;
   

  }
 
  if (index > 0){
    //Serial.print(index);
    if (strcmp(cmd, ":GS#")  == 0){//Get the Sidereal Time
      Serial.print("13:25:54#");
    }
    if (strcmp(cmd, ":GVF#") == 0){//Get Version - CAUSE A PROBLEM
      Serial.print("ETX Autostar#");
    }
    if (strcmp(cmd, ":GZ#") == 0){//Get telescope azimuth
      Serial.print("150*45#T");
    }
    if (strcmp(cmd, ":GW#") == 0){//Get alignment status
      Serial.print("AS2#");
    }
    Serial.flush();
  }
}
My problem is in the second nested if statement. When sending the other commands, arduino response appropriately but when enters in the second nested if then I cannot recieve responses anymore. For some reason only 2 letters between : and # characters are working (i.e. :GW#, :GZ#, :GS#) but the :GVF# works only once and then arduino stops sending responses.

Why is this happening?
9  Using Arduino / Interfacing w/ Software on the Computer / Re: Serial Communication with PC Software problem on: February 10, 2011, 06:15:09 am
I just figoured out what was the cause of the problem.
I have to open the PC program first and then to plug in the usb cable to the Arduino board.
I don't know why is this happening but I think its not a problem.
Now I see that the commands are being sending and reciving properly without any encoding (i.e. DEC).

Thanks for your help. Now I'm ready to implement the communication protocol smiley-lol.
Maybe I will use the Messenger library.
10  Using Arduino / Interfacing w/ Software on the Computer / Re: Serial Communication with PC Software problem on: February 08, 2011, 05:39:28 pm
What does this (in place of the Serial.print statement you have now) print in the serial monitor?
Code:
Serial.println(Serial.read(), DEC);
Hi! I've tried the above code and I got "29..77..141.." as response. Is it good or bad?  smiley-red
11  Using Arduino / Interfacing w/ Software on the Computer / Re: Serial Communication with PC Software problem on: February 08, 2011, 01:23:11 pm
Sorry, my mistake.
The code is this:
Code:
void loop(){
  int index = 0;//the incoming message length

  while (Serial.available()){//check for incoming message

    delay(20);// Each character does 1ms in order to transfer
    Serial.print(Serial.read());//Need to fix this
    //cmd[index] = Serial.read();//Stores the word and decreases the buffer
    //delay(1);
    //index++;
   

  }
  /*

  if (index > 0){
    Serial.print(index);
    if (strcmp(cmd, "#:GS#")  == 0){
      Serial.print("13:25:54#");
    }
  }
  */
}
I'm not able to see the exaclty what the PC programm sends and the only way is by a monitoring programm.
This is the request/reaponse from the monitoring program:
12  Using Arduino / Interfacing w/ Software on the Computer / Re: Serial Communication with PC Software problem on: February 07, 2011, 10:34:38 am
It is sent by the PC program.
Anyway, I found a library called Messenger here http://www.arduino.cc/playground/Code/Messenger.
It says that "is a "toolkit" that facilitates the parsing of ASCII messages"
Is it going to solve this issue?
13  Using Arduino / Interfacing w/ Software on the Computer / Re: Serial Communication with PC Software problem on: February 07, 2011, 05:50:08 am
Ok,
I've done some debugging and the Serial.read() gets odd characters and stores it to the array.
For example when I send a '#' char the Serial.read() stores a '.' char.
I think this is the cause of the problem.

How do I fix this?
14  Using Arduino / Interfacing w/ Software on the Computer / Re: Serial Communication with PC Software problem on: February 05, 2011, 07:01:15 pm
Thanks for your reply!
Unfortunatelly the PC program is commercial (it's not developed by me) and the protocol is fixed.

I have a monitoring program which I can see the requests/responces messages and I saw that the PC program sends the command properly (from the monitoring program I see that the program sends: "#:GS#").

From the specification of the protocol it says that the baud rate is 9600 and messages must be Hex datatyped so I assume that the PC program would be properly configured (or not?).
15  Using Arduino / Interfacing w/ Software on the Computer / Serial Communication with PC Software problem -- FIXED on: February 05, 2011, 12:11:58 pm
Hi!!
I'm new to the forum and to the Arduino programming and I would like your help.

I'm trying to implement a communication protocol between a software runnig at the PC and Arduino.
I've written a code for getting/sending commands from/into the Arduino but I'm facing a strange problem.

The first request response must be:
request -> #:GS#
response->13:25:54#
My code is this:
Code:
void loop(){
  int index = 0;//the incoming message length
  
  while (Serial.available()){//check for incoming message
    
    delay(20);// Each character does 1ms in order to transfer
    cmd[index] = Serial.read();//Stores the word and decreases the buffer
    delay(1);
    index++;

  }

  if (index > 0){
    
    //Serial.print(cmd);

    if (strcmp(cmd, "#:GS#")  == 0){
      Serial.print("13:25:54#");
    }
  }
}
When I test this with the Serial monitor it works well but in the PC software I cannot get the response.
This is due to the fact that the cmd array stores strange chatacters because when I uncomment the Serial.print(cmd) I get strange characters and the comparisson inside the if fails.
Although this doesn't happen when I test it with Serial Monitor.

Do you know what cause the problem?
I'm getting crazy with this. smiley-confuse
Pages: [1]