serial communication between Siemens PLC and Arduino

milan12

Member
Join Date
Sep 2015
Location
eindhoven
Posts
7
Hello everyone,

I want to make a serial communication using Siemens S7-1200 with CM 1241 (RS-232) and communicate with my Arduino.

Here is the setup of the communication, I have 2 temperature sensor and one Led connected to my Arduino and on the PLC side I have S7-1200 from Siemens and CM-1241. Arduino and my PLC are connected just by using Tx and Rx pin no handshake is done.

I am sending the temperature data from both the sensor to the PLC and on the PLC side i decide when to turn on the Led connected to my arduino depending on the different temperature values. I have assigning a ID to both the sensors before sending out the data. This is how the transmitted data from Arduino looks like $AOPT_TEMP1_20_TEMP2_21 <CR><LR>.

I have a filter on the PLC which only accepts the serial data starting with '$AOPT_', so for its good. I am receiving the serial data using RCV_PTP block and sending data using SEND_PTP. I can send signal from PLC to turn ON or OFF the led but what I want to do is evaluate the received temperature value from two temperature sensor TEMP1 & TEMP2 and then decide to control the Led. For instance if (TEMP1>TEMP2 ) then turn ON the Led else turn OFF.

I am able to receive the data on the PLC from Arduino but now I don't know how to proceed with comparing the received information. How do i extract the only required data from the received buffer? Any suggestions will be highly appreciated.

Thanks in advance
 
Hi Milan and welcome!

Wouldn't you know how to deconstruct whatever you are getting since you are sending it yourself?

I can't really help you here, but am interested so please keep us informed :).
 
Hi Milan and welcome!

Wouldn't you know how to deconstruct whatever you are getting since you are sending it yourself?

I can't really help you here, but am interested so please keep us informed :).
Hello boneless, Thank you very much :)

Yes true, since I am sending the data from arduino i know whats going in the PLC and even change it if necessary but the problem is that I am receiving each character in my PLC's buffer (RCV_PTP) and from there I don't know how to proceed further. Since I am new to PLC i do not know how to handle those received characters and how to filter out the required data in order to take a proper action, in my case depending on the temperature value Led should be ON or OFF.
 
Use Modbus. There are several free Arduino/Modbus projects out there. Then use the Siemens as a Modbus master.

Good luck,

Yosi
 
Use Modbus. There are several free Arduino/Modbus projects out there. Then use the Siemens as a Modbus master.

Good luck,

Yosi
Thanks Yosi for the suggestion but I cannot go to Modbus, Currently I am just using a Arduino to test my PLC program but in the reality there will be other system sending out the serial data. I am just doing the PLC part, in the mean time I am just trying to simulate the serial data to see weather the PLC work as it should.

Any idea on how I can solve my problem with the current system?
 
The only real question I've got is whether the data is always located in the same position or if you have to calculate the position.

The string is '$AOPT_TEMP1_20_TEMP2_21 <CR><LR>'

If the temperatures are always 2 characters long and at the same position then you would simply extract them using FIND to find the strings "TEMP1_" and "TEMP2_" in the input string and then using MID to extract the characters in question. You would then feed the result string into STRING_TO_INT to convert the results to an integer.

If the temperatures are of varying character lengths then you'd use FIND to find those same strings but use MID to extract one character at a time for as long as those characters are numeric and then use the STRING_TO_INT to convert the extracted strings.

Mind you this is a very simplified explanation. I would do some validation on the input string to test for length and to insure that both the TEMPx strings appear in it and, of course I'd validate the strings before conversion to insure that they consist of only numeric characters but you get the point.

Good luck,

Yosi
 
The only real question I've got is whether the data is always located in the same position or if you have to calculate the position.

The string is '$AOPT_TEMP1_20_TEMP2_21 <CR><LR>'

If the temperatures are always 2 characters long and at the same position then you would simply extract them using FIND to find the strings "TEMP1_" and "TEMP2_" in the input string and then using MID to extract the characters in question. You would then feed the result string into STRING_TO_INT to convert the results to an integer.

If the temperatures are of varying character lengths then you'd use FIND to find those same strings but use MID to extract one character at a time for as long as those characters are numeric and then use the STRING_TO_INT to convert the extracted strings.

Mind you this is a very simplified explanation. I would do some validation on the input string to test for length and to insure that both the TEMPx strings appear in it and, of course I'd validate the strings before conversion to insure that they consist of only numeric characters but you get the point.

Good luck,

Yosi
Hello Yosi,

Thank you for your answer. I will try to do this but I have one question before I use the FIND block dont I have to convert the characters received in my buffer to string? Can I directly use FIND in the received buffer?
 
I would suspect the receive buffer to be a string. If the data is byte rather than string you should use memcopy or the equivalent. Sorry I can't be of more help. The software isn't in front of me...

Good Luck,

Yosi
 
I would suspect the receive buffer to be a string. If the data is byte rather than string you should use memcopy or the equivalent. Sorry I can't be of more help. The software isn't in front of me...

Good Luck,

Yosi
Thanks for the suggestion Yosi. I managed to got this working. I can now extract the values from my received buffer. This is what I did,
- Created a new static data buffer in the project and filled (Fill_BLK) it with a empty characters
- Moved (MOVE_BLK) my received characters from RCV_PTP to my static buffer
- converted (Char to string) the characters in my static buffer to string
- Used FIND block to find the string from the string (I used TEMP1_) which was created in the above step. This gave me the start position (from the above string it gave me 7) of the string.
- I then used MID function to abstract the value of the TEMP1_, giving the start position as 13 and string length as 2.
- finally then I converted the abstracted value 20 from string to INT, is it necessary? this will result the same value, so is this really important?

Now, I am wondering how do I abstract the temperature value if the length is unknown? For temperature it will not be greater than 2 but if other sensors are connected and the output can vary from length 1 to 5 then I do I do this? Any suggestion for this?
 
I would think that you could just go ahead and use find on all 5 temperatures - looking for TEMP1_ then TEMP2_ all the way to TEMP5_ - if it returns 0 then it didn't find the substring anywhere and you don't try and process the temperature reading.
 
I would think that you could just go ahead and use find on all 5 temperatures - looking for TEMP1_ then TEMP2_ all the way to TEMP5_ - if it returns 0 then it didn't find the substring anywhere and you don't try and process the temperature reading.
danatomega, thanks for your reply I am able to find the sub-string using FIND block but the problem is what if my temperature returns more or less than 2 characters? Value of the temperature is not know, so in this case I just cannot use MID block to extract the data. I have already checked this and its fails to extract the correct value when the temperatures returns 3 characters.

Any suggestions how I can solve this?
 
I don't know all the capabilities of the siemens cpu but it looks as though it's at least similar to computer programming languages.

So, the method I would use is to use FIND on your TEMP1_ in addition to FIND on your TEMP2_ ... the difference (subtracting the 6 characters for the 'TEMPx_' substring) equals the length of the actual temperature reading. You may need to tweak this a bit to get the correct results.

Do this for each TEMPx_ that you're looking for excepting at the last one, where the length of your temperature reading would be roughly the length of the original string minus the location of your last FIND result.
 
I don't know all the capabilities of the siemens cpu but it looks as though it's at least similar to computer programming languages.

So, the method I would use is to use FIND on your TEMP1_ in addition to FIND on your TEMP2_ ... the difference (subtracting the 6 characters for the 'TEMPx_' substring) equals the length of the actual temperature reading. You may need to tweak this a bit to get the correct results.

Do this for each TEMPx_ that you're looking for excepting at the last one, where the length of your temperature reading would be roughly the length of the original string minus the location of your last FIND result.
danatomega, cool now it works fine, its calculates itself :D thanks for the suggestion.
 

Similar Topics

I am working to control some objects using Weintek HMI through Fatek PLC. I wrote a simple program to control an output Y_0 using input X_0. I...
Replies
3
Views
3,745
Hi Experts. I need your help, I wanna communicate a MIcro850 with L19ER CompactLogix through Serial communication (Modbus RTU). I am using...
Replies
2
Views
2,123
problem in communication between micrologix 1200 & Panel View 600 Plus by serial DF1 Hello Friends i have to do communication between micrologix...
Replies
6
Views
6,569
Hello. I want to establish serial communication between the S7-300 and a raspberry pi. Toward that end, I'd like to begin by sending a simple 1 or...
Replies
10
Views
7,235
Hi everyone, I'm trying to go online with an IFM PLC over a serial connection, using CoDeSys. Things were working fine for a while; I managed to...
Replies
7
Views
11,380
Back
Top Bottom