update folders
This commit is contained in:
6
snipplets/code/C/libserialport/.editorconfig
Normal file
6
snipplets/code/C/libserialport/.editorconfig
Normal file
@ -0,0 +1,6 @@
|
||||
[{clear,build,format-code}]
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
|
||||
[{*.c,*.ino}]
|
||||
indent_size = 2
|
12
snipplets/code/C/libserialport/.gitignore
vendored
Normal file
12
snipplets/code/C/libserialport/.gitignore
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
# libserialport examples
|
||||
port_info.c
|
||||
list_ports.c
|
||||
send_receive.c
|
||||
|
||||
# binaries
|
||||
port_info
|
||||
list_ports
|
||||
send_receive
|
||||
|
||||
listen
|
||||
abc.txt
|
1
snipplets/code/C/libserialport/Board/.gitignore
vendored
Normal file
1
snipplets/code/C/libserialport/Board/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
3party/
|
24
snipplets/code/C/libserialport/Board/Board.ino
Normal file
24
snipplets/code/C/libserialport/Board/Board.ino
Normal file
@ -0,0 +1,24 @@
|
||||
/*
|
||||
Author: Alexander Popov
|
||||
License: Unlicense
|
||||
*/
|
||||
|
||||
#include "3party/AsyncStream.h"
|
||||
AsyncStream<50> serial(&Serial, '\n');
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (strcmp(serial.buf, "ping") == 0) {
|
||||
Serial.println("PONG");
|
||||
}
|
||||
|
||||
Serial.println("ooooo");
|
||||
// delay(1000);
|
||||
Serial.println("zzzz");
|
||||
// delay(1000);
|
||||
Serial.println("xxx");
|
||||
// delay(1000);
|
||||
}
|
3
snipplets/code/C/libserialport/Board/README.md
Normal file
3
snipplets/code/C/libserialport/Board/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
Download `AsyncStream.h` from
|
||||
https://github.com/GyverLibs/AsyncStream
|
||||
and drop in to `3party` folder.
|
18
snipplets/code/C/libserialport/README.md
Normal file
18
snipplets/code/C/libserialport/README.md
Normal file
@ -0,0 +1,18 @@
|
||||
Download `libserialport` library.
|
||||
|
||||
```sh
|
||||
git clone git://sigrok.org/libserialport
|
||||
```
|
||||
|
||||
For **build** code use `build` script or build files manually.
|
||||
|
||||
Example:
|
||||
|
||||
```sh
|
||||
export C_INCLUDE_PATH=<path/to/libserialport>
|
||||
export LIBRARY_PATH=<path/to/libserialport/.libs>
|
||||
|
||||
gcc -static -Wall -O3 -o <output_filename> <file.c> -lserialport
|
||||
```
|
||||
|
||||
Arduino example project store in `Board` folder.
|
20
snipplets/code/C/libserialport/build_gcc
Executable file
20
snipplets/code/C/libserialport/build_gcc
Executable file
@ -0,0 +1,20 @@
|
||||
#!/bin/sh
|
||||
#
|
||||
# build - script for build all files.
|
||||
#
|
||||
# Alexander Popov <iiiypuk@fastmail.fm>
|
||||
|
||||
export C_INCLUDE_PATH=$HOME/Git/libserialport
|
||||
export LIBRARY_PATH=$HOME/Git/libserialport/.libs
|
||||
|
||||
for file in \
|
||||
port_info.c \
|
||||
list_ports.c \
|
||||
send_receive.c \
|
||||
listen.c \
|
||||
|
||||
do
|
||||
echo -ne "[ ] Building $file...\r"
|
||||
gcc -static -Wall -O3 -o ${file%.*} $file -lserialport
|
||||
echo [OK
|
||||
done
|
20
snipplets/code/C/libserialport/build_tcc
Executable file
20
snipplets/code/C/libserialport/build_tcc
Executable file
@ -0,0 +1,20 @@
|
||||
#!/bin/sh
|
||||
#
|
||||
# build - script for build all files.
|
||||
#
|
||||
# Alexander Popov <iiiypuk@fastmail.fm>
|
||||
|
||||
export C_INCLUDE_PATH=$HOME/Git/libserialport
|
||||
export LIBRARY_PATH=$HOME/Git/libserialport/.libs
|
||||
|
||||
for file in \
|
||||
port_info.c \
|
||||
list_ports.c \
|
||||
send_receive.c \
|
||||
listen.c \
|
||||
|
||||
do
|
||||
echo -ne "[ ] Building $file...\r"
|
||||
tcc -Wall -O3 -o ${file%.*} -I$C_INCLUDE_PATH $file $LIBRARY_PATH/libserialport.a
|
||||
echo [OK
|
||||
done
|
17
snipplets/code/C/libserialport/clear
Executable file
17
snipplets/code/C/libserialport/clear
Executable file
@ -0,0 +1,17 @@
|
||||
#!/bin/sh
|
||||
#
|
||||
# clear - script for delete all builded files.
|
||||
#
|
||||
# Alexander Popov <iiiypuk@fastmail.fm>
|
||||
|
||||
files=(
|
||||
"port_info"
|
||||
"list_ports"
|
||||
"send_receive"
|
||||
"listen"
|
||||
)
|
||||
|
||||
for file in ${files[@]}
|
||||
do
|
||||
rm $file &> /dev/null
|
||||
done
|
14
snipplets/code/C/libserialport/format-code
Executable file
14
snipplets/code/C/libserialport/format-code
Executable file
@ -0,0 +1,14 @@
|
||||
#!/bin/sh
|
||||
#
|
||||
# formt-code - script for beautify code by clang-format.
|
||||
#
|
||||
# Alexander Popov <iiiypuk@fastmail.fm>
|
||||
|
||||
files=(
|
||||
"listen.c"
|
||||
)
|
||||
|
||||
for file in "${files[@]}"
|
||||
do
|
||||
clang-format -i --style=LLVM --sort-includes=false $file
|
||||
done
|
133
snipplets/code/C/libserialport/listen.c
Normal file
133
snipplets/code/C/libserialport/listen.c
Normal file
@ -0,0 +1,133 @@
|
||||
#include <libserialport.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <signal.h>
|
||||
|
||||
/* Helper function for error handling. */
|
||||
int check(enum sp_return result);
|
||||
void handle_sigint(int sig);
|
||||
|
||||
bool INTERRUPT = false;
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
struct sp_port *serial_port;
|
||||
char *port_name;
|
||||
|
||||
const int size = 256;
|
||||
char *buffer = malloc(size + 1);
|
||||
|
||||
const unsigned int timeout = 1000;
|
||||
int result;
|
||||
|
||||
/* Get the port name from the command line. */
|
||||
if (argc != 2) {
|
||||
printf("Usage: %s <port>\n\n", argv[0]);
|
||||
|
||||
struct sp_port **port_list;
|
||||
enum sp_return result = sp_list_ports(&port_list);
|
||||
|
||||
/* Getting the available ports. */
|
||||
if (result != SP_OK) {
|
||||
puts("Getting available ports failed!");
|
||||
} else {
|
||||
puts("Available ports:");
|
||||
|
||||
int i;
|
||||
for (i = 0; port_list[i] != NULL; i++) {
|
||||
/* Get the name of the port. */
|
||||
struct sp_port *port = port_list[i];
|
||||
char *port_name = sp_get_port_name(port);
|
||||
|
||||
printf(" * %s\n", port_name);
|
||||
}
|
||||
|
||||
printf("\nAvailable %d ports.\n", i);
|
||||
|
||||
sp_free_port_list(port_list);
|
||||
}
|
||||
|
||||
return -1;
|
||||
} else {
|
||||
port_name = argv[1];
|
||||
}
|
||||
|
||||
printf("Connecting to '%s'...\n", port_name);
|
||||
check(sp_get_port_by_name(port_name, &serial_port));
|
||||
check(sp_open(serial_port, SP_MODE_READ_WRITE));
|
||||
|
||||
check(sp_set_baudrate(serial_port, 9600));
|
||||
check(sp_set_bits(serial_port, 8));
|
||||
check(sp_set_parity(serial_port, SP_PARITY_NONE));
|
||||
check(sp_set_stopbits(serial_port, 1));
|
||||
check(sp_set_flowcontrol(serial_port, SP_FLOWCONTROL_NONE));
|
||||
puts("Connected.");
|
||||
|
||||
signal(SIGINT, handle_sigint);
|
||||
|
||||
FILE *output_file;
|
||||
output_file = fopen("./abc.txt", "w");
|
||||
|
||||
/* Reading lines from serial port. */
|
||||
bool reading = true;
|
||||
while (reading && !INTERRUPT) {
|
||||
int pos = 0;
|
||||
|
||||
/* Character-by-character reading. */
|
||||
while (pos < size) {
|
||||
result = check(sp_blocking_read(serial_port, buffer + pos, 1, timeout));
|
||||
|
||||
if (result == -1) {
|
||||
puts("Error reading from serial port");
|
||||
|
||||
reading = false;
|
||||
break;
|
||||
} else if (result == 0) {
|
||||
puts("No more data");
|
||||
break;
|
||||
} else {
|
||||
if (buffer[pos] == '\n') {
|
||||
buffer[pos] = '\0';
|
||||
break;
|
||||
}
|
||||
|
||||
pos++;
|
||||
}
|
||||
}
|
||||
|
||||
puts(buffer);
|
||||
fputs(buffer, output_file);
|
||||
}
|
||||
|
||||
fclose(output_file);
|
||||
free(buffer);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Helper function for error handling. */
|
||||
int check(enum sp_return result) {
|
||||
char *error_message;
|
||||
|
||||
switch (result) {
|
||||
case SP_ERR_ARG:
|
||||
puts("Error: Invalid argument.");
|
||||
abort();
|
||||
case SP_ERR_FAIL:
|
||||
error_message = sp_last_error_message();
|
||||
printf("Error: Failed: %s\n", error_message);
|
||||
sp_free_error_message(error_message);
|
||||
abort();
|
||||
case SP_ERR_SUPP:
|
||||
puts("Error: Not supported.");
|
||||
abort();
|
||||
case SP_ERR_MEM:
|
||||
puts("Error: Couldn't allocate memory.");
|
||||
abort();
|
||||
case SP_OK:
|
||||
default:
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
void handle_sigint(int sig) { INTERRUPT = true; }
|
Reference in New Issue
Block a user