鍦–odesys鍒╃敤Socket鎿嶄綔Mysql-CODESYS璧勬簮涓嬭浇鍖-閰峰痉缃 - CoDeSys杞欢鏁欑▼涓嬭浇,CoDeSys宸ユ帶璧勮闂ㄦ埛
HSC6490-cn涓浗鍒堕狅紝涓浗鑺瘇
鏌ョ湅: 1286|鍥炲: 0

鍦–odesys鍒╃敤Socket鎿嶄綔Mysql

[澶嶅埗閾炬帴]

璇ョ敤鎴蜂粠鏈鍒

鍙戣〃浜 2023-4-20 13:37:32 | 鏄剧ず鍏ㄩ儴妤煎眰 |闃呰妯″紡
涓銆佹杩

铏界劧Codesys鏈変粯璐圭殑mysql鎿嶄綔搴擄紝浣嗘槸涓轰簡鏇村ぇ鐨勮嚜鐢卞害锛屾垜鍒╃敤Codesys鐨剆ocket鑷繁鍘诲疄鐜癕ysql鐨勬搷浣溿
鎿嶄綔Mysql鐨勭▼搴忕殑閫昏緫寰堢畝鍗曪細寤虹珛杩炴帴->鎺ユ敹骞惰В鏋愯璇佸寘->鍙戦佹暟鎹簱鐧诲綍楠岃瘉淇℃伅鍖->鑾峰緱鎺堟潈->鍙戦丵uery鍛戒护->鎺ユ敹骞惰В鏋愮粨鏋->鏂紑杩炴帴銆傚叾涓璇佽繃绋嬶紝鐢ㄥ埌浜哠HA1鍔犲瘑銆
Codesys涓婏紝闇瑕佺敤鍒"CAA Net Base Services" 鍜 "CAA Memory"涓や釜鍏叡搴撱
浜屻佺▼搴忕粍缁
鍦–odesys鍒╃敤Socket鎿嶄綔Mysql-1.png




涓夈佷唬鐮佸睍绀

3.1AuthPacket
[AppleScript] 绾枃鏈煡鐪 澶嶅埗浠g爜
//浣滆:AlongWu
 
TYPE AuthPacket :
STRUCT
 
FrameDataLen                :UDINT:=0;
Sqid                        :BYTE:=1;
ClientCap1                  :WORD:=16#A68D;
ClientCap2                  :WORD:=16#006F;
MaxSize                     :UDINT:=16#00FFFFFF;
CharSetCode                 :BYTE:=16#21;
UserName                    :STRING(50);
DataBaseName                :STRING(50);
Password                    :ARRAY[0..19] OF BYTE;                    //鍔犲瘑鍚庣殑璁よ瘉鎽樿
clientAuthPlugin            :STRING(50):='mysql_native_password';
 
 
 
END_STRUCT
END_TYPE

3.2HandShakePacket
[AppleScript] 绾枃鏈煡鐪 澶嶅埗浠g爜
//浣滆:AlongWu
TYPE HandShakePacket :
STRUCT
    
FrameDataLen                :UINT;                             //packet鐨勫唴瀹瑰尯瀛楄妭鏁
DataBaseMsg                 :STRING(50);                       //褰撳墠鏈嶅姟鐨刴ysql鐗堟湰淇℃伅
Sqid                        :BYTE;                             //浜や簰id
ProtoolId                   :BYTE;                             //鍗忚鍙
ServerThreadId              :UDINT;                            //鏈嶅姟鍣ㄧ嚎绋媔d
AuthBuffer                  :ARRAY[0..19] OF BYTE;             //20Byte鐨勯殢鏈鸿璇佺爜
ServerCap                   :WORD;                             //褰撳墠鏈嶅姟鍣ㄦ潈鑳界粍(16浣嶏紝姣忎綅浠h〃1椤硅兘鍔)
CharSetCode                 :BYTE;                             //瀛楃缂栫爜
ServerState                 :WORD;                             //褰撳墠鏈嶅姟鍣ㄧ姸鎬
 
 
END_STRUCT
END_TYPE

3.3MysqlConnectInfo
[AppleScript] 绾枃鏈煡鐪 澶嶅埗浠g爜
//浣滆:AlongWu
TYPE MysqlConnectInfo :
STRUCT
    
ServerIp            :NBS.IP_ADDR:=(sAddr:='192.168.0.105');             //mysql鏈嶅姟鍣╥p鍦板潃
Port                :UINT:=3306;                                        //鏈嶅姟鍣ㄧ鍙e彿
UserName            :STRING(50):='SpinMachine';                         //鐧诲綍璐﹀彿
Password            :STRING(50):='123456';                              //鐧诲綍瀵嗙爜
DataBase            :STRING(50):='spinmachine3';                        //鏁版嵁搴撳悕绉
ip1                 :BYTE:=192;
ip2                 :BYTE:=168;
ip3                 :BYTE:=0;
ip4                 :BYTE:=105;
 
 
END_STRUCT
END_TYPE

3.4MysqlRowInfo
[AppleScript] 绾枃鏈煡鐪 澶嶅埗浠g爜
//浣滆:AlongWu
TYPE MysqlRowInfo :
STRUCT
    
ColumnCount            :BYTE;                             //鍒楁暟閲
RowData                :ARRAY[0..999] OF BYTE;            //琛岀殑瀛楄妭鏁扮粍
IsNull                 :BOOL;                             //鏁版嵁琛ㄤ负绌
 
 
END_STRUCT
END_TYPE

3.5MysqlFirstRowData_FB
[AppleScript] 绾枃鏈煡鐪 澶嶅埗浠g爜
//浣滆:AlongWu
FUNCTION_BLOCK MysqlFirstRowData_FB
 
VAR CONSTANT
abyEmpty                           :ARRAY [0..999] OF BYTE;             //澶嶄綅buffer
END_VAR
 
VAR_INPUT
    
InputRxBytes                       :ARRAY[0..999] OF BYTE;              //鎺ユ敹瀛楄妭鏁扮粍
    
END_VAR
 
VAR_OUTPUT
    
FirstRowInfo                        :MysqlRowInfo;                      //瑙f瀽鍚庨琛屾暟鎹紝缁撴瀯浣
 
END_VAR
 
 
VAR
columnCount                         :BYTE;                               //鍒楁暟閲    
Addr                                :UINT;                               //鍦板潃
i                                   :UINT;            
TmpfieldSize                        :BYTE;                               //褰撳墠field瀛楄妭鏁
                        
END_VAR
 
 
(*
        
Result Set Header          杩斿洖鏁版嵁鐨勫垪鏁伴噺
Field                      杩斿洖鏁版嵁鐨勫垪淇℃伅锛堝涓級
EOF                        鍒楃粨鏉
Row Data                   琛屾暟鎹紙澶氫釜锛
EOF                        鏁版嵁缁撴潫
 
涓句緥锛
 
杩斿洖锛3琛3鍒楃殑琛ㄦ牸鏁版嵁銆
 
杩斿洖鐨勬暟鎹抚1娆★紝 璇ュ抚閲岄潰鏈 9涓猵acket锛屽嵆9涓猵acket鏄湪鍚屼竴涓姤鏂囬噷闈€
绗1涓猵acket =Result Set Header 
绗2涓猵acket =鍒1鐨刦ield 
绗3涓猵acket =鍒2鐨刦ield
绗4涓猵acket =鍒3鐨刦ield
绗5涓猵acket =EOF packet
绗6涓猵acket =琛1鐨刾acket 
绗7涓猵acket =琛2鐨刾acket 
绗8涓猵acket =琛3鐨刾acket 
绗9涓猵acket =EOF packet
 
*)
    
(*
    Result Set Header          杩斿洖鏁版嵁鐨勫垪鏁伴噺
    
    packlen                   :3B
    packid                    :1B
    columnCount               :1B
    
*)
 
 
 
(*
Field packet
packlen               :3B
packid                :1B
LengthEncodedString  鐩綍鍚嶇О
LengthEncodedString  鏁版嵁搴撳悕绉
LengthEncodedString  鏁版嵁琛ㄥ悕绉
LengthEncodedString  鏁版嵁琛ㄥ師濮嬪悕绉
LengthEncodedString  鍒楋紙瀛楁锛夊悕绉
LengthEncodedString  鍒楋紙瀛楁锛夊師濮嬪悕绉
int<1>     濉厖鍊
int<2>     瀛楃缂栫爜
int<4>     鍒楋紙瀛楁锛夐暱搴
int<1>     鍒楋紙瀛楁锛夌被鍨
int<2>     鍒楋紙瀛楁锛夋爣蹇
int<1>     鏁村瀷鍊肩簿搴
int<2>     濉厖鍊硷紙0x00锛
LengthEncodedString  榛樿鍊
 
row Packet
packlen               :3B
packid                :1B
data1_len             :1B = data鐨勫瓧鑺傛暟
data                  :data1_len B
data2_len             :1B = data鐨勫瓧鑺傛暟
data                  :data2_len B
......     澶氫釜瀛楁鍊
 
 
*)
 
 
 
//鍒楁暟閲
columnCount := GVL.Mysql_abyRx[4];
 
//褰曞叆鍒楁暟閲
FirstRowInfo.ColumnCount :=columnCount;
 
//娓呯┖buffer
FirstRowInfo.RowData := abyEmpty;
 
//澶嶄綅isNull鏍囧織
FirstRowInfo.IsNull := FALSE;
 
//鍒濆鍖栧湴鍧
Addr := 5;
 
columnCount := columnCount-1;
 
FOR i:=0 TO columnCount BY 1 DO
    Addr := Addr + GVL.Mysql_abyRx[Addr]+4;
END_FOR
 
//EOF packet
Addr := Addr + GVL.Mysql_abyRx[Addr]+4;
 
//棣栦釜row鐨勫瓧绗﹀鍒    
 
IF GVL.Mysql_abyRx[Addr] = 5 AND GVL.Mysql_abyRx[Addr+4] = 16#FE THEN
    //濡傛灉field鐨別of鍚庨潰杩樻槸 eof锛屽嵆琛屾暟鎹负绌
    FirstRowInfo.IsNull := TRUE;
ELSE
    MEM.MemMove(pSource:= ADR(GVL.Mysql_abyRx)+Addr+4, pDestination:= ADR(FirstRowInfo.RowData), uiNumberOfBytes:=BYTE_TO_UINT(GVL.Mysql_abyRx[Addr]));    
 
END_IF
 

3.6MysqlQueryCmdPack_FB
[AppleScript] 绾枃鏈煡鐪 澶嶅埗浠g爜
//浣滆:AlongWu
FUNCTION_BLOCK MysqlQueryCmdPack_FB
 
VAR CONSTANT
    EmptyResult           : ARRAY[0..999] OF BYTE;
END_VAR
 
VAR_INPUT
Cmd                       :BYTE;                                 //鍛戒护鐮
InputBytes                :ARRAY[0..998] OF BYTE;                //璇锋眰鎸囦护鐨勫唴瀹
END_VAR
 
VAR_OUTPUT
Result                    :ARRAY[0..999] OF BYTE;
ResultSize                :UINT;
END_VAR
 
VAR
    
 
PacketLen                 :UDINT;                              //鍖呯殑鍐呭鐨勫瓧鑺傞暱搴 (鎬诲瓧鑺傛暟 - 4)
tmp4byte                  :ARRAY[0..3] OF BYTE;
i                         :UINT;
END_VAR
 
(*
鍛戒护鐮佸垪琛
 
0x00 COM_SLEEP 锛堝唴閮ㄧ嚎绋嬬姸鎬)
0x01 COM_QUIT 鍏抽棴杩炴帴
0x02 COM_INIT_DB 鍒囨崲鏁版嵁搴
0x03 COM_QUERY SQL鏌ヨ璇锋眰
0x04 COM_FIELD_LIST 鑾峰彇鏁版嵁琛ㄥ瓧娈典俊鎭
0x05 COM_CREATE_DB 鍒涘缓鏁版嵁搴
0x06 COM_DROP_DB 鍒犻櫎鏁版嵁搴
0x07 COM_REFRESH 娓呴櫎缂撳瓨
0x08 COM_SHUTDOWN 鍋滄鏈嶅姟鍣
0x09 COM_STATISTICS 鑾峰彇鏈嶅姟鍣ㄧ粺璁′俊鎭
0x0A COM_PROCESS_INFO 鑾峰彇褰撳墠杩炴帴鐨勫垪琛
0x0B COM_CONNECT 锛堝唴閮ㄧ嚎绋嬬姸鎬)
0x0C COM_PROCESS_KILL 涓柇鏌愪釜杩炴帴
0x0D COM_DEBUG 淇濆瓨鏈嶅姟鍣ㄨ皟璇曚俊鎭
0x0E COM_PING 娴嬭瘯杩為氭
0x0F COM_TIME 锛堝唴閮ㄧ嚎绋嬬姸鎬侊級
0x10 COM_DELAYED_INSERT 锛堝唴閮ㄧ嚎绋嬬姸鎬侊級
0x11 COM_CHANGE_USER 閲嶆柊鐧婚檰锛堜笉鏂繛鎺ワ級
0x12 COM_BINLOG_DUMP 鑾峰彇浜岃繘鍒舵棩蹇椾俊鎭
0x13 COM_TABLE_DUMP 鑾峰彇鏁版嵁琛ㄧ粨鏋勪俊鎭
0x14 COM_CONNECT_OUT 锛堝唴閮ㄧ嚎绋嬬姸鎬)
0x15 COM_REGISTER_SLAVE 浠庢湇鍔″櫒鍚戜富鏈嶅姟鍣ㄨ繘琛屾敞鍐
0x16 COM_STMT_PREPARE 棰勫鐞哠QL璇彞
0x17 COM_STMT_EXECUTE 鎵ц棰勫鐞嗚鍙
0x18 COM_STMT_SEND_LONG_DATA 鍙戦丅LOB绫诲瀷鐨勬暟鎹
0x19 COM_STMT_CLOSE 閿姣侀澶勭悊璇彞
0x1A COM_STMT_RESET 娓呴櫎棰勫鐞嗚鍙ュ弬鏁扮紦瀛
0x1B COM_SET_OPTION 璁剧疆璇彞閫夐」
0x1C COM_STMT_FETCH 鑾峰彇棰勫鐞嗚鍙ョ殑鎵ц缁撴灉
*)
 
 
 
//鍏堝浣
Result := EmptyResult;
 
//鑾峰彇string鐨勫唴瀹圭殑瀛楄妭闀垮害,涓嶅惈0缁撴潫绗
i:=0;
WHILE  InputBytes[i] <> 0 AND i<999 DO
    i:=i+1;
END_WHILE
 
//鍖呯殑鍐呭 = cmd + string ;
PacketLen :=UINT_TO_UDINT(1+i+1);
 
ResultSize :=1+i+1;
 
 
 
//鍖呯殑鍐呭闀垮害璧嬪
MEM.MemMove(pSource:= ADR(PacketLen), pDestination:= ADR(Result), uiNumberOfBytes:=3 );
 
 
//sid
Result[3] := 0;
 
//cmd
Result[4] := Cmd;
 
//cmd鍐呭璧嬪
MEM.MemMove(pSource:= ADR(InputBytes), pDestination:= ADR(Result)+5, uiNumberOfBytes:=ResultSize);
 
//鍖呮诲瓧鑺傛暟
ResultSize := ResultSize +4;
 

3.7SHA1_FB
[AppleScript] 绾枃鏈煡鐪 澶嶅埗浠g爜
//浣滆:AlongWu
FUNCTION_BLOCK SHA1_FB
 
VAR CONSTANT
Kt            :ARRAY[0..3] OF DWORD:=[16#5A827999,16#6ED9EBA1,16#8F1BBCDC,16#CA62C1D6];                        //鍥哄畾K甯搁噺
Ht            :ARRAY[0..4] OF DWORD:=[16#67452301,16#EFCDAB89,16#98BADCFE,16#10325476,16#C3D2E1F0];            //鍥哄畾H甯搁噺
emptpBuff    :ARRAY[0..63] OF BYTE;    
END_VAR
 
VAR_INPUT
    InputBytes            :ARRAY[0..49] OF BYTE;                //杈撳叆瀛楄妭鏁扮粍
 
END_VAR
VAR_OUTPUT
    ResultCode            :ARRAY[0..19] OF BYTE;    ;
END_VAR
VAR
    
strSize                   :UINT;
i                         :UINT;
addr                      :UINT;
 
tmpBuffer                :ARRAY[0..63] OF BYTE;
tmp2Byte                 :ARRAY[0..1] OF BYTE;
Ht_temp                  :ARRAY[0..4] OF DWORD:=[16#67452301,16#EFCDAB89,16#98BADCFE,16#10325476,16#C3D2E1F0];            //缂撳啿鐨凥甯搁噺
Wt                       :ARRAY[0..79] OF DWORD;
HtTmp                    :DWORD;
 
 
 
END_VAR
 
//Ht_temp,澶嶄綅
Ht_temp := Ht;
tmpBuffer := emptpBuff;
i:=0;
 
//鎵惧嚭瀛楃涓插瓧鑺傞暱搴
WHILE  InputBytes[i] <> 0 AND i<50 DO
    i:=i+1;
END_WHILE
 
strSize:=i;
addr := i;
 
MEM.MemMove(pSource:= ADR(InputBytes), pDestination:= ADR(tmpBuffer), uiNumberOfBytes:=addr );
 
//瀛楃鏈鍚巄yte鐨勫悗闈㈣ˉ 0x10
tmpBuffer[addr] := 16#80;
 
 
//瀛楃鐨勬讳綅鏁帮紝 瀛楄妭鏁*8
strSize := strSize *8;
 
MEM.MemMove(pSource:= ADR(strSize), pDestination:= ADR(tmp2Byte), uiNumberOfBytes:=2 );
 
//鍦ㄦ渶鍚庣殑2涓猙yte锛屽綍鍏 杈撳叆瀛楃鐨勪綅鏁般
//楂樹綅鍦ㄥ墠锛屼綆浣嶅湪鍚
tmpBuffer[62]:= tmp2Byte[1];
tmpBuffer[63]:= tmp2Byte[0];
 
 
//Wt 鍓 16涓 32浣嶏紝涓  tmpBuffer 64涓8浣
FOR i:=0 TO 15 BY 1 DO
    addr := i*4;    
    MEM.MemMove(pSource:= ADR(tmpBuffer)+addr, pDestination:= ADR(Wt[i]), uiNumberOfBytes:=4 );    
    Wt[i] := MEM.ReverseBYTEsInDWORD (Wt[i]);
END_FOR
 
 
 
//鎵╁睍鍒 80涓32浣
FOR i:=16 TO 79 BY 1 DO
    addr := i*4;
    Wt[i] := Wt_Func(Wt[i-3],Wt[i-8],Wt[i-14],Wt[i-16]);
END_FOR
 
 
 
FOR i:=0 TO 19 BY 1 DO
    HtTmp :=Htemp_Func(Ht_temp[0],Ft1_Func(Ht_temp[1],Ht_temp[2],Ht_temp[3]),Ht_temp[4],Wt[i],Kt[0]);
    Ht_temp[4] := Ht_temp[3];
    Ht_temp[3] := Ht_temp[2];
    Ht_temp[2] := ROL(Ht_temp[1],30);
    Ht_temp[1] := Ht_temp[0];
    Ht_temp[0] := HtTmp;
END_FOR
 
 
FOR i:=20 TO 39 BY 1 DO
    HtTmp :=Htemp_Func(Ht_temp[0],Ft2_Func(Ht_temp[1],Ht_temp[2],Ht_temp[3]),Ht_temp[4],Wt[i],Kt[1]);
    Ht_temp[4] := Ht_temp[3];
    Ht_temp[3] := Ht_temp[2];
    Ht_temp[2] := ROL(Ht_temp[1],30);
    Ht_temp[1] := Ht_temp[0];
    Ht_temp[0] := HtTmp;
END_FOR
 
 
 
 
FOR i:=40 TO 59 BY 1 DO
    HtTmp :=Htemp_Func(Ht_temp[0],Ft3_Func(Ht_temp[1],Ht_temp[2],Ht_temp[3]),Ht_temp[4],Wt[i],Kt[2]);
    Ht_temp[4] := Ht_temp[3];
    Ht_temp[3] := Ht_temp[2];
    Ht_temp[2] := ROL(Ht_temp[1],30);
    Ht_temp[1] := Ht_temp[0];
    Ht_temp[0] := HtTmp;
END_FOR
 
 
FOR i:=60 TO 79 BY 1 DO
    HtTmp :=Htemp_Func(Ht_temp[0],Ft2_Func(Ht_temp[1],Ht_temp[2],Ht_temp[3]),Ht_temp[4],Wt[i],Kt[3]);
    Ht_temp[4] := Ht_temp[3];
    Ht_temp[3] := Ht_temp[2];
    Ht_temp[2] := ROL(Ht_temp[1],30);
    Ht_temp[1] := Ht_temp[0];
    Ht_temp[0] := HtTmp;
END_FOR
 
 
FOR i:=0 TO 4 BY 1 DO
    Ht_temp[i] := Ht_temp[i] + Ht[i];
    Ht_temp[i] := MEM.ReverseBYTEsInDWORD (Ht_temp[i]);
END_FOR
 
 
MEM.MemMove(pSource:= ADR(Ht_temp), pDestination:= ADR(ResultCode), uiNumberOfBytes:=20 );
 
 

3.7.1Ft1_Func
[AppleScript] 绾枃鏈煡鐪 澶嶅埗浠g爜
Ft1_Func := (B AND C) OR ((NOT B) AND D);

3.7.2Ft2_Func
[AppleScript] 绾枃鏈煡鐪 澶嶅埗浠g爜
Ft2_Func := B XOR C XOR D;

3.7.3Ft3_Func
[AppleScript] 绾枃鏈煡鐪 澶嶅埗浠g爜
Ft3_Func :=(B AND C) OR (B AND D) OR (C AND D);

3.7.4Htemp_Func
[AppleScript] 绾枃鏈煡鐪 澶嶅埗浠g爜
Htemp_Func := ROL(A,5) +Ft + E + W + K;

3.7.5Wt_Func
[AppleScript] 绾枃鏈煡鐪 澶嶅埗浠g爜
temp := Wi_3 XOR Wi_8 XOR Wi_14 XOR Wi_16;
 
//寰幆宸︾Щ1浣
Wt_Func := ROL(temp,1);

3.8MysqlClient
[AppleScript] 绾枃鏈煡鐪 澶嶅埗浠g爜
//浣滆:AlongWu
PROGRAM MysqlClient
 
 
VAR CONSTANT
abyEmpty                :ARRAY [0..999] OF BYTE;                //澶嶄綅abyRx
ClearSha1Input          :ARRAY[0..49] OF BYTE;
END_VAR
 
VAR
    
 
 
CloseOrder               :ARRAY[0..4] OF BYTE:=[1,0,0,0,1];        //鍏抽棴杩炴帴鍛戒护
 
 
Mysql_Init_Step          :BYTE:=0;                        //mysql閫氫俊姝ラ锛0-璁よ瘉锛1-鐧诲綍锛2-query
 
Enable_F_TRIG            :F_TRIG;                        //mysql閫氫俊浣胯兘涓嬮檷娌
Enable_R_TRIG            :R_TRIG;                        //mysql閫氫俊浣胯兘涓婂崌娌
Recei_R_TRIG             :R_TRIG;                        //鎺ユ敹涓婂崌娌
Close_R_TRIG             :R_TRIG;                        //鍏抽棴杩炴帴涓婂崌娌
Send_R_TRIG              :R_TRIG;                        //鍙戦佷笂鍗囨部
 
HSP                      :HandShakePacket;                //mysql璁よ瘉鎶ユ枃
AuthP                    :AuthPacket;                    //client鍥炲鐨勭敵璇锋姤鏂
Daddr                    :UINT;                            //瑙g爜byte鍦板潃
i                        :UINT;
TempFrameLen             :UDINT;                            //甯х殑鍐呭鍖哄瓧鑺傛暟
TempAddr                 :UINT;                            //缂撳啿鍦板潃
TempUint1                :UINT;                            //涓棿鍙橀噺1,UINT
//TempInt1               :INT;                            //涓棿鍙橀噺1锛孖NT
tempSha1                 :ARRAY[0..19] OF BYTE;    ;
tempSha2                 :ARRAY[0..19] OF BYTE;    ;
tempSha3                 :ARRAY[0..19] OF BYTE;    ;
 
tmpStrBytes                :ARRAY[0..49] OF BYTE;
 
LongSha1Bytes            :ARRAY[0..39] OF BYTE;
 
TempDw                    :DWORD;
SHA1                    :SHA1_FB;                //瀹炰緥SHA1绠楁硶
 
ReplyFirstByte            :BYTE;                    //mysql鍥炲鐨勫唴瀹圭殑棣栦釜瀛楄妭
 
WaitCount                :UINT:=0;                    //绌洪棽璁℃椂
 
TryConnect                :UINT;
TryAuth                    :UINT;
 
END_VAR
 

3.9MysqlUserLogin
[AppleScript] 绾枃鏈煡鐪 澶嶅埗浠g爜
//浣滆:AlongWu3.1
PROGRAM MysqlUserLogin
 
VAR CONSTANT
    EmptyResult            :ARRAY[0..998] OF BYTE;
    EmptyAuthPStr          :STRING(4):='';
END_VAR
 
 
VAR
 
 
WB_UserLogin                    AT %IX115.7     :BOOL;                //璐﹀彿鐧诲綍
WB_UserExit                     AT %IX116.0     :BOOL;                //璐﹀彿娉ㄩ攢
 
 
WHMI_Account                         AT %IW4850       :STRING(25);                        //杈撳叆鎿嶄綔鑰呰处鍙凤紝utf-8缂栫爜锛25涓嫳鏂囷紝25涓猙yte
WHMI_Password                        AT %IW4900       :STRING(25);                        //杈撳叆鎿嶄綔鑰呭瘑鐮侊紝utf-8缂栫爜锛25涓嫳鏂囷紝25涓猙yte
 
 
 
WB_UserLogin_R_TRIG                                   :R_TRIG;            //璐﹀彿鐧诲綍
WB_UserExit_R_TRIG                                    :R_TRIG;            //璐﹀彿娉ㄩ攢
 
 
B_Login_F_TRIG                                        :F_TRIG;            //璐﹀彿涓嬮檷娌
    
B_Login                                               :BOOL:=FALSE;                                //鍚姩鐧诲綍
B_LoginDeny                                           :BOOL;                                //鐧诲綍璐﹀彿瀵嗙爜閿欒
 
B_Login_R_TRIG                                        :R_TRIG;                            //鐧诲綍涓婂崌娌
LoginStep                                             :BYTE;                                //鐧诲綍姝ラ
 
QueryStr                                              :STRING(100);                        //鏌ヨ鍛戒护瀛楃涓
QueryBytes                                            :ARRAY[0..998] OF BYTE;                //鏌ヨ鍛戒护buffer
 
MysqlCmdPack                                          :MysqlQueryCmdPack_FB;                //Mysql鎸囦护瀹炰緥
 
MysqlFirstRowInfo                                     :MysqlRowInfo;                        //Mysql棣栬鏁版嵁缁撴瀯浣
MysqlFirstRowDecode                                   :MysqlFirstRowData_FB;                //Mysql瑙f瀽鍥炲琛ㄦ牸淇℃伅骞舵彁鍙栭琛屾暟鎹
 
AuthpStr                                              :STRING(4);
tmpUint                                               :UINT;
 
END_VAR
 
 
WB_UserLogin_R_TRIG(CLK:=WB_UserLogin , Q=> );                                                    //璐﹀彿鐧诲綍
WB_UserExit_R_TRIG(CLK:=WB_UserExit , Q=> );                                                    //璐﹀彿娉ㄩ攢
 
 
IF WB_UserLogin_R_TRIG.Q THEN
    //Mysql鍚姩鐧诲綍娴佺▼
    B_Login := TRUE;
END_IF
 
IF WB_UserExit_R_TRIG.Q THEN
    //娉ㄩ攢璐﹀彿
    B_Login := FALSE;
END_IF
 
 
 
B_Login_R_TRIG(CLK:=B_Login , Q=> );                                                    //璐﹀彿鐧诲綍涓婂崌娌
B_Login_F_TRIG(CLK:=B_Login , Q=> );                                                    //璐﹀彿鐧诲綍涓嬮檷娌
 
IF B_Login_F_TRIG.Q OR GVL.B_Mysql_AuthFalure THEN
    GVL.Mysql_AccountAutp := 0;
    
    GVL.RB_Mysql_Logining:=FALSE;
    GVL.B_Mysql_Login := FALSE;
    B_Login := FALSE;
END_IF
 
IF B_Login_R_TRIG.Q THEN
    LoginStep := 0;
    B_LoginDeny :=FALSE;
    GVL.B_Mysql_Login := FALSE;
    
END_IF
 
IF B_Login THEN
 
    CASE LoginStep OF 
        0:
            GVL.RB_Mysql_Logining:=TRUE;
            QueryBytes := EmptyResult;            //鎸囦护buffer澶嶄綅
            
            IF GVL.B_Mysql_Inited THEN 
                LoginStep := 10;                  //宸茶繛鎺ユ暟鎹簱
            ELSE
                LoginStep := LoginStep +1;        //鏈繛鎺ユ暟鎹簱,涓嬩竴姝
            END_IF
        1:
            GVL.B_Mysql_Enable :=TRUE;
            LoginStep := LoginStep +1;            //涓嬩竴姝
        2:
            IF GVL.B_Mysql_Inited THEN         
                LoginStep := 10;                  //宸茶繛鎺ユ暟鎹簱
            END_IF
            
            IF GVL.B_Mysql_AuthFalure THEN        //杩炴帴澶辫触锛屾帹鍑虹櫥褰
                B_Login := FALSE;
            END_IF
        10:
            GVL.B_Mysql_Result :=FALSE;            //澶嶄綅鎺ユ敹瑙f瀽鏍囧織
            
            // '瑕佺敤$27鏉ヨ浆涔夛紝鐗规畩鐨勭鍙凤紝鐢$+16杩涘埗鐨刟csii鍊
            QueryStr :='SELECT AUTP FROM `login` WHERE STATE=1 AND USER=$27';
            QueryStr := CONCAT(QueryStr,WHMI_Account);
            QueryStr := CONCAT(QueryStr,'$27 AND PASSWORD=$27');
            QueryStr := CONCAT(QueryStr,WHMI_Password);
            QueryStr := CONCAT(QueryStr,'$27 LIMIT 1');
            MEM.MemMove(pSource:= ADR(QueryStr), pDestination:= ADR(QueryBytes), uiNumberOfBytes:=INT_TO_UINT(LEN(QueryStr)));
            
            MysqlCmdPack(Cmd:=3 , InputBytes:= QueryBytes, Result=> GVL.Mysql_abyTx, ResultSize=>GVL.Mysql_WriteSize );        
            GVL.B_Mysql_Send :=TRUE;
            LoginStep := LoginStep +1;               //涓嬩竴姝
        11:
            IF GVL.B_Mysql_FF OR GVL.B_Mysql_FE THEN
                B_Login := FALSE;                    //鏁版嵁搴撻敊璇紝閫鍑虹櫥褰
            END_IF
        
            IF GVL.B_Mysql_Result THEN
                MysqlFirstRowDecode(InputRxBytes:=GVL.Mysql_abyRx , FirstRowInfo=>MysqlFirstRowInfo );
                IF MysqlFirstRowInfo.IsNull THEN
                    //鍥炲涓虹┖锛屽嵆璐﹀彿瀵嗙爜閿欒
                    B_Login := FALSE;    
                    LoginStep:=99;
                    
                    B_LoginDeny :=TRUE;
                ELSE
                    //鍥炲涓虹湡锛岃处鍙峰瘑鐮佹纭紝骞惰幏鍙栨巿鏉冨
                    GVL.B_Mysql_Login := TRUE;
                    LoginStep:=99;
                    AuthpStr :=EmptyAuthPStr;
                    tmpUint :=MysqlFirstRowInfo.RowData[0];
                    MEM.MemMove(pSource:= ADR(MysqlFirstRowInfo.RowData)+1, pDestination:= ADR(AuthpStr), uiNumberOfBytes:=tmpUint);
                    
                    GVL.Mysql_AccountAutp :=INT_TO_BYTE( STRING_TO_INT(AuthpStr));                    
                    
                END_IF
            END_IF
            GVL.RB_Mysql_Logining:=FALSE;
        ELSE
        //NTHONG
    
    END_CASE
ELSE
    GVL.RB_Mysql_Logining:=FALSE;
END_IF
 
 
 
//鐧诲綍鎻愮ず
 
IF B_LoginDeny OR GVL.B_Mysql_FF OR GVL.B_Mysql_AuthFalure THEN
 
    B_LoginDeny :=FALSE;
END_IF
 

鍥涖侀噸瑕佹彁绀

杩欓噷瑕佺壒鍒彁閱扖odesys鐨2涓猙ug銆
1锛屽娆′慨鏀圭粨鏋勪綋鐨勫彉閲忛『搴忥紙struct锛夊悗锛屽叾鍐呴儴鐨勫彉閲忕殑瀛楄妭鎺掑垪鏄笉纭畾鐨勩傞渶瑕佸強鏃堕氳繃CleanAll鍐岹enerateCode锛屾潵淇濊瘉缁撴瀯浣撶殑鍙橀噺鐨勫瓧鑺傞『搴忔槸鏍规嵁鏈鏂扮殑椤哄簭銆傚綋缁撴瀯浣撳瓧鑺傞『搴忎笉姝g‘锛屼笉浠呬娇鐢 MEM.MemMove鍐呭瓨澶嶅埗鎿嶄綔浼氬嚭鐜板瓧鑺傞『搴忛敊璇紝浼氬彂鐜版槑鏄炬暟鎹敊涔憋紝鍚屾椂鍦ㄦ棩甯告櫘閫氱▼搴忚繍琛岃繃绋嬶紝涔熶細鏈夊嚭鐜颁笉鏄撳彂鐜扮殑鏁版嵁鍜屼綅鐨勯敊涔便傝鍔″繀鍙婃椂CleanAll銆
2锛孋odesys鐨剆ocket鍦ㄨ繛鎺ョ姸鎬佷笅锛屽鏋滃嚭鐜版柇鐢碉紝浼氬鑷碈odesys鐨勬帀鐢典繚瀛樺姩浣滃け鏁堛備篃灏辨槸PtVars鐨勫彉閲忔槸娌℃湁鍙婃椂鏇存柊涓烘柇鐢靛墠涓鍒汇傛墍浠ワ紝闇瑕佹瘡娆℃搷浣滃畬mysql锛屽強鏃舵柇寮socket銆
浜斻佹荤粨

鑷繁鍐橫ysql鎿嶄綔搴撹幏鍙栨洿澶ц嚜鐢卞害鍦ㄤ簬浠ヤ笅2涓柟闈€
1锛孋odesys鐨凷tring鎿嶄綔鍑芥暟鎵鑳芥搷浣滅殑瀛楃鏁颁负255涓瓧绗︺傚彉閲忕殑瀛楃鏁拌秴杩255锛屾棤娉曟纭疄鏂藉瓧绗︽搷浣溿傝屽疄闄呭簲鐢ㄨ繃绋嬶紝255涓瓧绗﹀浜嶪nsert,Update鍛戒护澶皯浜嗐
2锛屽Query鏌ヨ缁撴灉鐨勮В鏋愬拰鍒╃敤锛屽彲浠ユ洿鐩存帴銆

+10
涓嶈禐涓涓紵
鎵撹祻榧撳姳涓涓嬶紒
鍥炲

浣跨敤閬撳叿 涓炬姤

鎮ㄩ渶瑕佺櫥褰曞悗鎵嶅彲浠ュ洖甯 鐧诲綍 | 绔嬪嵆娉ㄥ唽 鏂版氮寰崥鐧婚檰

鏈増绉垎瑙勫垯

椤堕儴qrcode搴曢儴
鍏虫敞閰峰痉缃戣闃呭彿锛岃幏鍙栨洿澶氳祫璁紒

QQ|灏忛粦灞|鎵嬫満鐗|Archiver|閰峰痉璁哄潧 ( 鑻廔CP澶16065247鍙-1 )|缃戠珯鍦板浘

GMT+8, 2024-11-23 20:28 , Processed in 0.164008 second(s), 60 queries .

閰峰痉缃

© 2001-2013 Hicodesys. 鎶鏈敮鎸 by 閰峰痉缃

蹇熷洖澶 杩斿洖椤堕儴 杩斿洖鍒楄〃