Switch to desktop version  
Handle custom protocols (ASCII/Binary) with BASIC - Printable Version

+- Ewon Technical Forum (https://techforum.ewon.biz)
+-- Forum: Development (https://techforum.ewon.biz/forum-50.html)
+--- Forum: BASIC Script (https://techforum.ewon.biz/forum-52.html)
+--- Thread: Handle custom protocols (ASCII/Binary) with BASIC (/thread-205.html)



Handle custom protocols (ASCII/Binary) with BASIC - simon - 11-01-2017

Hi,

Here is an example of BASIC Script to handle a custom protocol (Master/Slave) over a serial connection :


Code:
CLS
REM open port 1 - 9600,N,8,1
OPEN "com:1,9600,8n1n" FOR BINARY OUTPUT AS 1

REM Compute request 0xF5 0xFA 0X01 0X01 0X00 0X00 0XFE 0X0F
A$ = ""
A$ = A$ + Chr$ 245
A$ = A$ + Chr$ 250
A$ = A$ + Chr$ 1
A$ = A$ + Chr$ 1
A$ = A$ + Chr$ 0
A$ = A$ + Chr$ 0
A$ = A$ + Chr$ 254
A$ = A$ + Chr$ 15

REM Send Request
PUT 1,A$

TSET 1,1
ONTIMER 1,"GOTO RECEIVE"

END
RECEIVE:
REM Receive code
B$ = GET 1
PRINT B$
IF (B$ <> "") THEN
    REM normal poll rate (1 sec) + process data
    TSET 1,1
    REM Write decimal value coded in Byte 1,2,3 of response in tag named "RESULT"
    RESULT@ = ASCII B$(1) * 65536 + ASCII B$(2) * 256 + ASCII B$(3)
Else
    REM TIMEOUT
    TSET 1,5
ENDIF
END



RE: Handle custom protocols (ASCII/Binary) with BASIC - Gerard van Gelderen - 23-10-2017

Thanks for the script,
It's working fine, but i have a question about it.
If i put a barcode scanner on it and i'am read the code "#KLB15161415" , how can i get this code in a Mitsubishi FX2N plc?


RE: Handle custom protocols (ASCII/Binary) with BASIC - simon - 24-10-2017

Gerard,

You get a string value from your barcode reader and I don't know how your PLC supports the string variables.

I imagine it stores each character of the string into successive byte/word registers that you can read by creating Tags with the Melsec Ioserver in your Flexy.  Then by script, you take each ascii value from the barcode and write them to these Tags, the Flexy will automatically update the value in the PLc.


RE: Handle custom protocols (ASCII/Binary) with BASIC - simon - 02-10-2025

Hi Luis,

Can you do a CLOSE 1 before opening the port ?

CLS
CLOSE 1
REM open port 1 - 9600,N,8,1
OPEN "com:3,9600,8n1n" FOR BINARY OUTPUT AS 1


RE: Handle custom protocols (ASCII/Binary) with BASIC - LuisEnrique - 10-10-2025

This thread has been marked as solved. If you have a similar issue, it would be better to post your own thread rather than bump this one, to help keep everybody's different issues separate.

(02-10-2025, 03:08 PM)simon Wrote: Hi Luis,

Can you do a CLOSE 1 before opening the port ?

CLS
CLOSE 1
REM open port 1 - 9600,N,8,1
OPEN "com:3,9600,8n1n" FOR BINARY OUTPUT AS 1

Hi Simon
Now I wrote the CLOSE but in the end, like this, and works fine

PUT 4,A$

Thanks.
PRINT A$

CLOSE 4


RE: Handle custom protocols (ASCII/Binary) with BASIC - simon - 16-10-2025

Excellent ;-)


RE: Handle custom protocols (ASCII/Binary) with BASIC - LuisEnrique - 21-10-2025

This thread has been marked as solved. If you have a similar issue, it would be better to post your own thread rather than bump this one, to help keep everybody's different issues separate.

(16-10-2025, 07:46 AM)simon Wrote: Excellent ;-)

Hi Simon

Actually Iam testing the recive part to read the serial port com1, using the next code:

Recive:
CLS
OPEN "COM:1,9600,8n1n" FOR BINARY OUTPUT AS 1

B$ = GET 1
PRINT B$
IF (B$ <> "") THEN
  TSET 2,2
  Result_String@ = ASCII B$(1) *65536 + ASCII B$(2) * 256 + ASCII B$(3)
  ELSE
  TSET 2,5
  ENDIF
 
CLOSE 1

END

But the Console send a error  "=" expected(3):Recive

Could you help me please.

Thanks in advance.

Luis


RE: Handle custom protocols (ASCII/Binary) with BASIC - simon - 24-10-2025

Hi Luis,

I just copied/pasted yoru script and I do not get the error.
Is it the whole script ?
If yes, can you clear you script IDE, save and paste the script again ?


RE: Handle custom protocols (ASCII/Binary) with BASIC - LuisEnrique - 24-10-2025

This thread has been marked as solved. If you have a similar issue, it would be better to post your own thread rather than bump this one, to help keep everybody's different issues separate.

(24-10-2025, 11:21 AM)simon Wrote: Hi Luis,

I just copied/pasted yoru script and I do not get the error.
Is it the whole script ?
If yes, can you clear you script IDE, save and paste the script again ?

Hi Simon

No, I have more code... the Recive, The following code has to do with the serial port:


Wifi_Setup:
SETSYS COM, "LOAD"
SETSYS COM, "WifiSSID", Name_WiFi@
SETSYS COM, "save"
CFGSAVE //if saved after reboot

SETSYS COM, "LOAD"
SETSYS COM, "WifiPSK", PWD_WiFi@
SETSYS COM, "save"
CFGSAVE //if saved after reboot

PRINT "Wifi Setup Completed"

END

SMS_Email_T1:
T$ = Time$
H$ = STR$SMS_Code@+"|M|"+T$(7 To 10)+T$(4 To 5)+T$(1 To 2)+"|"+T$(12 To 13)+T$(15 To 16)+T$(18 To 19)+"|"+RFC@ +"|"+NSM@+"|"+NSUT@+"|"+N_UTR@+"|"+STR$Consumo@+"|"+STR$Lat_Int@+"."+Lat_Dec@+"|"+STR$Long_Int@+"."+Long_Dec@+"|"+STR$Ker@
H$ = H$ +"|"+"UV"+UV_STR@
SMS_String@ = H$

IF CheckBox_Tel1@=1 THEN
    A$ = Telephone1@+";"+H$+CHR$(13)+CHR$(10)
ENDIF 

CLS
REM open port 1 - 9600,N,8,1
OPEN "com:3,9600,8n1n" FOR BINARY OUTPUT AS 4
REM Send Request
PUT 4,A$
PRINT A$

CLOSE 4

IF CheckBox_Email1@=1 THEN  //Send Email if Email 1 is selected
    SENDMAIL Email1@,"","", H$
ENDIF

END //END Report Status FTP Send SMS TELEPHONE AND EMAIL 1

SMS_Email_T2:
T$ = Time$
H$ = STR$SMS_Code@+"|M|"+T$(7 To 10)+T$(4 To 5)+T$(1 To 2)+"|"+T$(12 To 13)+T$(15 To 16)+T$(18 To 19)+"|"+RFC@ +"|"+NSM@+"|"+NSUT@+"|"+N_UTR@+"|"+STR$Consumo@+"|"+STR$Lat_Int@+"."+Lat_Dec@+"|"+STR$Long_Int@+"."+Long_Dec@+"|"+STR$Ker@
H$ = H$ +"|"+"UV"+UV_STR@
SMS_String@ = H$

IF CheckBox_Tel2@=1 THEN
    A$ = Telephone2@+";"+H$+CHR$(13)+CHR$(10)
ENDIF 
CLS
REM open port 1 - 9600,N,8,1
OPEN "com:3,9600,8n1n" FOR BINARY OUTPUT AS 4
REM Send Request
PUT 4,A$
PRINT A$

CLOSE 4
     
IF CheckBox_Email2@=1 THEN //Send Email if Email 2 is selected
  SENDMAIL Email2@,"","", H$
ENDIF

END //END Report Status FTP Send SMS TELEPHONE AND EMAIL 2

Report_StFTP_SMS_T3:
T$ = Time$
H$ = STR$SMS_Code@+"|M|"+T$(7 To 10)+T$(4 To 5)+T$(1 To 2)+"|"+T$(12 To 13)+T$(15 To 16)+T$(18 To 19)+"|"+RFC@ +"|"+NSM@+"|"+NSUT@+"|"+N_UTR@+"|"+STR$Consumo@+"|"+STR$Lat_Int@+"."+Lat_Dec@+"|"+STR$Long_Int@+"."+Long_Dec@+"|"+STR$Ker@
H$ = H$ +"|"+"UV"+UV_STR@
IF ResultFTP_Aux@=1 THEN
    A$ = Telephone3@+";"+"Envio de FTP OK"+CHR$(13)+CHR$(10)
    ELSE
    A$ = Telephone3@+";"+"Envio de FTP en Falla"+CHR$(13)+CHR$(10)     
ENDIF

CLS
REM open port 1 - 9600,N,8,1
OPEN "com:3,9600,8n1n" FOR BINARY OUTPUT AS 4
REM Send Request
PUT 4,A$
PRINT A$

CLOSE 4

  SENDMAIL Email3@,"","", H$ //COMMAND FOR SENDING EMAIL

END //END Report Status FTP Send SMS


Recive:
CLS
OPEN "COM:1,9600,8n1n" FOR BINARY OUTPUT AS 1

B$ = GET 1
PRINT B$
IF (B$ <> "") THEN
  TSET 2,2
  Result_String@ = ASCII B$(1) *65536 + ASCII B$(2) * 256 + ASCII B$(3)
  ELSE
  TSET 2,5
  ENDIF
 
CLOSE 1

END