Go Down

Topic: 2 Newbie Arduino questions (Read 4797 times) previous topic - next topic

tstrowd

Has anybody had a problem with the Arduino losing it's program?
Scenerio: Host program sending "commands" to the Arduino(Serial port) and it works great.
The next day after powering up the Arduino does not respond to the host program. I upload the same I/O program and it runs great again.

Also:
Everything that comes to the arduino via serial port is an asc value?
Can i send an integer, (example 1500) to the arduino and it recognizes it as int 1500?

AWOL

Without knowing more about the Arduino and host programs, it is probably impossible to answer your first question.
Did you try just a reset?

Quote
Everything that comes to the arduino via serial port is an asc value?

No, it is a binary value.
Quote
Can i send an integer, (example 1500) to the arduino and it recognizes it as int 1500?

1500 decimal (0x5DC) requires at least eleven bits to represent, so can't be sent in a single byte, but you could easily send it as 0xDC and 0x05.

tstrowd

Yes i did try pushing the reset button to no avail.
I noticed this also when i used to use the OOpic it had a similiar problem.
Every once in a while.
The only fix was to upl it again.
I'm using VS C# as a host program.

AWOL

You've cross-posted this, but you still haven't posted your code.

tstrowd

Here is some code:
Maybe this is something very easy that i'm missing but i was wondering why i'm getting a strange value back from the Arduino to the host program. I'm converting  a dec value (300)  to hex and then sending to the Arduino and expect to get the same back.

int HexValueIn1 = 0;

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

void loop()
{
 
 if (Serial.available() == 2)
 {
   HexValueIn1= Serial.read();
   
   Serial.print(HexValueIn1, HEX);
  }

}

Now the C# side:

int decValue1 = 300;
string hexValue1 = decValue1.ToString("X"); //hexValue1 = 12C
Sport1.Write(hexValue1); //Writing 12C to serial port
string Value = Sport1.ReadExisting(); //Value comes back from Arduino (3132)
int value1 = int.Parse(Value,System.Globalization.NumberStyles.HexNumber); //value1 is now (12594)  ??????
           

AWOL

#5
Jul 14, 2009, 08:35 am Last Edit: Jul 14, 2009, 08:37 am by AWOL Reason: 1
Quote
 HexValueIn1= Serial.read();


Serial.read inputs a single byte, but 300 decimal (0x12c) is not representable in a single byte.

tstrowd

So there is no way to read in more than 1 byte at a time?
I just tried decimal value 30 and get 3145 back.
Decimal 30 should be less than 1 byte.

AWOL

No, there is no way of reading more than a byte at a time, because that's limited by the serial interface hardware.

Sorry, I don't know C#, but I'm guessing that
Code: [Select]
string hexValue1 = decValue1.ToString("X");
converts your number to an ASCII string, so the decimal number 300 comes out as a string containing the binary values 0x33 0x30 0x30 0x00.

Have you tried cutting out the C# and using the serial monitor?
If you're not sure of what you're doing, it is never a good idea to try debugging two different programs simultaneously.


tstrowd

string hexValue1 = decValue1.ToString("X");
Sport.Write(hexValue1);
The serial port in C# sends everything as a string.
The above statements converts 300 into  hexvalue 12C then sends it out the serial port as a string.
Are there any good examples out there of serial comm with the arduino other than just basic asc values? Like a stream of serial bytes and have the arduino react off of it.
I'm getting the impression that most people don't use much serial comm with a host computer.

EmilyJane

Reply #4 makes sense up to a point. The C# program is sending a string representing 0x12C which would be 0x31, 0x32, 0x43. Arduino is returning 3132 which is the ASCII characters for the first two characters of the string it was sent.

Does that help any or is that just a coincidence?

tstrowd

Ok that makes sense. So apparantly any string coming across the serial port Arduino will see as asc values.
Is there a function or method to convert that asc value into an integer on the arduino side?
Example :VB has a ASC(value) method

EmilyJane

#11
Jul 16, 2009, 03:58 am Last Edit: Jul 16, 2009, 04:11 am by EmilyJane Reason: 1
It'll see them as bytes. If they happen to represent ASCII characters, it's up to your sketch to know that if it's important. If it is a string, then they will be ASCII characters.

You'll have to read the bytes into an array of char and then there is a string to integer function. Can't remember at the moment but any C reference will have it.

I'm sure AWOL knows. :)

Edit:
Code: [Select]
char *s;
int i;
...
s = "1234";
i = atoi(s);

tstrowd

Thanks for your help. I'll look for the converting function.
It is important to convert when i send a integer 2000 over the serial port. For example i want the host program to send a value address to write and read from the Ardunio EEprom.

BTW is there a easy way to pulse a digital output with one of the three timers. If i use delay(?) it pauses the program there. I need a way to do this on it's own thread so it dosen't interrupt the program execution.

EmilyJane

Sorry you used up your questions, you'll have to start a new thread. Be sure you mention important things like the frequency of the pulse and how wide it has to be. :)

tstrowd

Even though i used up all my questions i want to post this so other people might benefit.
My problem with getting back unexpected values,or values that didn't make sense was because of the way i was sending it from C#.
Originally i was sending it in decimal converted to string through the serial port.
Port.Write("20");

The correct way is to send it , Hex bytes:
Port1.Write(new byte[]{0x6E},0,1);

0x6E=110 decimal and guess what? It comes back as a string"110".

Go Up