From: misomosi Date: Tue, 4 Aug 2026 15:32:35 +0000 (-0400) Subject: Read track data from MSRX6 BT Serial X-Git-Url: http://git.misomosispi.com/?a=commitdiff_plain;p=gists.git Read track data from MSRX6 BT Serial --- diff --git a/MSRX6/mxread.odin b/MSRX6/mxread.odin new file mode 100644 index 0000000..9a6c2be --- /dev/null +++ b/MSRX6/mxread.odin @@ -0,0 +1,78 @@ +package mxread +import "core:bytes" +import "core:os" +import "core:time" +import "core:fmt" +import "core:sys/windows" + +COM_PORT :: "\\\\.\\COM10" +main :: proc() { + fp, errno := os.open(COM_PORT, os.O_RDWR) + if errno != 0 { + fmt.println("COM port open failure!", errno); + return; + } + fd := cast(windows.HANDLE)os.fd(fp) + dcb: windows.DCB + dcb.DCBlength = size_of(type_of(dcb)) + if !windows.GetCommState(fd, &dcb) { + fmt.println("GetCommState failure!", windows.GetLastError()); + return; + } + + dcb.BaudRate = 9600 + dcb.StopBits = .One + dcb.Parity = .None + if !windows.SetCommState(fd, &dcb) { + fmt.println("GetCommState failure!", windows.GetLastError()); + return; + } + + timeouts: windows.COMMTIMEOUTS + timeouts.ReadIntervalTimeout = 50 + timeouts.ReadTotalTimeoutConstant = 10000 + timeouts.ReadTotalTimeoutMultiplier = 10 + timeouts.WriteTotalTimeoutConstant = 2000 + timeouts.WriteTotalTimeoutMultiplier = 10 + if !windows.SetCommTimeouts(fd, &timeouts) { + fmt.println("SetCommTimeouts failure!", windows.GetLastError()); + return; + } + + /* + fmt.println("Telling MSR to reset!") + RESET := [2]u8 { 0x1b, 0x61 } + _, errno = os.write(fp, RESET[:]) + time.sleep(time.Second) + if errno != 0 { + fmt.println("Write failed!", errno) + } + fmt.println("Telling MSR to blink!") + LEDS := [2]u8 { 0x1b, 0x81 } + _, errno = os.write(fp, LEDS[:]) + time.sleep(time.Second) + if errno != 0 { + fmt.println("Write failed!", errno) + } + */ + fmt.println("Reading track data!") + READ := [2]u8 {0x1b, 0x72} + _, errno = os.write(fp, READ[:]) + if errno != 0 { + fmt.println("Write failed!", errno) + } else { + buffer: [1024]u8 + N, nread := 0, 0 + errno = 0 + for errno == 0 { + N += nread + if bytes.index_any(buffer[:N], {0x3f, 0x3f}) >= 0 { + break; + } + nread, errno = os.read(fp, buffer[N:]) + } + fmt.println(cast(string)buffer[4:N]) + } + fmt.println("Success!"); + os.close(fp) +}