New to Applescript and need help with Serial Device passing Data

Hi all,

New here and new to scripting. I have a situation in which I need to capture a lap time transmitted through a serial connection from the device. i can do that with the sample script I found with the app coolterm. The issue I have is that the timer device is a holdover from time gone by. The developer tells me that the timer was/is programmed to use an Epson line printer. With that being said, the output is a not suitable for input into a spreadsheet as needed. The output begins with a carriage return, then a time in seconds and mseconds followed by a space and other characters. See below.
They do offer a windows software that will take the time and format as needed and then pass it on to a spreadsheet or webpage as needed. They do not offer this software or support for a mac. I am sure it would something super simple for the guru and experts here!

Code I am using to export the time in raw format to textedit:

tell application “CoolTerm”

set w to WindowID (0)
if w < 0 then
	display alert "No open windows"
	return
end if

# Open the serial port
if Connect w then
	delay 1
	
	repeat
		set i to 0
		repeat until i > 0
			# Poll the port and move any data from the serial receive
			Poll (w)
			# See if any data has arrived
			set i to BytesAvailable w
			delay 0.2
		end repeat
		
		# read the data from the buffer
		set d to ReadAll w
		
		# Generate keystrokes from the received Data
		
		# tell TextEdit to come to the front
		tell application "TextEdit"
			activate
		end tell
		
		# Send keystrokes to the application in the front
		tell application "System Events"
			keystroke d
		end tell
		
	end repeat
	
else
	display alert ("Not Connected")
end if

end tell


Sample output from this code:

… 2.873 (M)

I don’t understand what you want to do.

Text formatting?
Save data to CSV or other Excel-like applications?

CoolTerm seems to have an odd AppleScript dictionary.

Each technical issue may be seems easy, I feel your intention is difficult to understand.

Also, CoolTerm is too limited in functionality, so you may consider to work with other tools.

And it looks like it’s gone beyond the level of a forum question because it’s too complicated.

If I understand you correctly. You like to take the capture data to feed it to spreadsheet.
You could. convert the data to CSV. There input data is AppleScript list of strings.
Then you covert it with AppleScript’s delimiters

ex.

set captureData to {"0D", "0E", "0F", "20", "20", "32", "2E", "30", "34", "39", "20", "28", "4D", "29", "0D"}

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set csvData to text items of captureData as string
set AppleScript's text item delimiters to ASTID
return csvData

Other approach could be to:
If you know how to use Node-Red you could listen to serial port data and convert it to CSV and save it on local disk. For later be imported to spreadsheet application.

thanks for the info. essentially, the “writing” of the data would be an active text box or cell on a website. For this post, i chose textedit to output the received data. I am not too familiar with node-red but I will check it out. More or less, I need to 1 - capture the serial data coming in, 2-modify the data to only the time data, 3 write that to the intended program. In this instance, it would be the active Chrome window with an editable text box.

Why I suggest Node-Red. You could listen to serial input, filter any data you want to a csv.
Or you could feed the data to dashboard that contains table. When you have all this setup it will be a event-driven process and will update your dashboard when you feed new data.

Standard to access a dashboard in Node-Red is: http://localhost:1880:/ui/

General speking, AppleScript is weak at interrupt driven processing.

CoolTerm does not have such a interrupt trigger functionarity with AppleScript.

If you use CoolTerm, you have to watch the recieving result from serial port and compare the previous result. Endless loop with some seconds’ delay.

We don’t know the stability of CoolTerm. If the stability of CoolTerm is very well, you can detect the new data recieve by comparing with previous data. It will be a easy task.

And…re-formatting text and sending some web form is easy, too.

(1) Detect serial data receive by loop with delay (1 or some seconds you like).
(2) Re-format text as you like (filter some format time stamp data)
(3) Send text to some web form and make transmit action (we don’t know about the structure of the web page)

It is not so difficult. But we don’t know the frequency of data recieve. So, time delay should be changed by yourself. It is a kind of tuning.

I don’t know about the stability of CoolTerm. Looping and run command with CoolTerm might cause crash because of its memory management failure. If the time delay is 1 second, CoolTerm will execute AppleScript command 86,400 times per day.

I recommend to quit CoolTerm once a day to refresh its memory usage. It depends on CoolTerm’s stability.