ASCII Serial Com
Serial communication library between computers, microcontrollers, FPGAs, etc. Uses only ASCII. Not the most efficient protocol, but meant to be easy to read
Loading...
Searching...
No Matches
ascii_serial_com_dummy_register_device.c
1#include <errno.h>
2#include <poll.h>
3#include <stdio.h>
4#include <string.h>
5#include <unistd.h>
6
7#include "asc_exception.h"
8#include "ascii_serial_com.h"
12
13char dataBuffer[MAXDATALEN];
14#define nRegs 10
15REGTYPE regs[nRegs];
16
22 .state_rw = &reg_block};
23circular_buffer_uint8 *asc_in_buf;
24circular_buffer_uint8 *asc_out_buf;
25
26CEXCEPTION_T e;
27
28int timeout = -1;
29
30int main(int argc, char *argv[]) {
31
32 if (argc > 1) {
33 if (strncmp("-h", argv[1], 2) == 0) {
34 fprintf(stderr,
35 "\n ascii_serial_com_dummy_register_device [-h] <infile> "
36 "<outfile>\n\n");
37 fprintf(stderr,
38 " If no filenames are provided, then stdin and stdout are used\n"
39 " -h: show help and exit\n"
40 "\n");
41 return 0;
42 }
43 }
44 if (argc != 3 && argc != 1) {
45 fprintf(stderr, "Error: either 0 or 2 arguments required:\n");
46 fprintf(stderr, "\n ascii_serial_com_dummy_register_device [-h] <infile> "
47 "<outfile>\n\n");
48 fprintf(stderr,
49 " If no filenames are provided, then stdin and stdout are used\n"
50 " -h: show help and exit\n"
51 "\n");
52 return 1;
53 }
54
55 FILE *infile;
56 FILE *outfile;
57 int infileno;
58 int outfileno;
59 if (argc == 3) {
60 const char *infilename = argv[1];
61 const char *outfilename = argv[2];
62 fprintf(stdout, "infile: %s\noutfile: %s\n", infilename, outfilename);
63
64 infile = fopen(infilename, "r");
65 if (!infile) {
66 perror("Error opening input file");
67 fprintf(stderr, "Exiting.\n");
68 return 1;
69 }
70 infileno = fileno(infile);
71 if (infileno < 0) {
72 perror("Error getting infile descriptor");
73 fprintf(stderr, "Exiting.\n");
74 return 1;
75 }
76 outfile = fopen(outfilename, "a+");
77 if (!outfile) {
78 perror("Error opening output file");
79 fprintf(stderr, "Exiting.\n");
80 return 1;
81 }
82 outfileno = fileno(outfile);
83 if (outfileno < 0) {
84 perror("Error getting infile descriptor");
85 fprintf(stderr, "Exiting.\n");
86 return 1;
87 }
88
89 } else { // no args
90 fprintf(stderr, "infile: stdin\noutfile: stdout\n");
91 infile = stdin;
92 outfile = stdout;
93 infileno = STDIN_FILENO;
94 outfileno = STDOUT_FILENO;
95 }
96
97 Try {
98 ascii_serial_com_register_block_init(&reg_block, regs, nRegs);
99 ascii_serial_com_device_init(&ascd, &ascd_config);
102
103 circular_buffer_io_fd_poll_init(&cb_io, asc_in_buf, asc_out_buf, infileno,
104 outfileno);
105 }
106 Catch(e) {
107 fprintf(stderr, "Uncaught exception: %u, during init, exiting.\n", e);
108 return 1;
109 }
110
111 while (true) {
112 Try {
113 int poll_ret_code = circular_buffer_io_fd_poll_do_poll(&cb_io, timeout);
114 if (poll_ret_code != 0) {
115 return 1;
116 }
118
120
122
123 // Do we need to process data in the input buffer?
124 // If so, poll with short timeout, otherwise just poll
125 // (all else is just waiting on IO)
126 if (!circular_buffer_is_empty_uint8(asc_in_buf)) {
127 timeout = 5; // ms
128 } else {
129 timeout = -1; // unlimited
130 }
131 }
132 Catch(e) {
133 fprintf(stderr, "Uncaught exception: %u, exiting.\n", e);
134 return 1;
135 }
136 }
137
138 return 0;
139}
circular_buffer_uint8 * ascii_serial_com_device_get_output_buffer(ascii_serial_com_device *ascd)
ASCII Serial Com Device get output buffer.
void ascii_serial_com_device_receive(ascii_serial_com_device *ascd)
ASCII Serial Com Device receive messages.
circular_buffer_uint8 * ascii_serial_com_device_get_input_buffer(ascii_serial_com_device *ascd)
ASCII Serial Com Device get input buffer.
void ascii_serial_com_device_init(ascii_serial_com_device *ascd, ascii_serial_com_device_config *config)
ASCII Serial Com Device init.
ASCII Serial Com Device.
void ascii_serial_com_register_block_handle_message(ascii_serial_com *asc, char ascVersion, char appVersion, char command, char *data, size_t dataLen, void *register_block_state_vp)
ASCII Serial Com Register Block handle message.
void ascii_serial_com_register_block_init(ascii_serial_com_register_block *register_block_state, REGTYPE *block, uint16_t n_regs)
ASCII Serial Com Register Block init.
ASCII Serial Com Register Block.
bool circular_buffer_is_empty_uint8(const circular_buffer_uint8 *circ_buf)
circular buffer get if empty
uint8_t circular_buffer_io_fd_poll_do_poll(circular_buffer_io_fd_poll *cb_io, int timeout)
poll circular buffer IO with file descriptor polling object
void circular_buffer_io_fd_poll_init(circular_buffer_io_fd_poll *cb_io, circular_buffer_uint8 *in_buf, circular_buffer_uint8 *out_buf, int fd_in, int fd_out)
Initialize circular buffer IO with file descriptor polling object.
size_t circular_buffer_io_fd_poll_do_input(circular_buffer_io_fd_poll *cb_io)
circular buffer IO with file descriptor polling object: read from input fd
size_t circular_buffer_io_fd_poll_do_output(circular_buffer_io_fd_poll *cb_io)
circular buffer IO with file descriptor polling object: write to output fd
Circular buffer IO with file descriptor polling.
ASCII Serial Com Device Config Struct.
void(* func_rw)(ascii_serial_com *, char, char, char, char *data, size_t, void *)
ASCII Serial Com Device State struct.
ASCII Serial Com Register Block State struct.
Circular buffer IO with file descriptor polling struct.
circular buffer struct