Serialib ::: Simple Serial Library v1.2

Example1.cpp

00001 
00017 #include <stdio.h>
00018 #include "serialib.h"
00019 
00020 
00021 #if defined (_WIN32) || defined( _WIN64)
00022 #define         DEVICE_PORT             "COM1"                               // COM1 for windows
00023 #endif
00024 
00025 #ifdef __linux__
00026 #define         DEVICE_PORT             "/dev/ttyS0"                         // ttyS0 for linux
00027 #endif
00028 
00029 
00030 int main()
00031 {
00032     serialib LS;                                                            // Object of the serialib class
00033     int Ret;                                                                // Used for return values
00034     char Buffer[128];
00035 
00036 
00037 
00038 
00039     // Open serial port
00040 
00041     Ret=LS.Open(DEVICE_PORT,115200);                                        // Open serial link at 115200 bauds
00042     if (Ret!=1) {                                                           // If an error occured...
00043         printf ("Error while opening port. Permission problem ?\n");        // ... display a message ...
00044         return Ret;                                                         // ... quit the application
00045     }
00046     printf ("Serial port opened successfully !\n");
00047 
00048 
00049 
00050 
00051     // Write the AT command on the serial port
00052 
00053     Ret=LS.WriteString("AT\n");                                             // Send the command on the serial port
00054     if (Ret!=1) {                                                           // If the writting operation failed ...
00055         printf ("Error while writing data\n");                              // ... display a message ...
00056         return Ret;                                                         // ... quit the application.
00057     }
00058     printf ("Write operation is successful \n");
00059 
00060 
00061 
00062 
00063     // Read a string from the serial device
00064     Ret=LS.ReadString(Buffer,'\n',128,5000);                                // Read a maximum of 128 characters with a timeout of 5 seconds
00065                                                                             // The final character of the string must be a line feed ('\n')
00066     if (Ret>0)                                                              // If a string has been read from, print the string
00067         printf ("String read from serial port : %s",Buffer);
00068     else
00069         printf ("TimeOut reached. No data received !\n");                   // If not, print a message.
00070 
00071 
00072 
00073     // Close the connection with the device
00074 
00075     LS.Close();
00076 
00077     return 0;
00078 }
00079 
00080 
 All Classes Functions