libserialport
Aug 3, 2023 22:51:41 GMT 1
Post by alexfish on Aug 3, 2023 22:51:41 GMT 1
Hi All
Lib Serial Port
Requires Latest Fossil "Beta" from HERE
I have spent some time looking at serial ports & examples
& as Alex said , in the short found something that should be on your system
and so to the short: and yes it be a bit bitty , and in short hot of the press
Here tested on Arduino 2560 with GRBL mega, again in the short this is a bit a naughty one
+ needed to get around the &foo that BaCon reports as wrong type in the few libs I have tested
Other + can use Straight Strings
Requires libserialport devs
More To Follow
BR
Alex
Results sitting Here
Lib Serial Port
Requires Latest Fossil "Beta" from HERE
I have spent some time looking at serial ports & examples
& as Alex said , in the short found something that should be on your system
and so to the short: and yes it be a bit bitty , and in short hot of the press
Here tested on Arduino 2560 with GRBL mega, again in the short this is a bit a naughty one
+ needed to get around the &foo that BaCon reports as wrong type in the few libs I have tested
Other + can use Straight Strings
PRAGMA INCLUDE <libserialport.h>
PRAGMA OPTIONS `pkg-config --cflags libserialport`
PRAGMA LDFLAGS `pkg-config --libs libserialport`
OPTION PARSE FALSE
/* This example can be used with one or two ports. With one port, it
* will send data and try to receive it on the same port. This can be
* done by connecting a single wire between the TX and RX pins of the
* port.
*
* Alternatively it can be used with two serial ports connected to each
* other, so that data can be sent on one and received on the other.
* This can be done with two ports with TX/RX cross-connected, e.g. by
* a "null modem" cable, or with a pair of interconnected virtual ports,
* such as those created by com0com on Windows or tty0tty on Linux. */
/* Get the port names from the command line. */
REM in this case edited the Example read_write from Examples in the lib
FUNCTION check(enum sp_return result)
LOCAL error_message TYPE char*
SELECT result
CASE SP_ERR_ARG
PRINT "Error: Invalid argument."
END
CASE SP_ERR_FAIL
error_message = sp_last_error_message()
printf("Error: Failed: %s\n", error_message)
sp_free_error_message(error_message)
CASE SP_ERR_SUPP
PRINT "Error: Not supported."
END
CASE SP_ERR_MEM
printf("Error: Couldn't allocate memory.\n")
END
CASE SP_OK:
DEFAULT
RETURN result
END SELECT
END FUNCTION
REM ================================================
SUB do_ports(char* my_portname$)
/* The ports we will use. */
struct sp_port *ports;
/* Open and configure port. */
PRINT "Looking for port " & my_portname$
check(sp_get_port_by_name(my_portname$, &ports))
PRINT "Opening port." & my_portname$
check(sp_open(ports, SP_MODE_READ_WRITE))
PRINT "Setting port to 115200 8N1, no flow control."
check(sp_set_baudrate(ports, 115200))
check(sp_set_bits(ports, 8))
check(sp_set_parity(ports, SP_PARITY_NONE))
check(sp_set_stopbits(ports, 1))
check(sp_set_flowcontrol(ports, SP_FLOWCONTROL_NONE))
/* Now send some data on each port and receive it back. */
/* Get the ports to send and receive on. */
LOCAL *tx_port = ports TYPE struct sp_port
LOCAL *rx_port = ports TYPE struct sp_port
/* The data we will send. */
LOCAL data = "\n" TYPE char*
LOCAL size = strlen(data)
/* We'll allow a 1 second timeout for send and receive. */
LOCAL timeout = 1000 TYPE unsigned int
/* TODO On success, sp_blocking_write() and sp_blocking_read()
* return the number of bytes sent/received before the
* timeout expired. We'll store that result here. */
LOCAL result TYPE int
/* Send data. */
PRINT "Sending on port : " & my_portname$
result = check(sp_blocking_write(tx_port, data, size, timeout))
/* Check whether we sent all of the data. */
IF (result == size) THEN
printf("Sent %d bytes successfully.\n", size)
ELSE
printf("Timed out, %d/%d bytes sent.\n", result, size);
END IF
LOCAL readsize = 1024 TYPE int
/* Allocate a buffer to receive data. */
LOCAL buf TYPE char*
buf = malloc(readsize + 1)
/* Try to receive the data on the other port. */
result = check(sp_blocking_read(rx_port, buf, readsize, timeout))
/* Check whether we received the number of bytes we wanted. */
IF (result == size) THEN
printf("Received %d bytes successfully.\n", size)
ELSE
printf("Timed out, %d/%d bytes received.\n", result, size)
END IF
/* Check if we received the same data we sent. */
buf[result] = '\0';
printf("Received :%s\n", buf)
/* Free receive buffer. */
free(buf)
/* Close ports and free resources. */
' TODO needs to be kept:: but free at end of session
check(sp_close(ports))
sp_free_port(ports)
END SUB
do_ports("/dev/ttyACM0")
PRINT "DONE"
Requires libserialport devs
More To Follow
BR
Alex
Results sitting Here
Looking for port /dev/ttyACM0
Opening port./dev/ttyACM0
Setting port to 115200 8N1, no flow control.
Sending on port : /dev/ttyACM0
Sent 1 bytes successfully.
Timed out, 28/1 bytes received.
Received :
Grbl 0.8c ['$' for help]