Modbus/include/ModbusClientCommon.cin
Fix error in Exception handling Fix other issues
This commit is contained in:
parent
087453d5c3
commit
bcf79a7b27
6 changed files with 563 additions and 501 deletions
|
@ -16,6 +16,7 @@ variables
|
|||
on preStart
|
||||
{
|
||||
writeClear(0);
|
||||
setStartdelay(10);
|
||||
}
|
||||
|
||||
on start
|
||||
|
|
|
@ -9,7 +9,7 @@ variables
|
|||
{
|
||||
const word gMaxPacketLength = __size_of(struct ModbusReqWriteRegisters);
|
||||
|
||||
msTimer gtRobin;
|
||||
msTimer gtRobin; // Timer that sends the packets and watches for timeouts
|
||||
word gTxID = 0x0000; // Transaction Identifier for Modbus. Used as index for gQueue
|
||||
|
||||
// Global storage for pending and sent requests, associated by TxID
|
||||
|
@ -663,6 +663,7 @@ void OnModbusReceive2Exceptions(byte exCode, struct ModbusApHeader mbap)
|
|||
void ModbusSend(byte buffer[], word length, word TxID)
|
||||
{
|
||||
struct QueueElement qe;
|
||||
|
||||
qe.Length = length;
|
||||
memcpy(qe.Buffer, buffer, length);
|
||||
|
||||
|
@ -670,14 +671,12 @@ void ModbusSend(byte buffer[], word length, word TxID)
|
|||
|
||||
if (gQueuePending.Size() == 1 && gQueueSent.Size() == 0) // start timer at beginning
|
||||
setTimerCyclic(gtRobin, 1);
|
||||
|
||||
}
|
||||
|
||||
/// <-ModbusSend>
|
||||
on timer gtRobin
|
||||
{
|
||||
struct ModbusApHeader mbap;
|
||||
struct QueueElement qe;
|
||||
enum ModbusRequestError reqError;
|
||||
|
||||
//writeLineEx(0, 1, "Queue Sent: %d, Queue Pending: %d, Queue Ack: %d", gQueueSent.Size(), gQueuePending.Size(), gQueueAck.Size());
|
||||
|
@ -700,7 +699,7 @@ on timer gtRobin
|
|||
reqError = FinalTimeout;
|
||||
}
|
||||
|
||||
memcpy_n2h(mbap, qe.Buffer);
|
||||
memcpy_n2h(mbap, gQueueSent[TxID].Buffer);
|
||||
switch(mbap.FuncCode) // throw an error in each case
|
||||
{
|
||||
case ReadBits1:
|
||||
|
@ -741,6 +740,7 @@ on timer gtRobin
|
|||
if (gQueueSent.Size() > 4) // Wago 750-881 cannot handle more than 5 messages at a time :(
|
||||
continue;
|
||||
|
||||
|
||||
ModbusSnd(gQueuePending[TxID].Buffer, gQueuePending[TxID].Length); // send packet
|
||||
|
||||
memcpy(gQueueSent[TxID], gQueuePending[TxID]); // move packet to sent queue
|
||||
|
|
|
@ -11,6 +11,11 @@ void ModbusConnectTo(char Remote_IP[], word Remote_Port)
|
|||
TcpConnectTo(Remote_IP, Remote_Port);
|
||||
}
|
||||
|
||||
void ModbusConnectTo(dword Remote_IP, word Remote_Port)
|
||||
{
|
||||
TcpConnectTo(Remote_IP, Remote_Port);
|
||||
}
|
||||
|
||||
void ModbusSnd(byte buffer[], word length)
|
||||
{
|
||||
TcpSnd(buffer, length);
|
||||
|
|
|
@ -11,6 +11,11 @@ void ModbusConnectTo(char Remote_IP[], word Remote_Port)
|
|||
UdpConnectTo(Remote_IP, Remote_Port);
|
||||
}
|
||||
|
||||
void ModbusConnectTo(dword Remote_IP, word Remote_Port)
|
||||
{
|
||||
UdpConnectTo(Remote_IP, Remote_Port);
|
||||
}
|
||||
|
||||
void ModbusSnd(byte buffer[], word length)
|
||||
{
|
||||
UdpSnd(buffer, length);
|
||||
|
|
|
@ -18,85 +18,3 @@ variables
|
|||
byte gRxBuffer[8192];
|
||||
}
|
||||
|
||||
// Not needed
|
||||
dword SetupIp(char Local_IP[])
|
||||
{
|
||||
int adapterIndex;
|
||||
const int size = 512;
|
||||
char text[size] = "";
|
||||
dword addresses[8];
|
||||
dword address;
|
||||
word adapterCount;
|
||||
word i;
|
||||
long error;
|
||||
|
||||
adapterCount = IpGetAdapterCount();
|
||||
adapterIndex = @sysvar::Config::TcpIp::AdapterIndex;
|
||||
|
||||
switch (adapterCount)
|
||||
{
|
||||
case 0:
|
||||
writeLineEx(0, 3, "<%NODE_NAME%> Error: There is no network interface available!");
|
||||
stop();
|
||||
return INVALID_IP;
|
||||
break;
|
||||
case 1:
|
||||
writeLineEx(0, 1, "<%NODE_NAME%> Info: There is 1 network interface available!");
|
||||
if (adapterIndex != 1)
|
||||
{
|
||||
writeLineEx(0, 3, "<%NODE_NAME%> Error: You have not selected the first adapter!");
|
||||
stop();
|
||||
return INVALID_IP;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
writeLineEx(0, 1, "<%NODE_NAME%> Info: There are %d network interfaces available!", adapterCount);
|
||||
// // // // TEST \\ \\ \\ \\
|
||||
for (i = 1; i <= adapterCount; i++)
|
||||
{
|
||||
IpGetAdapterDescription(i, text, size);
|
||||
writeLineEx(0, 1, "<%NODE_NAME%> Info: Interface %d: %s", i, text);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
error = IpGetAdapterAddress(adapterIndex, addresses, elCount(addresses));
|
||||
if (error == 8)
|
||||
{
|
||||
writeLineEx(0, 3, "<%NODE_NAME%> Error 8: Could not retrieve IP address from interface #%d: The address array was insufficient", adapterIndex);
|
||||
stop();
|
||||
return INVALID_IP;
|
||||
}
|
||||
else if (error == 87)
|
||||
{
|
||||
writeLineEx(0, 3, "<%NODE_NAME%> Error 87: Could not retrieve IP address from interface #%d: The specified interface index was invalid", adapterIndex);
|
||||
stop();
|
||||
return INVALID_IP;
|
||||
}
|
||||
|
||||
address = addresses[0]; // the interface used
|
||||
|
||||
if (address == INVALID_IP)
|
||||
{
|
||||
writeLineEx(0, 3, "<%NODE_NAME%> Error: IP address to be used is invalid!");
|
||||
stop();
|
||||
return INVALID_IP;
|
||||
}
|
||||
|
||||
IpGetAdapterDescription(adapterIndex, text, size);
|
||||
writeLineEx(0, 1, "<%NODE_NAME%> Interface: %s", text);
|
||||
writeLineEx(0, 1, "<%NODE_NAME%> Wrong interface? Change sysvar::TCPIP::AdapterIndex");
|
||||
|
||||
IpGetAdapterAddressAsString(adapterIndex, text, size);
|
||||
writeLineEx(0, 1, "<%NODE_NAME%> Ip address: %s", text);
|
||||
strncpy(Local_IP, text, 16);
|
||||
|
||||
IpGetAdapterMaskAsString(adapterIndex, text, size);
|
||||
writeLineEx(0, 1, "<%NODE_NAME%> Subnet mask: %s", text);
|
||||
|
||||
IpGetAdapterGatewayAsString(adapterIndex, text, size);
|
||||
writeLineEx(0, 1, "<%NODE_NAME%> Gateway address: %s", text);
|
||||
|
||||
return address;
|
||||
}
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue