How can I make a window code work with linux?

First question - which serial port are you connecting to? You say you have a “serial adapter”, but is it the Pololu USB to serial adapter, the Pololu 23201a serial adapter, or something else? Your C code seems to be connecting to /dev/ttyS0, which is COM1 in Windows. Is that what you also tried with cutecom?

I’d start by removing the Orangutan and using cutecom with an LED on the serial output line to make sure that you are getting a signal there. It should blink when you are sending serial data.

Your linux C code looks close to working, but you’ll have to type ‘w’ then ENTER to get it to receive your command. Your Orangutan C code has some very obvious errors that the C compiler is probably reporting to you. Did you get it to compile without any warnings? In particular, typing a string between single quotes (‘fwd’) doesn’t make any sense in the C language. Single quotes are used for single characters; double quotes are used for strings.

-Paul

Thanks!

where should I connect the led in the pololu serial adapter 23201a?
I got only 1 serial port on my computer so I guess that it’s ttys0.
I connected the LED’s GND to the adapters GND and the other LED’s leg to the TX. But no blink there…
there is another TX in the serial adapter, the one that is set low, should I use that?

Do not ever connect an LED directly between a signal and ground! If you do that, you risk destroying either the serial adapter or the LED. Here’s the circuit that you should be using whenever you connect an LED to anything:

http://en.wikipedia.org/wiki/LED_circuit

Please take a look at that and understand why you need to have a resistor in the circuit.

One more question - what power supply are you using for the serial adapter?

-Paul

I don’t use power supply to the adapter. I thought that it gets the power from the serial port…
I tried to put double quotes on the orangutan program and got an error about that. I didn’t get an error when I used single quotes…
So, should I connect a power supply to the adapter? and use a 3V-5.5V, connecting Vcc to the plus and GND to GND?

O.K it was the power supply! Now it is working! what about getting information from the orangutan to the computer? is there a READ function?

I really doubt that have everything working yet, since the code you posted earlier had obvious errors in it. Have you fixed the errors involving your strings like ‘fwd’?

Anyway, here is a good tutorial covering I/O in C. As you will see, there are many different functions that you can use, including a read() that corresponds to write().

cs.cf.ac.uk/Dave/C/node18.html

-Paul

Thanks!
First off all, I fixed the Orangutan problems.
Second, I looked at the link you sent me, and I am afraid I didn’t get it… There he talks about using files, but I am not sending files, I am sending bytes (right???). Can I use scanf for this??? I am not sure…
Any how, I understand that the read function is very much like the write function, I got one question though, when I use write, the syntax is: write(fd, “stuffToSend”, number ob bytes) right?

So in the read function it should be: read(fd, “I don’t know what to put here!”, I don’t know what to put here to…). So, I am missing 2 parameters for the read function.

In the example you sent me he dose this:

[code]float bigbuff[1000];
int main()

int fd;
int bytes_read;
int file_length

bytes_read = read(fd, bigbuff, file_length);[/code]

well, something like that… I didn’t write the whole thing. will that function read the bytes sent from the Orangutan?

O.K. Here is what I’ve done:

orangutan code:

    /*Serial Control Test Program for Orangutan or Baby Orangutan
    V 1.1
    Adam Borrell
    5/29/08*/

    #define F_CPU 20000000//CPU clock for Baby Orangutan
    //#define F_CPU 8000000//CPU clock for Orangutan
    //***Uncomment ONE of the above lines for the specific device***

    #define BAUD 9600//baud rate for UART
    #define MYUBRR (F_CPU/16/BAUD-1)//baud rate variable for UART hardware

    #include <avr/io.h>
    #include <avr/interrupt.h>
    #include <util/delay.h>

    unsigned volatile char dataIn,newSerCmd=0,wasPressedAlready=0;

    void USART_Init(unsigned int ubrr){//Initialize USART hardware & settings for Serial Radio
       UBRR0H=(unsigned char)(ubrr>>8);//set buad rate
       UBRR0L=(unsigned char) ubrr;
       UCSR0B=(1<<TXEN0)|(1<<RXEN0)|(1<<RXCIE0);//enable transmitter & receiver, receive complete interrupt
       UCSR0C=(3<<UCSZ00);//Set frame format for 8bit with 1 stop
    }

    void USART_Trans(unsigned char data){//Transmit a byte of data over USART
       while(!(UCSR0A&(1<<UDRE0)));//wait for transmition to complete
       UDR0=data;
    }

    ISR(USART_RX_vect){//USART Byte reieved
       dataIn=UDR0;//grab copy of serial byte
       newSerCmd=1;//indicate new byte received
    }

    int main(){
       DDRB|=(1<<PB0);//setup output pins
       DDRB &=~ (1<<PB3);
	   PORTB |=(1<<PB3);

       USART_Init(MYUBRR);//Initialize USART
       sei();//enable global interrupts

       while(1){
          if(newSerCmd){//if new byte received
             switch(dataIn){
                case 'f'://forward
                   PORTB|=(1<<PB0);
				   //PORTD&=~(1<<PD5);
                   break;
                case 's'://reverse
                   PORTB&=~(1<<PB0);
                   //PORTD|=(1<<PD5);
                   break;
                case 130://stop
                   PORTB&=~(1<<PB1);
                   PORTD&=~(1<<PD5);
                   break;
                default:
                   break;
             }
             newSerCmd=0;
          }

          if((PINB&((1<<PB3)|(1<<PB4)|(1<<PB5)))){
             if(!wasPressedAlready){
                USART_Trans('a');//transmit byte
                wasPressedAlready=1;
             }
          }else if(wasPressedAlready){
             wasPressedAlready=0;
             _delay_ms(10);//debounce delay
          }
       }
       return 0;
    }

And the linux code:

/* functions for communicating with the pololu sadapter over a serial port */

#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <sys/select.h>

/* write a message to the SSC */
/*void sendCommand(Sadapter, char id, char cmd, char servo, char data) {
  char buf[5];
  snprintf(buf,5,"%c%c%c%c%c",0x80,id,cmd,servo,data);
  write(fd,buf,5);
}

void sendCommand6(Sadapter, char controller, char cmd, char servo, char data1, char data2) {
  char buf[6];
  servo += controller*16;
  snprintf(buf,6,"%c%c%c%c%c%c",0x80,0x01,cmd,servo,data1,data2);
  write(fd,buf,6);
}

int enable(Sadapter, int controller, int servo, int enabled) {
  if(!connected) { return 0; }

  if(enabled) {
    sendCommand((char)controller,0,(char)servo,(char)0x40);
    printf("enabled %i\n",servo); fflush(stdout);
  } else {
    sendCommand((char)controller,0,(char)servo,0x00);
    printf("disabled %i\n",servo); fflush(stdout);
  }

}

int stepTo(Sadapter, int controller, int servo, int pos) {
  if(!connected) { return 0; }

  sendCommand6((char)controller,4,(char)servo,(char)(pos/128),(char)(pos%128));

  return 1;
}
*/
int connected, fd, done=0;
char idstring[26];
void endSession() {
  if(!connected) return;
  close(fd);
}

int connectSadapter() {
  struct termios options;
  int count,ret;

  connected=0; /* reset in case we fail to connect */

  fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
  if (fd == -1) return 0;
  else fcntl(fd, F_SETFL, 0);

  tcgetattr(fd, &options);

  /* go to 9600 baud */
  cfsetispeed(&options, B9600);
  cfsetospeed(&options, B9600);

  options.c_cflag |= (CLOCAL | CREAD); /* enable */

  options.c_cflag &= ~PARENB; /* 8N1 */
  options.c_cflag &= ~CSTOPB;
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;

  /* set all of the options */
  tcsetattr(fd, TCSANOW, &options);

  connected=1;
  return 1;
}

int main()
{//I added the devInput variable
	char input[32];
	char devInput[32];
	
	if(done){
          printf("Program Terminated!\n");
          system("PAUSE");
          return -1;
       }
	
	printf("\n\nPololu Serial Adapter program\n\n");
	printf("Commands:\n");
	printf("Forward - w\n");
	printf("Reverse - s\n");
	printf("Stop - x\n");
	printf("Exit - q\n");
	//connect to serial adapter
	connectSadapter();
	
	while (!done)
	{
		*input=getchar();
		switch(*input)
		{
			case 'q'://exit the program
				done = 1;
				printf("Ending ssesion and exitting program\n");
				endSession();
				break;
			case 'w'://forward
				printf("Forward\n");
				write(fd,"f",1);
				break;
			case 's'://reverse
				printf("Reverse\n");
				write(fd,"back",4);
				break;
			case 'x'://stop
				printf("Stop\n");
				write(fd,"s",1);
				break;
			default:
				break;
		}
		
	}
	//I added this read function
	*input = read(fd, devInput, 1);
	if (*input == 'a')
	{
		printf("I got A!!! It's WORKING!!!\n");
	}
	
}
const char *getId() {
  return idstring;
}

what do you think?

You didn’t say what happened when you ran the program, but I think it won’t work. A good way to debug it would have been to print out the exact value returned by read(), as a number, on your screen.

Here is a more detailed description of the read command:

linux.die.net/man/2/read

The return value is the number of bytes that was read into the buffer, but your program is expecting to find a character there. Instead, you should be looking at the buffer.

Also, you should know that in UNIX a device like /dev/ttyS0 is treated as if it was a real file. So you can use fopen, fprintf, etc. on that device. You might find it useful to read about the fdopen command here:

linux.die.net/man/3/fopen

This would create a FILE structure out of your fd variable that you could use in those other commands. I recommend reading the manual pages for the C functions that you are using - in most cases they say extremely clearly exactly what they do, and you won’t have to guess.

Install the “manpages-dev” package on your ubuntu computer, and you’ll be able to get to any of them by typing “man fdopen” or whatever you want.

-Paul

-Paul

Here is what I’ve done:

I added this read function. In the beginning I wrote the ssize_t but I got an error for that so I tried this:

devInput = read(int fd, void *buf, size_t count);
	
	
		printf(devInput);

Note that devInput us a float and I got an error with the print f, I think I forgot to add %d or something like that…

Here are the errors I got when compiling with ssize_t and without it:
serialAdapterC.c: In function ‘main’:
serialAdapterC.c:131: error: expected expression before ‘ssize_t’
serialAdapterC.c:134: warning: passing argument 1 of ‘printf’ from incompatible pointer type
arbel@arbel-desktop:~/Desktop/serial$ gcc -o serialAdapterC serialAdapterC.c
serialAdapterC.c: In function ‘main’:
serialAdapterC.c:131: error: expected expression before ‘int’
serialAdapterC.c:131: error: too few arguments to function ‘read’
serialAdapterC.c:134: warning: passing argument 1 of ‘printf’ from incompatible pointer type

I read all the stuff you gave mt several times and I can’t seem to get it :frowning:
THANKS
Arbel

Hello,
I think the troubles you are having come from not understanding the C language itself. You have to make sure that you understand every single word that you type, or you are not going to be able to make your programs work. I think that in this case you are getting confused between defining a function and calling it. You don’t need to specify the types of your variables in a function call.

This site looks like it has a pretty detailed explanation and a ton of examples:

wsz.pl/~piotrazz/zajecia/C/r … tions.html

(I linked you directly to the explanation of functions - click on “Top” at the bottom to go to the top level.)

There are lots of sites with great introductions to C that you can find with a Google search, so I recommend that you go look for some, go over some C tutorials in detail, and make sure that you understand what you are doing!

By the way, where did you learn your C? Do you know any other languages better than C?

-Paul

I learned all my programming from book and from the internet. You are right, I am not so good at C but I am trying… I feel a bit more comfortable with visual basic and gambas, it is more intuitive for me. the problem is that I can’t go so far with learning C or any language without help. when I read something (as detail as it may be) I am still missing some information about stuff I don’t fully understand. So I try but it is hard to get the complected stuff all by my self…