--- /dev/null
+==================================================\r
+How to use this sample source\r
+\r
+1. Compile\r
+ \r
+ % gcc -o SerialTest SerialTest.c\r
+\r
+ Executable file "SerialTest" is created.\r
+\r
+2. Execute\r
+ \r
+ % ./SerialTest /dev/ttyS0 logfile.dat\r
+ \r
+ Data will be obtained and saved in to the file "logfile.dat" from the URG Connected to first port (COM1) \r
+ If URG is connected to other ports change /dev/ttyS0 as follows\r
+ \r
+ COM2: /dev/ttyS2\r
+ USB: /devttyACM0\r
+ (All distributions may not have same device file name)\r
+\r
+3. Output file\r
+ \r
+ Output file "logfile.dat" after running the sample program have the following format.\r
+-------------------------------------------\r
+[Step Number] [distance] (first scan)\r
+[Step Number] [distance] \r
+ : (until step 786)\r
+[Empty Row]\r
+[Step Number] [distance] (second scan)\r
+[Step Number] [distance] \r
+ : (until step 786)\r
+[Empty Row]\r
+ : :\r
+\r
+(same until 10 scan)\r
+---------------------------------------------\r
+Data is seperated by space and line feed so it can be directly used to plot graph using "gnuplot" by following command\r
+ % gnuplot\r
+ >splot "logfile.dat" using 2\r
+\r
--- /dev/null
+/*!
+ @File
+ @Brief: This is a demo program to obtain 10 scan data from URG-Series and save them into the file.
+ @Note : This sample source is a referece for the user when creating his/her program and does not gurantee for users program
+ @Note : Hokuyo Automatic Co., Ltd does not bear responsibility for any loss or damage caused due to this demo program.
+ @Note : Mention the publisher's name when distributing this source or its part to the third party
+ @Publisher HOKUYO AUTOMATIC CO.,LTD.
+ @author Tomoaki Yoshida
+ @Date 2005/03/11
+ */
+#include<stdio.h>
+#include<stdlib.h>
+#include<sys/termios.h>
+#include <string.h>
+// SERIAL PORT RELATED FUNCTIONS
+
+//! SPEED SETTINGS
+/*!
+ @param aBR [in] Declared for communication speed (9600-500000)
+ @return cfset[i|o] Returned value for speed function
+ Returns B0 if failed
+ */
+speed_t bitrate(int aBR)
+{
+ switch(aBR)
+ {
+ case 9600:
+ return B9600;
+
+ case 19200:
+ return B19200;
+
+ case 38400:
+ return B38400;
+
+ case 57600:
+ return B57600;
+
+ case 115200:
+ return B115200;
+
+ case 230400:
+ return B230400;
+
+ case 460800:
+ return B460800;
+
+ case 500000:
+ return B500000;
+
+ default: // invalid bitrate
+ return B0;
+ }
+}
+
+/* Parameter settings for serial port communication
+ @param aFd [in] File descriptor to point serial port
+ @param aSpeed [in] Communication speed (bps)
+ @retval 0 Failed
+ @retval 1 Success
+ */
+int setupSerial(int aFd, int aSpeed)
+{
+ int speed;
+ struct termios tio;
+
+ speed=bitrate(aSpeed);
+ if(speed==0)
+ return 0; // invalid bitrate
+
+ tcgetattr(aFd,&tio);
+
+ cfsetispeed(&tio,speed); // bitrate
+ cfsetospeed(&tio,speed); // bitrate
+
+ tio.c_cflag=(tio.c_cflag & ~CSIZE) | CS8; // data bits = 8bit
+
+ tio.c_iflag&= ~( BRKINT | ICRNL | ISTRIP );
+ tio.c_iflag&= ~ IXON; // no XON/XOFF
+ tio.c_cflag&= ~PARENB; // no parity
+ tio.c_cflag&= ~CRTSCTS; // no CTS/RTS
+ tio.c_cflag&= ~CSTOPB; // stop bit = 1bit
+
+ // Other
+ tio.c_lflag &= ~( ISIG | ICANON | ECHO );
+
+ // Commit
+ if(tcsetattr(aFd,TCSADRAIN,&tio)==0)
+ return 1;
+ return 0;
+}
+
+//! Open and set up specified serial port
+/*!
+ @param aDevNme [in] Serial port device file name
+ @param aSpeed [in] Communication speed(bps)
+ @return Serial port file handle
+ Returns NULL when failed to open.
+ */
+FILE* openSerial(char *aDevName, int aSpeed)
+{
+ FILE* dev;
+
+ dev=fopen(aDevName,"w+");
+ if(dev==NULL)
+ return NULL; // invalid device file
+
+ // setup parameters
+ if(setupSerial(fileno(dev),aSpeed))
+ return dev;
+
+ // fail
+ perror("setting term parameters");
+ fclose(dev);
+ return NULL;
+}
+
+//
+
+//! Obtain scanned data
+/*!
+ @param aPort [in] Serial port file handle where URG is connected
+ @param aStart [in] Starting scan step from (0-768)
+ @param aEnd [in] Ending scan step at (0-768)
+ @param aSkip [in] Number of steps to be merged when multiple points are combined(1-99)
+ @return Obtained distance data
+ memory pointer is malloc. Should be freed after use.
+ Returns NULL when failed
+ */
+int* urgGetScan(FILE* aPort, int aStart, int aEnd, int aSkip)
+{
+ char req[16];
+ int *scan,*pt;
+ char line[80];
+ int i;
+
+ sprintf(req,"G%03d%03d%02d\n",aStart,aEnd,aSkip);
+ i=fwrite(req,sizeof(char),strlen(req),aPort);
+ if(i==0)
+ return NULL;
+ // echo-back
+ if(fgets(line,sizeof(line),aPort)==NULL)
+ return NULL;
+ if(strcmp(req,line)!=0)
+ {
+ // invalid
+ return NULL;
+ }
+
+ // status
+ if(fgets(line,sizeof(line),aPort)==NULL)
+ return NULL;
+ if(strcmp("0\n",line)!=0)
+ {
+ //invalid
+ return NULL;
+ }
+ // result
+ scan=(int*)malloc(sizeof(int)*((aEnd-aStart)/aSkip+1));
+ pt=scan;
+ while(1)
+ {
+ if(fgets(line,sizeof(line),aPort)==NULL)
+ {
+ free(scan);
+ return NULL;
+ }
+ if(!strcmp("\n",line))
+ {
+ //end of result
+ break;
+ }
+ for(i=0;i<strlen(line)-1;++i,++pt)
+ {
+ // Note: It is assumed that received data is complete
+ // (Problem will occour if data is incomplete)
+ *pt=((line[i] - 0x30)<<6) + line[++i] - 0x30;
+ }
+ }
+ return scan;
+}
+
+
+
+main(int argc, char **argv)
+{
+ FILE *dev,*output;
+ int *scan;
+ int i,pt;
+
+ if(argc!=3)
+ {
+ printf("USAGE:\n");
+ printf(" %s portname outputfilename\n",argv[0]);
+ printf(" ex) %s /dev/ttyS0 logfile.dat\n",argv[0]);
+ exit(0);
+ }
+
+ // Open serial port (19200bps)
+ dev=openSerial(argv[1],19200);
+ if(dev==NULL)
+ {
+ perror("Could not open serial port");
+ exit(1);
+ }
+
+ // Open output file
+ output=fopen(argv[2],"w");
+ if(output==NULL)
+ {
+ perror("Could not open output file");
+ exit(1);
+ }
+
+ // Obtain data for 10 scan
+ for(i=0;i<10;++i)
+ {
+ printf("Trying scan #%d\n",i);
+ // Obtain all step (0-768) distance data
+ scan=urgGetScan(dev,0,768,1);
+ if(scan!=NULL) // save data in a file if succeeded
+ {
+ printf("Scan Complete.\n");
+ for(pt=0;pt<=768;++pt)
+ {
+ fprintf(output,"%d %d\n",pt,scan[pt]);
+ }
+ free(scan);
+ fprintf(output,"\n");
+ }
+ else
+ {
+ fprintf(stderr,"scan #%d failed",i);
+ }
+ }
+ printf("Complete!\n");
+}