Re: [BLACKBOX] PC Ports

From: [at]} <philipp>
Date: Tue, 28 Aug 2012 17:19:11 -0700

----boundary-LibPST-iamunique-1084695048_-_-
Content-type: text/plain

Below is an example to read/write to a (virtual) serial port
Hi Robert

Below is an example to read/write to a (virtual) serial port: 
(To try it just generate a new form with Controls > New Form.... Use the CPC beautifier to format it into a readable text. Hope this helps.)



MODULE UartSimple;

IMPORT S := SYSTEM, Dialog, Services, Strings, Print := StdLog, Uart := CommV24, WinApi;

TYPE
Action = POINTER TO RECORD (Services.Action) END;

VAR
commPort*: Dialog.List;
txString*: ARRAY 32 OF CHAR;
conn: Uart.Connection;
rxTask: Action;



(* UART Receive *)
PROCEDURE (rxTask: Action) Do;
VAR
rx: BYTE;
BEGIN
WHILE Uart.Available(conn) > 0 DO
Uart.ReceiveByte(conn, rx);
IF rx >= 21 THEN Print.Char(S.VAL(SHORTCHAR, rx)) END;
IF rx = 0AH THEN Print.Ln END
END;
Services.DoLater(rxTask, Services.Ticks() + 300)
END Do;


PROCEDURE SendTXString* ();
VAR i: INTEGER;
tx: ARRAY 64 OF BYTE;
BEGIN
FOR i := 0 TO LEN(txString$) DO
(* Uart.SendByte(conn, S.VAL(BYTE, txString[i])) *)
tx[i] := S.VAL(BYTE, txString[i]);
END;

Uart.SendBytes(conn, tx, 0, LEN(txString$));
END SendTXString;


PROCEDURE OpenUART*;
VAR
listItem: Dialog.String;
BEGIN
IF conn = NIL THEN
commPort.GetItem(commPort.index, listItem);
Uart.Open(listItem$, 2400, {}, conn);
IF conn # NIL THEN
Print.Open();
Print.String("Port " + listItem$ + " opened!"); Print.Ln();
IF rxTask = NIL THEN
NEW(rxTask);
Services.DoLater(rxTask, Services.now)
END
ELSE
Print.Open(); Print.String("Port " + listItem$ + " is not available."); Print.Ln()
END
ELSE
Print.Open(); Print.String("Comm port open. Please close first before opening again."); Print.Ln()
END
END OpenUART;

PROCEDURE CloseUART*;
BEGIN
IF conn # NIL THEN
Uart.Close(conn); conn := NIL;
Print.String("Port closed!"); Print.Ln()
ELSE
Print.String("No port opened."); Print.Ln()
END;
Services.RemoveAction(rxTask);
rxTask := NIL
END CloseUART;



PROCEDURE QueryCommPorts*;
CONST
SIZE = 256;
VAR
type, i, res: INTEGER;
valueNameLen, valueLen: INTEGER;
hKey: WinApi.HKEY;
name: WinApi.PtrSTR;
subKey, data: ARRAY[untagged] SIZE OF SHORTCHAR;

BEGIN
subKey := "HARDWARE\DEVICEMAP\SERIALCOMM";

res := WinApi.RegOpenKeyExA(WinApi.HKEY_LOCAL_MACHINE, subKey, 0, WinApi.KEY_READ, hKey);
IF res = WinApi.ERROR_SUCCESS THEN
Print.Ln(); Print.String("QUERY WINDOWS REGISTRY:"); Print.Ln();

name := S.VAL(WinApi.PtrSTR, WinApi.HeapAlloc(WinApi.GetProcessHeap(), WinApi.HEAP_ZERO_MEMORY, SIZE+1));

commPort.SetLen(0);
i := 0;
REPEAT
valueNameLen := SIZE; (* they just have to be big enough - if too small -> ERROR_MORE_DATA *)
valueLen := SIZE; (* ditto *)

res := WinApi.RegEnumValueA(hKey, i, name, valueNameLen, NIL, type, data[0], valueLen);

IF res = WinApi.ERROR_SUCCESS THEN
Print.String(name$);
IF (type = WinApi.REG_SZ) OR (* Check if it is type string *)
(type = WinApi.REG_EXPAND_SZ) OR
(type = WinApi.REG_MULTI_SZ) OR
(type = WinApi.REG_DWORD_BIG_ENDIAN) THEN
Print.String(" -> "); Print.String(data$); Print.Ln;
commPort.SetItem(i, "\\.\" + data$)
END
ELSIF res = WinApi.ERROR_NO_MORE_ITEMS THEN
Print.String("END: NO MORE ITEMS"); Print.Ln()
ELSIF res = WinApi.ERROR_MORE_DATA THEN
Print.String("END: ERROR MORE DATA - BUFFER TOO SMALL"); Print.Ln()
ELSE
Print.String("ERROR READING VALUE"); Print.Ln()
END;

INC(i)
UNTIL res # WinApi.ERROR_SUCCESS;
Dialog.UpdateList(commPort);

res := WinApi.HeapFree(WinApi.GetProcessHeap(), WinApi.HEAP_ZERO_MEMORY, S.VAL(WinApi.PtrVoid, name));
res := WinApi.RegCloseKey(hKey)
ELSE
Print.String("ERROR: ACCESS TO " + subKey + " DENIED"); Print.Ln()
END
END QueryCommPorts;



BEGIN
Print.Clear(); Print.Open();
Print.String("A simple UART Terminal program example"); Print.Ln();
Print.String("Author: Philipp Hof -9600.");
Print.Ln();
QueryCommPorts;
CLOSE
IF conn # NIL THEN Uart.Close(conn) END;
Services.RemoveAction(rxTask);
END UartSimple.



________________________________
From: Robert <robert.campbell_{([at]})nowhere.xy
To: BLACKBOX{([at]})nowhere.xy
Sent: Wednesday, 29 August 2012 8:03 AM
Subject: [BLACKBOX] PC Ports

This is more of a Windows question rather than BlackBox, I think,
but does anyone know how to write a simple (ASCII) string to the RS232
port and to the Bluetooth port?


I am using XP.

Cheers

Robert


----
To unsubscribe, send a message with body "SIGNOFF BLACKBOX" to LISTSERV{([at]})nowhere.xy----
To unsubscribe, send a message with body "SIGNOFF BLACKBOX" to LISTSERV{([at]})nowhere.xy----boundary-LibPST-iamunique-1084695048_-_-
Content-type: application/rtf
Content-transfer-encoding: base64
Content-Disposition: attachment; filename="rtf-body.rtf"
e1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcZnJvbXRleHQgXGZiaWRpcyBcZGVmZjB7XGZvbnR0YmwN
CntcZjBcZnN3aXNzXGZjaGFyc2V0MCBBcmlhbDt9DQp7XGYxXGZtb2Rlcm4gQ291cmllciBOZXc7
fQ0Ke1xmMlxmbmlsXGZjaGFyc2V0MiBTeW1ib2w7fQ0Ke1xmM1xmbW9kZXJuXGZjaGFyc2V0MCBD
b3VyaWVyIE5ldzt9fQ0Ke1xjb2xvcnRibFxyZWQwXGdyZWVuMFxibHVlMDtccmVkMFxncmVlbjBc
Ymx1ZTI1NTt9DQpcdWMxXHBhcmRccGxhaW5cZGVmdGFiMzYwIFxmMFxmczIwIEJlbG93IGlzIGFu
IGV4YW1wbGUgdG8gcmVhZC93cml0ZSB0byBhICh2aXJ0dWFsKSBzZXJpYW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ZiBhIFdpbmRvd3MgcXVlc3Rpb24gcmF0aGVyIHRoYW4g
QmxhY2tCb3gsIEkgdGhpbmssXHBhcg0KYnV0IGRvZXMgYW55b25lIGtub3cgaG93IHRvIHdyaXRl
IGEgc2ltcGxlIChBU0NJSSkgc3RyaW5nIHRvIHRoZSBSUzIzMlxwYXINCnBvcnQgYW5kIHRvIHRo
ZSBCbHVldG9vdGggcG9ydD9ccGFyDQpccGFyDQpJIGFtIHVzaW5nIFhQLlxwYXINClxwYXINCkNo
ZWVyc1xwYXINClxwYXINClJvYmVydFxwYXINClxwYXINClxwYXINCi0tLS1ccGFyDQpUbyB1bnN1
YnNjcmliZSwgc2VuZCBhIG1lc3NhZ2Ugd2l0aCBib2R5ICJTSUdOT0ZGIEJMQUNLQk9YIiB0byBM
SVNUU0VSVkBMSVNUUy5PQkVST04uQ0hccGFyDQpccGFyDQpccGFyDQotLS0tXHBhcg0KVG8gdW5z
dWJzY3JpYmUsIHNlbmQgYSBtZXNzYWdlIHdpdGggYm9keSAiU0lHTk9GRiBCTEFDS0JPWCIgdG8g
TElTVFNFUlZATElTVFMuT0JFUk9OLkNIXHBhcg0KfQ=
----boundary-LibPST-iamunique-1084695048_-_---
Received on Wed Aug 29 2012 - 02:19:11 UTC

This archive was generated by hypermail 2.3.0 : Thu Sep 26 2013 - 06:29:58 UTC