Society of Robots - Robot Forum

Software => Software => Topic started by: dellagd on June 30, 2010, 07:04:25 AM

Title: Send Text to AxonII from Hyperterminal (new problem)
Post by: dellagd on June 30, 2010, 07:04:25 AM
I have been looking through webbotlib code for a way to recieve characters from a Hyperterminal. (I enter characters on hyperterminal and want to get them onto my AxonII)

The only thing I have found is this:

Quote
uartGetByte
int uartGetByte(UART* uart)
Returns the next byte from the receive buffer or -1 if there is no buffer or the buffer is empty.
Example:
Code: [Select]
int ch = uartGetByte(UART0);
if(ch!=-1){
// We have got a character in 'ch'
}
There are shorthand forms when dealing with a specific uart. eg
uart0GetByte() is the same thing as uartGetByte(UART0) and
uart1GetByte() is the same thing as uartGetByte(UART1)

but it seems this is only for integers. I am looking for a way to get a whole string of ASKII characters across the UART usb link
Title: Re: Send Text to AxonII from Hyperterminal
Post by: Admin on June 30, 2010, 07:22:01 AM
Attached is example code to transfer entire strings to the Axon. The Razor IMU code sends a full string of data, and then the Axon reads in the entire string and breaks it up into useful bits.

The key was the C function atoi.
Title: Re: Send Text to AxonII from Hyperterminal
Post by: Webbot on June 30, 2010, 07:52:58 AM
Quote
but it seems this is only for integers.

The uartGetByte needs to return two pieces of information:
1. Has a character been received Yes/No
2. The actual character byte - which can be a value from 0 to 255

It does this by returning the first piece in the top byte of the return value where 0 = Yes, 0xFF = No
If there is no character then the low byte is also set to 0xFF so the return value is 0xFFFF which is -1
If there is a character then the low byte holds the character

This is a very common technique in C and is used quite liberally throughout the standard C libraries.

So what you can do is:

Code: [Select]
int ch = uartGetByte(UART1);
if( ch != -1 ){
    char ascii = ch & 0xff ; // Get the character from the low byte of the returned value

    // 'ascii' now holds the character
}

Title: Re: Send Text to AxonII from Hyperterminal
Post by: dellagd on June 30, 2010, 08:56:04 AM
I see what you mean Webbot. I was didnt realize that it was just checking to make sure it was just NOT -1.

Also, could you explain how that code takes a whole string of data? It seems it just takes a one number and does things accordingly.

I would like to enter, say, a whole password in my program. I am guessing you would just have to use the uartGetByte() on the Hyperterminal UART connection however many times there are letters, (use it five times for a five letter password) then assemble them into a single string?
Title: Re: Send Text to AxonII from Hyperterminal
Post by: waltr on June 30, 2010, 09:56:36 AM
A printable character is just 7bits of a byte. So in getting data from the UART there isn't a difference between a character and a byte. Look up the ASCII table to see what characters look like in binary and hex.

After your code Gets the Byte from the UART buffer your code then must parse each character (byte) to say match a password or a command.
Title: Re: Send Text to AxonII from Hyperterminal
Post by: Admin on June 30, 2010, 12:16:17 PM
I see what you mean Webbot. I was didnt realize that it was just checking to make sure it was just NOT -1.

Also, could you explain how that code takes a whole string of data? It seems it just takes a one number and does things accordingly.

I would like to enter, say, a whole password in my program. I am guessing you would just have to use the uartGetByte() on the Hyperterminal UART connection however many times there are letters, (use it five times for a five letter password) then assemble them into a single string?
See my posted code, dude :P

I reads one character at a time and puts it into an array. Then you can do whatever you want with that array.
Title: Re: Send Text to AxonII from Hyperterminal
Post by: dellagd on June 30, 2010, 02:20:20 PM
my apoligies, but I do not see data being put into an array anywhere.

I'm probably to un-skilled to see it (I'm pretty new to programming AVR chips)

I (emphasis on I)see data being sent to Hyperterminal as a string, but not recieved to the axon and stored as one.

EDIT:

I think I got it.

Code: [Select]
while(cByte!=';')
{
cByte=GET_DATA_U0;//store command data

if(cByte!=-1)//has new data
{
Razer_Raw_Data[counter]=cByte;//store data
counter++;
}

what it does is cycle through , going to the next array node (its a node, right?) after it recognizes you have entered new character until it gets to 15, when the array runs out.

My only question is what happens when you get to 15 characters in this? What if I kept entering characters? I remember something about exceeding array sizes being bad, but I might be wrong...
Title: Re: Send Text to AxonII from Hyperterminal
Post by: Admin on June 30, 2010, 02:56:58 PM
what it does is cycle through , going to the next array node (its a node, right?) after it recognizes you have entered new character until it gets to 15, when the array runs out.

My only question is what happens when you get to 15 characters in this? What if I kept entering characters? I remember something about exceeding array sizes being bad, but I might be wrong...

The while loop says:
while(cByte!=';')

So if your Axon was sent a ; character, it'll exit that loop :P
Title: Re: Send Text to AxonII from Hyperterminal
Post by: dellagd on June 30, 2010, 03:12:56 PM
I see.
is there any way to use another key like 'return' or shift
Title: Re: Send Text to AxonII from Hyperterminal
Post by: Admin on June 30, 2010, 03:19:13 PM
I see.
is there any way to use another key like 'return' or shift
yeap . . . not sure what they are offhand, but if you google around you'll find it

probably this:
http://www.robelle.com/smugbook/ascii.html (http://www.robelle.com/smugbook/ascii.html)
Title: Re: Send Text to AxonII from Hyperterminal
Post by: dellagd on June 30, 2010, 03:53:55 PM
Char   Oct   Dec   Hex   Control-Key      Control Action

CR   15   13   d   ^M                  Carriage Return

                         /\
                          l
                          l
                          l
                          l

              Here's what I got 

So I assume that using uartGetByte(uart0); Would return CR? but wouldnt that be two characters?

As a solution I see using the Hexadecimal one. Is there a way to get the hexadecimal reading instead of the character one?
Title: Re: Send Text to AxonII from Hyperterminal
Post by: waltr on June 30, 2010, 04:09:45 PM
Quote
So I assume that using uartGetByte(uart0); Would return CR? but wouldnt that be two characters?

Nope, a 'CR' is 0x0D in Hex or 00001101 in binary (notice that I show 8 bits).

When coding at the uController level THINK in BINARY (or hex). For the uController doesn't know anything about keyboards, display screens or characters, it only knows about bits and only 8 at a time.
Title: Re: Send Text to AxonII from Hyperterminal
Post by: dellagd on June 30, 2010, 04:31:30 PM
so this 'CR' could be put into one char variable?
Title: Re: Send Text to AxonII from Hyperterminal
Post by: Razor Concepts on June 30, 2010, 04:36:37 PM
Yup. CR is just the abbreviation of carriage return, nothing actually comes out as CR
Title: Re: Send Text to AxonII from Hyperterminal
Post by: dellagd on June 30, 2010, 04:52:19 PM
so in hyperterminal when I press return uartGetByte(UART0) will sense that and use it as and actual key press just like if I pressed 'r'? (just to clarify)

I could do...

int ch = uartGetByte(UART0);
if(ch!=-1){

char Typed_letter = ch & 0xff
     
        if (Typed_letter == "CR")
          { // do something?  }


}

IS that how I would go about using it?

Title: Re: Send Text to AxonII from Hyperterminal
Post by: waltr on June 30, 2010, 08:51:29 PM
Quote
if (Typed_letter == "CR")

This isn't quite right. In C a double quote is for a string, more than one character and the compiler should complain as you are trying to compare a value of type char with a string. The "CR" would really be 0x43, 0x52 in two bytes (char type).
For a single character use single quotes like you did for 'r'. Also there needs to be a macro that defines 'CR' as the value 0x0D but this is still not the way to do it.

The C standard for a CR is '\r'. This is called an "escape sequence" as the slash character indicates to the compiler that the next character is not to be taken literally but has a different meaning.

I did post above about thinking in binary or hex, right?

What will work is:
if (Typed_letter == 0x0d)   // this is an ASCII CR

or
if (Typed_letter == 0b00001101)  // this is also an ASCII CR

or
if (Typed_letter == 13)  // this is also an ASCII CR

or
if (Typed_letter == '\r')    // this is a CR in C

All of these have exactly the same value.

There are a few online books on the C programming language that would help you. If you want to buy a book then there is only one I recommend and that's:
"The C Programming Language" by Brain W. Kernighan and Dennis M. Richie.
I bought mine in 1987 and still use to as THE reference.

Title: Re: Send Text to AxonII from Hyperterminal
Post by: dellagd on July 01, 2010, 05:10:27 AM
so you could just use hexadecimal fright off the bat without doing any conversions on 'Typed_letter'?

That makes it simple
Title: Re: Send Text to AxonII from Hyperterminal
Post by: waltr on July 01, 2010, 07:02:17 AM
Yep. I use hex a lot so that I know exactly what is in the code or put the comment in hex so in the debugger I don't have to convert to know if it matches.

Windows has a Calculator that does number base conversions. Very handy to enter a hex value and see its decimal or binary equivalent.
Title: Re: Send Text to AxonII from Hyperterminal (new problem)
Post by: dellagd on July 01, 2010, 04:23:53 PM
ok ok

I implemented everything you guys said I should. Here is my hardware.h (made by Webbot lib Project designer) and main .c file.

Main .c file:
Code: [Select]
#include "hardware.h"
int letter = -1;
char password[9];
uint8_t counter=0;
// Initialise the hardware

void message();

void appInitHardware(void) {
initHardware();
}

// Initialise the software
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
return 0;
}

// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {

while(SWITCH_released(&button)){
// released
}

// -------- End   Switch/Button-------
rprintf("Enter Password:");

letter = uartGetByte(uart1);

while(letter != 0x0d)
{
//store command data
letter = uartGetByte(uart1);

if(letter!=-1)//has new data
{
password[counter]=letter;//store data
counter++;
}
}

if (password[1] == 's' && password[2] == 'e' && password[3] == 'm' && password[4] == 'i' && password[5] == 'n' && password[6] == 'o' && password[7] == 'l' && password[8] == 'e' && password[9] == 's')
{
message();
}
// -------- Start Marquee-------
// Marquee - see 'segled.h'
// Before using the Marquee you need to redirect rprintf to write to it
// This can be done using

Now the .h file (It may help)

Code: [Select]
/*
This file has been auto-generate by WebbotLib tools V1.1
** DO NOT MODIFY BY HAND **
*/
#ifndef _HARDWARE_H_
#define _HARDWARE_H_
#include "xhardware.h"
// Type of interrupt handler to use for uart interrupts.
// Do not change unless you know what you're doing.
#ifndef UART_INTERRUPT_HANDLER
#define UART_INTERRUPT_HANDLER ISR
#endif

// ------------------- uart1 -------------------

// Create hardware UART uart1
HW_UART _uart1 = MAKE_UART_BUFFERED(null,null,UCSR1A,UCSR1B,UBRR1L,UBRR1H,UDR1,null,BV(U2X1),D2,D3,&uart1GetByte,&uart1SendByte);

// Create a routine to write bytes to uart1
// You can set rprintf to use it by calling rprintfInit(&uart1SendByte)
MAKE_WRITER(uart1SendByte){
return uartSendByte(uart1,byte);
}

// Create a routine to read a byte from uart1
// Returns -1 if there was no data
MAKE_READER( uart1GetByte){
return uartGetByte(uart1);
}

#ifndef USART1_TX_vect
# error Uart1 Tx complete vector undefined
#else
UART_INTERRUPT_HANDLER(USART1_TX_vect){
uartTransmitService(uart1);
}
#endif

#ifndef USART1_RX_vect
# error Uart1 Rx complete vector undefined
#else
UART_INTERRUPT_HANDLER(USART1_RX_vect){
uartReceiveService(uart1);
}
#endif

// ----------- Define the ADC channels ----------
const uint8_t NUM_ADC_CHANNELS = 16;

// ----------- My devices -----------------------
SWITCH button = MAKE_SWITCH(D5);
SEGLED led_display = MAKE_SEGLED(C3,C2,C0,D6,D7,C4,C5,null,false);
static SEGLED_LIST marquee_list[] = {&led_display};
MARQUEE marquee = MAKE_MARQUEE(marquee_list,500000,2000000,&marquee_put_char);
// Create a Writer to write to marquee
MAKE_WRITER(marquee_put_char){
return marqueeSendByte(&marquee,byte);
}

// ----------- Initialise built in devices ------
void sysInitHardware(void){
SWITCH_init(&button);
setErrorLog(&uart1SendByte);
rprintfInit(&uart1SendByte);
uartInit(uart1,115200);
segled_init(&led_display);
}

// ----------- Initialise my added devices ------
void initHardware(void){
}
// ----------- Register the statusLED -----------
void registerLED(void){
statusLEDregister(C1,false);
}

// ----------- Ports are configured on the fly --
void configure_ports(void){
}

#endif

When I run this code on my Axon II this is what happens:

1. I press the on-axon button to give me the "Enter password" prompt
2. I begin to type the password but nothing appears on the Hyperterminal window. )I continued to type thinking that it may just not appear) I press enter and then after a few seconds I get WebbotLib Error: 11

What is this?What does it mean? My code compiled with 0 errors and warnings.
Title: Re: Send Text to AxonII from Hyperterminal (new problem)
Post by: Admin on July 01, 2010, 04:35:21 PM
Quote
2. I begin to type the password but nothing appears on the Hyperterminal window. )I continued to type thinking that it may just not appear) I press enter and then after a few seconds I get WebbotLib Error: 11

What is this?What does it mean? My code compiled with 0 errors and warnings.
There is an option in the menu of Hypterterminal to echo any character you type (so you can see it).

Error 11 is a receive buffer overflow (see the errors.h file in WebbotLib). This happens when you send a bunch of data but the mcu doesn't process it before the buffer overflows.

Try increasing buffer size in Project Designer. If your baud isn't matching, the 'random junk' can quickly fill a small buffer. Have your mcu repeat back what it sees to verify baud (if you aren't sure).

Also, a bug in your code . . . you forgot to look at password[0]. ;D
Title: Re: Send Text to AxonII from Hyperterminal (new problem)
Post by: dellagd on July 01, 2010, 05:38:10 PM
both of what you said fixed it.

I can now see the text and the password entered correctly after I changed the arrays.

Those little buggers...

+1 helpful!
Title: Re: Send Text to AxonII from Hyperterminal (new problem)
Post by: Webbot on July 01, 2010, 09:25:05 PM
If its of interest then you still have some bugs in your code.
1. Your array for storing the entered password is only 9 characters long. What if they keep pressing keys - say like 2000 keypresses? After the first 9 you will be writing over all sorts of stuff and the program will go mad
2. You read the uart just before your while loop and then immediately inside inside your while loop. So if the first read returned anything other than -1 (ie a character) then you will be throwing it away.
3. You shouldn't be doing this in appControl as that is your main loop. So every time round the loop you could be asking for a password - although it depends on the button press. You may want to think about doing it in appInitSoftware instead as that is only ever called once - before the first call to appControl

Glad you got it going
Title: Re: Send Text to AxonII from Hyperterminal (new problem)
Post by: dellagd on July 02, 2010, 09:06:51 AM
Thankyou webbot for your help.

Starting on the 'exceeding the arrays capacity' what do you sudgest?
I guess I could do

Code: [Select]
If (counter < 7) { counter++ }
instead of just

Code: [Select]
counter++
Is there a simpler "short-hand" trick to put a limit on how high a variable can go?


As for the repeating the password entery, that was just a test program and I was just trying to figure out how to work the whole password thing. Since then I have created a .h file for the password:

Code: [Select]
/* This is the Header file for password protecting any made programs for the Axon II
                 ________      ____      ____       ____
                |        |     \   \    /    \     /   /
                |        |      |   |   |     |    |   |
                |_______/       \   \  /      \   /   /
                |                |   | |       |  |   |
                |                \   \/         \/   /
                |                 |__________________|

                       _____
                      |     |
                      |     |    ____    ____
                      |___ /   /       /     \
                      |        |       |     |
                      |        |       \_____/                               */

int letter = -1;
char password[9];
int counter=0;


void Pass_pro()
{
  while(SWITCH_released(&button)){
                // released
        }


  while (password[0] != 's' || password[1] != 'e' || password[2] != 'm' || password[3] || 'i' && password[4] || 'n' && password[5] != 'o' || password[6] != 'l' || password[7] != 'e' || password[8] != 's')
  {
        rprintf("Enter Password:\n");

        letter = uartGetByte(uart1);

    while(letter != 0x0d)
                    {
                    //store command data
                    letter = uartGetByte(uart1);

                   if(letter!=-1)//has new data
                            {
                            password[counter]=letter;//store data
                            counter++;
                            }
                            }
  }

}

Now from your main code you would just call Pass_pro(); and boom, you must enter the correct password before it will continue in the code. Yes I would put this in the appInitSoftware() function.




Title: Re: Send Text to AxonII from Hyperterminal (new problem)
Post by: Admin on July 02, 2010, 11:22:18 AM
Webbot, that is a thought . . . adding a password protection function into the library.

Hijacking another robot/microcontroller with any wireless capability would be easy without it . . .
Title: Re: Send Text to AxonII from Hyperterminal (new problem)
Post by: dellagd on July 02, 2010, 02:28:13 PM
my point exactly

I'm working on it so that it will only let you back to the main program in you type in the password and hit enter.

The only current problem is that if you go past 9 characters (the passwords legnth) it will give you "Wrong Password"
That would let the hijacker know the password is 9 characters long.

working on it...
Title: Re: Send Text to AxonII from Hyperterminal (new problem)
Post by: Admin on July 02, 2010, 04:17:15 PM
Wait, no 256 bit data encryption?! ;) :P
Title: Re: Send Text to AxonII from Hyperterminal (new problem)
Post by: Webbot on July 02, 2010, 05:32:29 PM
Webbot, that is a thought . . . adding a password protection function into the library.

Hijacking another robot/microcontroller with any wireless capability would be easy without it . . .

I don't see that as a function of my robot library as its purely computational. ie if Dellagd can use WebbotLib to write his own then thats good enough for me as it means others can. After all - no point in having an 'open source' encryption/password function unless it uses things like one way encryption.
Title: Re: Send Text to AxonII from Hyperterminal (new problem)
Post by: dellagd on July 02, 2010, 05:40:58 PM
Wait, no 256 bit data encryption?! ;) :P

unfortunately no John (I may call you john right? Admin sounds so.... General...)
Title: Re: Send Text to AxonII from Hyperterminal (new problem)
Post by: Admin on July 02, 2010, 05:45:52 PM
geez I was joking about the encryption!
(but Webbot, think about that secret project I'm working and how a basic password function could benefit it)

Quote
unfortunately no John (I may call you john right? Admin sounds so.... General...)
How about General Admin? ;D
Title: Re: Send Text to AxonII from Hyperterminal (new problem)
Post by: dellagd on July 02, 2010, 06:27:23 PM
Were not in the army here

-Sergeant Dellagd

PS I was being sarcastic about the 'unfortunately no'
PPS In my mind I knew you were going to say that :P..... John
Title: Re: Send Text to AxonII from Hyperterminal (new problem)
Post by: Webbot on July 02, 2010, 06:34:01 PM
Long response follows for a difficult subject......

For all encryption problems you have to think of the client application encrypting the data, transmitting it, and the receiver decrypting it back to the original.

The transmit phase is the point where sniffers/hackers may be able to see the data on the wire.

If your transmit medium is a physical cable then its easy - as its hard for a sniffer to plug in an extra cable to see the messages without you noticing.

But once you go with a wireless medium, at a robo competition say, then there may be lots of other folks (hackers) with their own wireless laptops.

Lets take the simple scenario of 'no encryption'. You type the password 'ABCDEF' into Hyperterminal on your laptop and each character is sent over the wireless link. It is received by the robot but also by all the hackers. They now know your password and can use it themselves. So the addition of a password is useless.

The next step is to apply encryption/decryption at both ends of the message. ie on your laptop you type in 'ABCDEF' but it gets sent as 'NO1DEG', the robot receives this and decrypts it back to 'ABCDEF'. Sounds cool. But first off all you cant use hyperterminal. You need to write a custom app for your PC that takes the characters 'ABCDEF' and encrypts them into 'NO1DEG' before sending them. In the meantime the hackers see 'NO1DEG' as the message going over the wireless. They may not know about the encryption mechanism but they dont have to. They can just type 'NO1DEG' into Hyperterminal to crack your code - they dont need to know that what you originally typed was 'ABCDEF'.

You could go a step further. ie when switching on the robot it could create a random key (from ADCs, IMU readings etc) that is used for encrypt/decrypt process - and in this way the key would be different every time. But your client app would need to know what key to use. The robot could send the key to the pc (un-encrypted). This would work and would be reasonsably secure. Howerver: if I make this part of WebbotLib then if any of the hackers have WebbotLib then their PC knows everything yours does ! Hence not a good thing to add to a library.

But lets go back and challenge the problem. Your robot is sending/receiving data to/from the PC. Does it matter if someone else can see it? Of course certain telemetry data could just be written to an sdCard for later review and never transmitted at all.

If the answer is YES then the simplest thing of all is to not let the hackers connect their laptop to your wireless link in the first place. So with Bluetooth, for example, you can say if it is a secure link and give a password. If the hackers dont know the password then they cannot join the wireless link - and so you don't then need to encrypt/decrypt the data on the link.But then thats a hardware thing and nothing that WebbotLib can do to facilitate.
Title: Re: Send Text to AxonII from Hyperterminal (new problem)
Post by: dellagd on July 02, 2010, 06:58:58 PM
I see Webbot I  really appreciate your information-

Tomorrow I am at a golf tournament and I cant work on this but I would really like to attempt the multiple keys for letters typed for password. I would ask the axon what key it has randomly selected. I also use VB occasionally on my computer so I could whip up a quick program to convert whatever I enter into the correct key(That the axon selected) Enter that into hyperterminal and then send. No need to create a new hyperterminal and relatively secure.

Sound good?