Serial (232) API, Minimal example

Share your advanced knowledge/code with the community.
plouf
Posts: 295
Joined: Tue Feb 25, 2014 6:01 pm
Location: Athens,Greece

Serial (232) API, Minimal example

Post by plouf »

I give a try to Serial API in chrome based browsers
a few thing noticed, 1) its doable, 2) similar command set to PB is possible, 3) better way is to be binded in PB bindevents (dunno how thought)

Because this is supported only in Chrome based browsers , not even in android chrome (cordova plugin required for this)
First thing you MUST do is check is SerialAPi is support
Then you MUST ask user to SELECT the serial
Then Open port
Then send or receive data
final you may close it to release port, NOTE that if you close it you must go again to ask user first, then open port

save this as SerialAPI.sbi

Code: Select all

Global SerialInputDataStream.s= ""
Procedure IsSerialPort()
  ret =0;
  ! 
  ! if (typeof navigator.serial!="undefined") {
  ! v_ret=1
  !};
  ProcedureReturn ret 
EndProcedure

Procedure AskPermitionSerialPort()
  !async function permitionserialport() {
  !	  window.sbserialport = await navigator.serial.requestPort();
  !	};
  ! permitionserialport();
EndProcedure

Procedure OpenSerialPort(baudrate.l)
  !	var options = {
	!	baudRate: v_baudrate
	!};
	! window.sbserialport.open(options);
EndProcedure

Procedure CloseSerialPort()
  !window.sbserialport.forget();
EndProcedure

Procedure WriteSerialPortString(datatosend.s)
  ! var a=1;
  ! pl_encoder = new TextEncoder();
	! pl_writer = window.sbserialport.writable.getWriter();
	! pl_writer.write(pl_encoder.encode(v_datatosend));
	! pl_writer.releaseLock();
EndProcedure

Procedure StartReadSerialPort()
  SerialInputDataStream=""
  !async function pl_readsserialportstringline() {
  !const pl_reader = window.sbserialport.readable.getReader();
	!while (true) {
	!	const { value, done } = await pl_reader.read();
	!	if (done) {
	!		pl_reader.releaseLock();
	!		break;
	!	}
	!	const pl_decoder = new TextDecoder();
  ! g_serialinputdatastream = pl_decoder.decode(value);
	! }
	!};
	! pl_readsserialportstringline();
EndProcedure
use this example to play around

Code: Select all

IncludeFile "SerialAPI.sbi"

Procedure GadgetEvents()
    Select EventGadget()
      Case 1 
        Debug IsSerialPort()
      Case 2
        AskPermitionSerialPort()
      Case 3
        OpenSerialPort(9600)
      Case 4
        WriteSerialPortString(GetGadgetText(14))
      Case 5 
        StartReadSerialport()
      Case 6
        CloseserialPort()
      Case 7
        SerialInputDataStream=""
    EndSelect
EndProcedure
Procedure TimerEvents()
  SetGadgetText(15,SerialInputDataStream)
EndProcedure

OpenWindow(1,1,1,500,600,"Simple Serial Example")

ButtonGadget(1,1,1,200,20,"1 - Check Serial API?")
ButtonGadget(2,1,30,200,20,"2 - Select Serial (Grand permition)")
ButtonGadget(3,1,60,200,20,"3 - Connect to Selected Serial (9600)") 
ButtonGadget(4,1,90,200,20,"4a- Send String ->")
  StringGadget(14,220,90,200,20,"Hello World !")
ButtonGadget(5,1,120,200,20,"4b-Receive String ->")
  TextGadget(15,220,120,200,20,"",#PB_Text_Border )
  
ButtonGadget(6,1,150,200,20,"5 - close Serial Port") 
ButtonGadget(7,220,150,200,20," ^^^ CLEAR INPUT STRING ^^^")

BindEvent(#PB_Event_Gadget, @GadgetEvents())
 BindEvent(#PB_Event_Timer, @TimerEvents())

AddTimer(1,500)
Christos
User avatar
Caronte3D
Posts: 189
Joined: Sat Nov 23, 2019 5:21 pm
Location: Some Universe

Re: Serial (232) API, Minimal example

Post by Caronte3D »

Do you know if it works with bluetooth?
plouf
Posts: 295
Joined: Tue Feb 25, 2014 6:01 pm
Location: Athens,Greece

Re: Serial (232) API, Minimal example

Post by plouf »

bluettooth is a complete diffirent think than serial ..
i have not mess with it :)
Christos
tj1010
Posts: 218
Joined: Wed May 27, 2015 1:36 pm
Contact:

Re: Serial (232) API, Minimal example

Post by tj1010 »

weird coincidence is there are 232 over BT converters..

I haven't used 232 since QBASIC and VB 1.0 had vanilla API support and I used it for AX.25 modem control like three decades ago.. I still see it in industrial PLC. It's still in a lot of SBC stuff in 2024 so this is pretty useful for low complexity communication and control without a TCP/IP stack..

Side Note: If you want to dig in to BT 5.0 just ignore the PHY RF stuff; you can't really use it anyway outside of PAN design. Instead focus on the profile you need. A2DP, ATT, AVRCP, BIP, BPP, CIP, CTT, DIP, DUN, FAX, FTP, GAVDP, GAP, GATT, GOEP, HCRP, HDP, HFP, HID, HOGP, HSP, iAP, ICP, LAP, MESH, MAP, OBEX, OP, PAN, PBAP, PXP, SPP, SDAP, SAP'. FTP for file transfer and A2DP for audio streaming; there is a device control profile too; AVRCP. The standard actually has it's own codecs and compression algos used transparently, and is pretty secure outside of a infrequent implementation bug that is exploitable. Cordova libs likely don't cover but a few profiles..
Post Reply