80 lines
1.6 KiB
Text
80 lines
1.6 KiB
Text
|
/*@!Encoding:1252*/
|
|||
|
|
|||
|
includes
|
|||
|
{
|
|||
|
#include "TcpCommon.can"
|
|||
|
}
|
|||
|
|
|||
|
variables
|
|||
|
{
|
|||
|
int state = 0;
|
|||
|
}
|
|||
|
|
|||
|
// Get information of local network interface such like ip address
|
|||
|
|
|||
|
on preStart
|
|||
|
{
|
|||
|
setStartdelay(1000);
|
|||
|
}
|
|||
|
|
|||
|
on start
|
|||
|
{
|
|||
|
long fehler;
|
|||
|
|
|||
|
fehler = TcpConnectTo("192.168.1.3", 502);
|
|||
|
state = fehler == 0;
|
|||
|
}
|
|||
|
|
|||
|
on key 'd'
|
|||
|
{
|
|||
|
SendTcpData();
|
|||
|
}
|
|||
|
|
|||
|
void SendTcpData()
|
|||
|
{
|
|||
|
const word length = 12;
|
|||
|
char buffer[length];
|
|||
|
|
|||
|
if (state == 0)
|
|||
|
{
|
|||
|
writelineex(0, 2, "Tcp socket is invalid!");
|
|||
|
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
// Modbus Application Header
|
|||
|
buffer[00] = 0x23; // [2] Transaction ID
|
|||
|
buffer[01] = 0x45;
|
|||
|
buffer[02] = 0x00; // [2] Protocol ID = 0
|
|||
|
buffer[03] = 0x00;
|
|||
|
buffer[04] = (length-6)>>8; // [2] Length; Number of bytes following
|
|||
|
buffer[05] = (length-6);
|
|||
|
buffer[06] = 0xFF; // [1] Unit identifier; not relevant
|
|||
|
// Payload
|
|||
|
buffer[07] = 0x01; // [1] Function Code; 1:Read DI; 2:Read DIO; 3:Read AIO; 4:Read AI; 5:Write 1 DO; 6:Write 1 AO; 7: Read Exeption, 15:Write n DO; 16:Write n AO;
|
|||
|
buffer[08] = 0x00; // [2] Start address
|
|||
|
buffer[09] = 0x00;
|
|||
|
buffer[10] = 0x01; // [2] Number of items; 1:max 2000=0x7D0
|
|||
|
buffer[11] = 0xFF;
|
|||
|
|
|||
|
write("Sende '%s' (L<>nge: %d)", buffer, length);
|
|||
|
|
|||
|
if (gSocket.Send(buffer, elCount(buffer)) != 0)
|
|||
|
{
|
|||
|
gIpLastErr = gSocket.GetLastSocketError();
|
|||
|
|
|||
|
if (gIpLastErr != WSA_IO_PENDING)
|
|||
|
{
|
|||
|
gSocket.GetLastSocketErrorAsString(gIpLastErrStr, elcount(gIpLastErrStr));
|
|||
|
|
|||
|
writelineex(0, 2, "Tcp send error (%d): %s", gIpLastErr, gIpLastErrStr);
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
writelineex(0, 1, "Tcp data sent successfully!");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/**/
|