VB with PLC S7-300 communication

Join Date
Mar 2017
Location
6 October
Posts
69
Hello ,

I have a machine with panel PC ( OS : WIN98 ) and Siemens S7-300 , communicating through serial port and the panel PC has a program to control and read data from the machine ( i think it's written with VB ) .

I can select how they communicate ( MPI form my case ) .

Can someone guide me , what should i search for to make the communication through Ethernet in a new simple program ( PLC has CP ) ?
 
Snap7 is an awesome free library that i can recommend!

I appreciate your answer but i used S7.net

it really helped me to do good project ( connect and read data ) as the PC show it , but when i try to write data to PLC using the new project i got wrong numbers

i try to use textbox to write 15 in DB1.DBW0 ( machine speed set point , predefined as integer - which i read is 11 ) the plc accept the number but i get 12459 ( some thing like that ) not 15 and the PC panel get the same wrong number.

i try to write it with S7.net conversion it works perfect but i can not enter the speed from textbox i need to predefined it in the program

the code which give wrong number is
Code:
private void button3_Click(object sender, EventArgs e)
        {
            string address = textBox5.Text;
            object setpoint = textBox7.Text;
            plc.Write(address, setpoint);
        }
the working code
Code:
private void button3_Click(object sender, EventArgs e)
        {
            string address = textBox5.Text;
            ushort setpoint = 15 ;
            plc.Write(address, setpoint);
        }

is there a way to make ushort get value from textbox ( it's not possible for me ) ?
 
Last edited:
Try to convert the text from the textbox before you write. I am guessing that the library tries to write the value as a string.


Are you writing to a Word or a DWord?
For a Word something like this might work


Code:
UInt16 valueToWrite =  UInt16.Parse(setpoint);
plc.Write(address, valueToWrite );
 
Last edited:
Try to convert the text from the textbox before you write. I am guessing that the library tries to write the value as a string.


Are you writing to a Word or a DWord?
For a Word something like this might work


Code:
UInt16 valueToWrite =  UInt16.Parse(setpoint);
plc.Write(address, valueToWrite );

The library tries to write value throw it's own conversion ( C# and S7 Plc )

when i try to write using first code , it works great on PLC simulator
when i try to go online with PLC and write same data i got the wrong numbers ( which you describe why it will be wrong in the post you change o_O )
so i go for the manual of the library and found a conversion page
Code:
Value conversion between C# and S7 plc 
- Read S7 Word: 
ushort result = (ushort)plc.Read("DB1.DBW0"); 
[COLOR="Red"]- Write S7 Word: 
ushort val = 40000; 
plc.Write("DB1.DBW0", val); [/COLOR]

- Read S7 Int / Dec, you need to use the method ConvertToShort(): 
short result = ((ushort)plc.Read("DB1.DBW0")).ConvertToShort(); 
- Write S7 Int / Dec, you need to use the method ConvertToUshort(): 
short value = -100; 
plc.Write("DB1.DBW0", value.ConvertToUshort()); 

- Read S7 DWord: 
uint result = (uint)plc.Read("DB1.DBD40"); 
- Write S7 DWord: 
uint val = 1000; 
plc.Write("DB1.DBD40", val); 

- Read S7 Dint, you need to use ConvertToInt(): 
int result2 = ((uint)plc.Read("DB1.DBD60")).ConvertToInt(); 
- Write S7 Dint: 
int value = -60000;
 plc.Write("DB1.DBD60", value); 

- Read S7 Real, you need to use ConvertToDouble(): 
double result = ((uint)plc.Read("DB1.DBD40")).ConvertToDouble(); 
- Write S7 Real, you need to use ConvertToInt(): 
double val = 35.687; 
plc.Write("DB1.DBD40", val.ConvertToUInt());

- Read bool from byte 
byte myByte = 5; // 0000 0101 
myByte.SelectBit(0) // true 
myByte.SelectBit(1) // false

as i am trying to write to a word (predefined in the DB as int) , i think of using word conversion but the value is predefined as you can see in all the write codes

i just want to put the value from a textbox or any other entry that works with ushort

hope i describe my problem in a good way :D
 
Last edited:
Siemens 300 is big endian your pc is little endian this needs to be handled somewhere in the library. I have never used the library that you are using so i don't know how this is handled in your case



For a 16 bit word Value 12 byte swapped equals 3072. Is this the value you get in the plc when you try to write 12 or do you get another value? If you get 3072 try to swap the bytes before you write the value





Code:
public ushort SwapBytes(ushort x) {
     return (ushort)((ushort)((x & 0xff) << 8) | ((x >> 8) & 0xff)); 

}
 
Last edited:
Siemens 300 is big endian your pc is little endian this needs to be handled somewhere in the library. I have never used the library that you are using so i don't know how this is handled in your case



For a 16 bit word Value 12 byte swapped equals 3072. Is this the value you get in the plc when you try to write 12 or do you get another value? If you get 3072 try to swap the bytes before you write the value





Code:
public ushort SwapBytes(ushort x) {
     return (ushort)((ushort)((x & 0xff) << 8) | ((x >> 8) & 0xff)); 

}

thanks again for the help .

the PLC and PC show the 12 to 12594 :sick:

but i manage to change it throw
Code:
            string address = textBox11.Text;
            object setpoint = Convert.ToInt16(textBox12.Text);
            plc.Write(address, setpoint);
 

Similar Topics

Dear Experts, One of my client using S7-300 Plc In that Cp343 Module also using for scada and hmi connection ...
Replies
10
Views
6,580
Hi there, I have a Lenze C300, P300 & Top Line 8400. I have a basic application which only requires the drive to follow an external encoder...
Replies
0
Views
2,388
hello dear we have a problem with communicate with delta dop hmi and siemens s7-300 cpu . we use ISO ON TCP connection and AG-SEND and AG-RECV...
Replies
2
Views
3,452
Hı guys.I want to communicate S7-300 profinet plc with Control Tech.Emerson Unidrive M200 model inverter by Ethernet.I looked for any example...
Replies
1
Views
1,858
Dear All As I mentioned in the subject I want to know what is required on the PCS 7 both hardware and software aspects also for S7 300/S7 1200...
Replies
0
Views
2,891
Back
Top Bottom