Switch to desktop version  

Ewon TechForum
Providing technical solutions to technical requests



Handle custom protocols (ASCII/Binary) with BASIC
simon
eWON Support
#1
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
Reply

Gerard van Gelderen
Junior Member
#2
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?
Reply

simon
eWON Support
#3
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.
Reply

simon
eWON Support
#4
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
Reply

LuisEnrique
Junior Member
#5
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
Reply

simon
eWON Support
#6
Excellent ;-)
Reply

LuisEnrique
Junior Member
#7
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
Reply

simon
eWON Support
#8
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 ?
Reply

LuisEnrique
Junior Member
#9
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
Reply




Possibly Related Threads…
Yesterday, 04:04 PM
Last Post: LuisEnrique
26-03-2026, 10:41 PM
Last Post: LuisEnrique
16-10-2025, 08:20 AM
Last Post: simon
15-09-2025, 05:20 PM
Last Post: AngelaT
  Setup Wifi using basic? Started by LuisEnrique
2 Replies - 1.648 Views
18-07-2025, 07:22 AM
Last Post: LuisEnrique
05-06-2025, 12:23 PM
Last Post: simon
15-05-2025, 05:57 PM
Last Post: LuisEnrique
20-03-2025, 12:55 PM
Last Post: driesdebouver
24-10-2024, 01:52 PM
Last Post: anas.rhouati
19-03-2024, 01:55 PM
Last Post: simon



Users browsing this thread:
1 Guest(s)



Theme © Ewon 2019 - Forum software by © MyBB - Cookie policy