From 051cb22a2a5be8ba2976aa2d36409afab107b954 Mon Sep 17 00:00:00 2001 From: Michael Lotz Date: Sun, 12 Jun 2011 20:40:33 +0000 Subject: [PATCH] Add serial_io, a simplistic BSerialPort based command line tool that prints what it reads from the specified port and writes what gets input on stdin. It currently hardcodes the basic setup of the serial port. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@42131 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/tests/kits/device/Jamfile | 1 + src/tests/kits/device/bserial/Jamfile | 5 ++ src/tests/kits/device/bserial/SerialIO.cpp | 83 ++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 src/tests/kits/device/bserial/Jamfile create mode 100644 src/tests/kits/device/bserial/SerialIO.cpp diff --git a/src/tests/kits/device/Jamfile b/src/tests/kits/device/Jamfile index 754ca60542..036d9fbeda 100644 --- a/src/tests/kits/device/Jamfile +++ b/src/tests/kits/device/Jamfile @@ -1,3 +1,4 @@ SubDir HAIKU_TOP src tests kits device ; +SubInclude HAIKU_TOP src tests kits device bserial ; SubInclude HAIKU_TOP src tests kits device stickit_BJoystick ; diff --git a/src/tests/kits/device/bserial/Jamfile b/src/tests/kits/device/bserial/Jamfile new file mode 100644 index 0000000000..5db5a60eaa --- /dev/null +++ b/src/tests/kits/device/bserial/Jamfile @@ -0,0 +1,5 @@ +SubDir HAIKU_TOP src tests kits device bserial ; + +SimpleTest serial_io : + SerialIO.cpp + : libdevice.so $(TARGET_LIBSTDC++) ; diff --git a/src/tests/kits/device/bserial/SerialIO.cpp b/src/tests/kits/device/bserial/SerialIO.cpp new file mode 100644 index 0000000000..95ff024cf6 --- /dev/null +++ b/src/tests/kits/device/bserial/SerialIO.cpp @@ -0,0 +1,83 @@ +#include + +#include +#include + + +static int32 +reader_thread(void *data) +{ + BSerialPort *port = (BSerialPort *)data; + char buffer[128]; + + while (true) { + ssize_t read = port->Read(buffer, sizeof(buffer)); + if (read <= 0) + continue; + + for (ssize_t i = 0; i < read; i++) { + putc(buffer[i], stdout); + fflush(stdout); + } + } + + return 0; +} + + +int +main(int argc, char *argv[]) +{ + BSerialPort port; + + if (argc < 2) { + printf("usage: %s \n", argv[0]); + + int32 portCount = port.CountDevices(); + printf("\tports (%ld):\n", portCount); + + char nameBuffer[B_OS_NAME_LENGTH]; + for (int32 i = 0; i < portCount; i++) { + if (port.GetDeviceName(i, nameBuffer, sizeof(nameBuffer)) != B_OK) { + printf("\t\tfailed to retrieve name %ld\n", i); + continue; + } + + printf("\t\t%s\n", nameBuffer); + } + + return 1; + } + + status_t result = port.Open(argv[1]); + if (result < B_OK) { + printf("failed to open port \"%s\": %s\n", argv[1], strerror(result)); + return result; + } + + port.SetDataRate(B_9600_BPS); + port.SetDataBits(B_DATA_BITS_8); + port.SetStopBits(B_STOP_BITS_1); + port.SetParityMode(B_NO_PARITY); + port.SetFlowControl(B_NOFLOW_CONTROL); + + thread_id reader = spawn_thread(reader_thread, "serial reader", + B_NORMAL_PRIORITY, &port); + if (reader < 0) { + printf("failed to spawn reader thread\n"); + return reader; + } + + resume_thread(reader); + + char buffer[128]; + while (true) { + char *string = fgets(buffer, sizeof(buffer), stdin); + if (string == NULL) + continue; + + port.Write(buffer, strlen(string) - 1); + } + + return 0; +}