Serial communication with Siemens CP340 and LabVIEW

hprosjplc

Member
Join Date
Feb 2008
Location
Oslo
Posts
18
Hi!
We are a group of students located in norway, and i have a challenge communicating between a laptop and the Siemens CP340 RS232 module.

The challenge now is to establish a working com line between these two so i can flick a switch in labview and get a desired response from the PLC.
What i have done is load a already-existing default Siemens CP340 code into the PLC. I get two errors on FB2 and FB3: download(294:23) When i use terminal just to test, i get a reply "15" no matter what i send to the PLC.

Have anyone got an idea how to make this work?
I appreciate the help.
 
Last edited:
I do not know much about that PLC but I have done a LOT of RS232 stuff in LabView using the VISA tools, so I would be happy to help with that if that is the issue. Do you know which end the problem is on? the PLC or LabView?
 
Ah, thank you very much!

I never had a second thought about not beeing able to download FB2 and FB3 to the PLC. I followed your link, and downloaded a older example program. This worked, and now the errors is gone, and the CP340 sends to the computer "15 02 02 02 02 02 02 15 02 02....." For some reason cyclically. Now, this is at least a start. Iv`e tried to alter DB 4 to make the PLC send something else, but i can`t seem to make that work. Neither can i see any change in DB4 when i send to the PLC.

I have very little experience programming in STL, but ill` try to explain:
In the end, all this PLC will do is work as a "highway" between modbus and RS232. -But i will wait with this part for now.

Right now i want to call FB4 cyclic, and alter it according to the inputs of the PLC, transmit this trough RS232 to Labview, and enterpet this as boolean variables in the program. I will also have switches to control the outputs of the PLC.

Is there maybe an easyer way to do this gosenbach?

In advance, thank you very much! :nodi:
 
Another thing.. When i use terminal i can read the caracters from the PLC just fine, but when i use "serial read and write" in labview i only get squares and blanks.
 
It has been about a year since I worked with LabView so I am trying to remember well enough to describe it to you. I switched jobs and no longer have labview to take a screen shot with but I may have some backups of some examples at home.

The company I used to work for built machinery that was all microprocessor bassed (vs. PLC based) so any time we wanted to put a HMI on one, I had to write all the serial drivers from scratch. I found it best to use labview and then compile an app for a panel PC, worked great.

I had a standard "serial read block" that I copied/pasted into any program that I needed to do a serial read.

If you set up your program so you can get a second while loop running in parallel with your main program, that is where you want to put your serial reads. This way you can be always scanning the serial port even while you are doing things with the rest of your program and instantly react to any data coming in one the RS232 line.

Set up a while loop and pass your VISA setup info into it. Inside this wile loop put a case statement. This will have two cases, 0 and not 0. There is a command to look at the serial port and see how many characters are stacked up in your buffer serial buffer. (the buffer is pretty big, it good practice to make sure you clear the buffer at the start of the program to make sure any garbage is gone. It will mess up things every time.) Witeh this value tot he case statement so anytime there is data in the buffer (not 0) then you do your visa read. Read only one character at a time. This is important. Since you are in a while loop running parallel, this was go pretty quick. For each character you read, check it to make sure it is not the EOL character (whatever that my be, [CR] for example). If it is not EOL, add it to the end of a string. If it is the EOL, add it to the end of the string (if you need to) and break the loop, passing the completed string out and into the other section of your program where you can strip out the needed information. Remember to pass out the completed sting every time you get your [CR], then start over with a new string.

This should result into all your serial data being split up into nice command that are easy to work with. Remember also to clear the buffer at the beginning to the proram when you first open the VISA channel or you are going to be banging your head against the wall for days.

Hope this helps.

cheers.
 
Thank you for a great answer gosenbach! This will help greatly on the labview program! I will try tomorrow to make somethig like you described.

Do you have any suggestion why i only get squares when i perform a serial read when i get normal ascii characters in terminal?

But, the challenge now is to get the Siemens CPU314 with the CP340 serial module to transmit the correct information.. I can paste a piece of the program:
THIS IS FC1:
----------------network 1---------------
A "VKE 1 Merker"
S "Anstoß Senden"
A(
O "SEND_Done"
O "SEND_Error"
)
R "Anstoß Senden"
NOP 0
-------------network2--------------------
A "Anstoß Senden"
= L 0.0
BLD 103
CALL "P_SEND" , "InstanzDB3"
REQ :=L0.0
R :=
LADDR :=256
DB_NO :=4
DBB_NO:=0
LEN :=12
DONE :="SEND_Done"
ERROR :="SEND_Error"
STATUS:="SEND_STATUS"
NOP 0
-------------network3:--------------------
A "SEND_Error"
JNB _001
L "SEND_STATUS"
T "SEND_Statusspeicher"
_001: NOP 0
--------------network4:-------------------
A "VKE 1 Merker"
= L 0.0
BLD 103
CALL "P_RCV" , "InstanzDB2"
EN_R :=L0.0
R :=
LADDR :=256
DB_NO :=4
DBB_NO:=12
NDR :="RECV_NDR"
ERROR :="RECV_ERROR"
LEN :="RECV_LEN"
STATUS:="RECV_STATUS"
NOP 0
---------------network5:---------------
A "RECV_ERROR"
JNB _002
L "RECV_STATUS"
T "RECV_Statusspeicher"
_002: NOP 0
---------------------------------------

Excuse the german, this is an example program.
Those of you who are more experienced with serial comm and STL can see the send and transmit blocks wich are called from this FC1.
Now, from what i can understand the data to be transmitted is stored in DB4, and the last 6 lines of DB4 stores the recieved info. When i change the DB4 data block, the PLC still sends the same characters to the computer.. Any ideas?
 
Ok, now that i have read the manual one more time, i have got some ideas how to solve this matter. I have to send a confirmation from labview to make the PLC send the content of the registers. i get 0x02, then i should send 0x10 to get the info, and another 0x10 when the transmission is completed.
 
hprosjplc said:
Do you have any suggestion why i only get squares when i perform a serial read when i get normal ascii characters in terminal?

I do remember something like that but I am having a difficult time remembering exactly. I think any characters that are not 0-9, a-z, A-Z, just show up as squares, such as space, LF, CR, and other "non-printable" characters. You terminal interprets these characters correctly and outputs human readable printaable text. LV is just giving you a data string. it is up to you to format it. Try taking that data you are reciving and wirig it to one of the text displys on the control panel and see what comes up. It might give you a hint as to what those characters are.
 
Check out this post in the NI Forum. (Which is an Excelent forum if you need any help with LabView.)

There is a .vi example on there and I think it is the one that I based my serial read code off of. although I can not open it to confirm. If you want to open it and post a screen shot, I can tell you if it is what I think it is or not.

http://forums.ni.com/ni/board/message?board.id=170&message.id=82914#M82914
 
Ok, now i have established a good connection, and i recieve the correct message from the PLC. The challenge now is to put this serial information in a type of array or string like you said. How can i do this?

A simple way would be to count the number of chars while they are coming in, and put the char in an array, where the posistion in the array is chosen according to the character recieved posistion. So: the third character in is placed immideatly in the third array position. The problem is, i`m not able to count how many characters i have recieved! Any suggestions? I recieve 12 chars from the PLC in about 300ms.+/-
 
Lets see....
when you read your string, just put it in a string. As each character comes in, just add it to the end of the string until you get to your EOL/EOF character (useally a CR or a LFCR). At that point you can use the function (if I remember correctly) build array if you want it in an array (this will be an array of strings, not an array of characters.

What you were describing sounds difficult and un-necessary.

You can use varrious string functions to strip out the information you need from the string and convert it if you need to. I would thing that the data coming up fromt he PLC is all in a similar format. Two examples: If the data you are looking for is a comand, you can wire it into a case statement that executes the command, in which case you can just leave it as a string. if the data is a value, you can strip out any alpha characters, just leavig the numerics. You can then convert the string into a number or decimal.

What is the format of the 12 character string and what is the important data in it? If you can post an example of one of the strings, maybe I can be a little more specific..
 
My challenge is just this part of adding the characters to the end of the string. I cant seem to find a suitable block in LV to make this happen, although it should be a piece of cake. I reciece this sequence of data: 02 xx xx xx xx xx xx xx xx xx xx xx xx 10 03 xx
02 is the start, then 12 bytes of hex data, 10 and 03 are "transfer finished" and at the end a checksum. All of this is recieved in about 2/10 of a second, and i can`t find a way t store it because LV enterpets each of the characters as a string, and not the complete message as a string.
 
I`ve tried both of them with little sucess.. Concatinate string needs paralell information to build a string right? Ill try again with that one tomorrow. Build text i can`t get to function properly.. ill read some more about that VI and see what i can doo.

I used another methot to get almost the result i wanted:
On the serialport i used "bytes at port" and "replace array subset" with shift registers. When a character enters, it is placed in a string according to the value of "bytes at port". This works ok, but it seems to be some kind of timing problem.. The finished string misses two of the hex values i get in terminal, and some read operations fail, and some wrong values are transferred to the array.

Thanks very much again!
 

Similar Topics

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...
Replies
13
Views
9,696
Currently,we are facing the communication go through with 422 tx+ and tx-. Mitsubishi fx5u series is used and using command rs2. But the package...
Replies
0
Views
91
I have wasted a week trying to figure out how to connect an SLC5/03 with my laptop. I do not have and can not Buy the 1747 UIC and PC3 cables. I...
Replies
14
Views
2,477
MELSEC A RS422 (25pin) <> iQR C24 Serial RS232 (9pin) - Simple PLC Communication Communication from a MELSEC-A Series CPU (RS-422 – DB 25 Pin)...
Replies
4
Views
1,003
Hey guys, I have a problem reading weighing data from AND AD4406 with OP-05 RS-232C Relay and Control input using Rockwell PLC Micro820. I already...
Replies
0
Views
924
Back
Top Bottom