﻿<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://www.plcopen.org/xml/tc6_0200">
  <fileHeader companyName="Rexroth" productName="ctrlX PLC Engineering" productVersion="ctrlX PLC 1.20.1" creationDateTime="2023-07-05T15:00:54.1348839" />
  <contentHeader name="Barcode Scanner.project" modificationDateTime="2023-07-05T14:30:56.8568964">
    <coordinateInfo>
      <fbd>
        <scaling x="1" y="1" />
      </fbd>
      <ld>
        <scaling x="1" y="1" />
      </ld>
      <sfc>
        <scaling x="1" y="1" />
      </sfc>
    </coordinateInfo>
    <addData>
      <data name="http://www.3s-software.com/plcopenxml/projectinformation" handleUnknown="implementation">
        <ProjectInformation />
      </data>
    </addData>
  </contentHeader>
  <types>
    <dataTypes />
    <pous>
      <pou name="PRG_Barcode_Scanner" pouType="program">
        <interface>
          <localVars>
            <variable name="bExe_WriteNewConfig">
              <type>
                <BOOL />
              </type>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> example based on: https://developer.community.boschrexroth.com/t5/ctrlX-PLC/USB-RS232-adapter/m-p/54609
 see forum: https://developer.community.boschrexroth.com/t5/ctrlX-PLC/USB-serial-communication-via-USB-C-USB-A-adapter-dev-ttyACM0/m-p/73146/highlight/true#M900
 see forum: https://developer.community.boschrexroth.com/t5/ctrlX-PLC/Edit-plc-system-cfg-Connect-Barcode-Scanner-to-PLC-project/m-p/77072</xhtml>
              </documentation>
            </variable>
            <variable name="udiResultConfig">
              <type>
                <UDINT />
              </type>
            </variable>
            <variable name="bErrorConfig">
              <type>
                <BOOL />
              </type>
            </variable>
            <variable name="bDoneConfig">
              <type>
                <BOOL />
              </type>
            </variable>
            <variable name="result">
              <type>
                <derived name="Syscom.RTS_IEC_RESULT" />
              </type>
            </variable>
            <variable name="handle">
              <type>
                <derived name="Syscom.RTS_IEC_HANDLE" />
              </type>
            </variable>
            <variable name="settings">
              <type>
                <derived name="Syscom.SysComSettings" />
              </type>
            </variable>
            <variable name="bDoneComOpen">
              <type>
                <BOOL />
              </type>
            </variable>
            <variable name="arByte">
              <type>
                <array>
                  <dimension lower="0" upper="99" />
                  <baseType>
                    <BYTE />
                  </baseType>
                </array>
              </type>
            </variable>
            <variable name="udiReturn">
              <type>
                <UDINT />
              </type>
            </variable>
            <variable name="iState">
              <type>
                <INT />
              </type>
              <initialValue>
                <simpleValue value="0" />
              </initialValue>
            </variable>
            <variable name="bInitComSettings">
              <type>
                <BOOL />
              </type>
              <initialValue>
                <simpleValue value="TRUE" />
              </initialValue>
            </variable>
            <variable name="bStartCom">
              <type>
                <BOOL />
              </type>
            </variable>
            <variable name="udiResultComOpen">
              <type>
                <UDINT />
              </type>
            </variable>
            <variable name="bErrorComOpen">
              <type>
                <BOOL />
              </type>
            </variable>
            <variable name="udiLastReceivedNumberOfBytes">
              <type>
                <UDINT />
              </type>
            </variable>
            <variable name="bComReadCyclicActive">
              <type>
                <BOOL />
              </type>
            </variable>
            <variable name="sChar">
              <type>
                <string length="1" />
              </type>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> STRING (one character)</xhtml>
              </documentation>
            </variable>
            <variable name="sStringFinished">
              <type>
                <string length="11" />
              </type>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> generated STRING</xhtml>
              </documentation>
            </variable>
            <variable name="iIdx">
              <type>
                <INT />
              </type>
              <documentation>
                <xhtml xmlns="http://www.w3.org/1999/xhtml"> for loop index</xhtml>
              </documentation>
            </variable>
          </localVars>
        </interface>
        <body>
          <ST>
            <xhtml xmlns="http://www.w3.org/1999/xhtml">CASE iState OF

0:

	(* Code erhalten am 12.06.2023. Damit ist es nun möglich, auch ttyACM-Devices am USB zu nutzen. Erfolgreich getestet am 13.06.2023 *)
	(* Diese Sequenz 0 muss einmalige ausgeführt werden und anschliessend muss ein Reboot durchgeführt werden *)
	IF bExe_WriteNewConfig THEN
		udiResultConfig := SettgSetStringValue(pszComponent:='SysCom', pszKey:='Linux.DeviceFile', pszValue:='/dev/ttyACM', iLen:=11);
		IF (udiResultConfig &lt;&gt; 0) THEN
			bErrorConfig := TRUE;
		ELSE
			bDoneConfig := TRUE;
		END_IF
		bExe_WriteNewConfig := FALSE;
		
		
	ELSIF bStartCom
	AND NOT bDoneComOpen THEN
		(* next step, open com port *)
		iState:= 1;
	END_IF

	
1:

	IF bInitComSettings THEN
		(* Com settings *)
		settings.sPort		:= SYS_COMPORT1;
		settings.ulBaudrate	:= SYS_BR_115200;
		settings.byParity	:= SYS_NOPARITY;
		settings.byStopBits	:= SYS_ONESTOPBIT;
		bInitComSettings 	:= FALSE;
	END_IF


	(* Com open *)
	handle := Syscom.SysComOpen2(
						pSettings:= ADR(settings),
						pSettingsEx:= 0,
						pResult:= ADR(result));
			
			
	IF (udiResultComOpen &lt;&gt; 0) THEN
		bErrorComOpen := TRUE;
	ELSE			
		bDoneComOpen := TRUE;
		iState:= 2;
	END_IF
	
	

2:
	(* Com read *)
	udiReturn := Syscom.SysComRead(
			hCom		:= handle, 
			pbyBuffer	:= ADR(arByte), 
			ulSize		:= SIZEOF(arByte), 
			ulTimeout	:= 0, 
			pResult		:= ADR(result));
			
			sStringFinished := ''; // clear STRING
			FOR iIdx:=0 TO 29 BY 1 DO
            	sChar[0] := arByte[iIdx];          // get ASCII from BYTE-buffer and save one character
            	sStringFinished := CONCAT(sStringFinished, sChar);     // build the whole STRING         
			END_FOR

		
	IF udiReturn &lt;&gt; 0 THEN
		udiLastReceivedNumberOfBytes := udiReturn;
	END_IF
		
	bComReadCyclicActive := TRUE;	

END_CASE
</xhtml>
          </ST>
        </body>
        <addData>
          <data name="http://www.3s-software.com/plcopenxml/objectid" handleUnknown="discard">
            <ObjectId>f7190c7d-136a-42ad-b3c5-5ce494df823e</ObjectId>
          </data>
        </addData>
      </pou>
    </pous>
  </types>
  <instances>
    <configurations />
  </instances>
  <addData>
    <data name="http://www.3s-software.com/plcopenxml/projectstructure" handleUnknown="discard">
      <ProjectStructure>
        <Object Name="PRG_Barcode_Scanner" ObjectId="f7190c7d-136a-42ad-b3c5-5ce494df823e" />
      </ProjectStructure>
    </data>
  </addData>
</project>