3 * Copyright 2019 AT&T Intellectual Property
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
24 #include "adruino_serial.h"
26 int start_serial_inferface(int baudrate, char *serial_port)
30 fd = serialport_init(serial_port, baudrate);
32 fprintf(stderr, "couldn't open serial port %s\n", serial_port);
35 fprintf(stderr, "Openning serial port: %s ...\n", serial_port);
38 serialport_flush(fd); // take 2 seconds
39 fprintf(stderr, "Serial port ready!\n");
45 int serial_readline(int fd, char* buf, int buf_max)
48 perror("serial port not opened");
52 memset(buf, 0, buf_max);
55 serialport_read_until(fd, buf, SERIAL_EOL_CHAR, buf_max, SERIAL_TIMEOUT);
56 } while( buf[0] == '\n' );
58 // serialport_read_until(fd, buf, SERIAL_EOL_CHAR, buf_max, SERIAL_TIMEOUT);
63 int serial_writeline(int fd, char* buf)
65 if(buf[strlen(buf)-1] != SERIAL_EOL_CHAR){
67 int len = strlen(buf);
68 buf[len] = SERIAL_EOL_CHAR;
72 serialport_write(fd, buf);
78 void test_adruino_serial(void)
81 char buf[MAX_SERIAL_BUFFER];
83 fd = start_serial_inferface(DEFAULT_BAUDRATE, DEFAULT_SERIAL_PORT);
86 fprintf(stderr, "[E2 Agent]: ");
87 fgets(buf, MAX_SERIAL_BUFFER, stdin);
89 // serialport_write(fd, "hello\n");
90 //serialport_write(fd, buf);
91 serial_writeline(fd, buf);
93 serial_readline(fd, buf, MAX_SERIAL_BUFFER);
94 fprintf(stderr, "[Adruino] %s", buf);