Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <inttypes.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <string.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <termios.h>
- #include <sys/ioctl.h>
- int SerialConnection();
- int serial_fd;
- int main(int argc, char* argv[])
- {
- char buffer[255]; /* Input buffer */
- char *bufptr; /* Current char in buffer */
- int nbytes; /* Number of bytes read */
- if(argc != 2)
- {
- printf( "Usage: %s <Serial Port>n",argv[0]);
- exit(1);
- }
- serial_fd = SerialConnection(argv[1]);//Open serial port
- if(serial_fd < 1)
- {
- printf( "Serial Port Open Failuren");
- exit(1);
- }
- printf( "Serial Port Open Succesfullyn");
- while(1)
- {
- memset(buffer, 0, 255); //clear buffer
- int ELMInit = 1;
- switch(ELMInit)
- {
- case 1:
- printf( "Request ATZ Reset ELM Adaptern");
- write(serial_fd, "ATZr", 4);
- bufptr = buffer;
- while ((nbytes = read(serial_fd, bufptr, buffer + sizeof(buffer) - bufptr - 1)) > 0)
- {
- printf("current buffer %s n",buffer);
- bufptr += nbytes;
- if (bufptr[-1] == 'r')
- {
- printf("carriage return found %s n",buffer);
- if(strstr(buffer, "ELM327") != NULL)
- {
- printf("success %sn",buffer);
- }
- break;
- }
- *bufptr = '