Redlion G3 To Serial Scale RS-232 Comms

shiftedbits

Member
Join Date
May 2007
Location
CALIFORNIA
Posts
62
Greetings All,

Does anyone have written a program for G3's or can share some wisdom on how to talk Serially and get data from an ASCII device in to a G3? I would like to use the G3 to accomplish this. The manual is pretty vague. For starters, I would like to poll the external devce once a second. I only need to send a "SINGLE" ASCII character out as a command, and the device responds back with 5 different fields with ecah field representing 5 bytes of data that I need to split and distribute to 5 different tags.

How do you detect com errors? Can this be done via programming within the G3.

Just looking to find a simple example or guidance on how to go about this.

Thanks.
 
Use a portPrint to send out the data. Then there is a portRead (I think thy is what it is called). Look at the C3 manual there are 2 different manuals. One for usable instructions and one for format. There are a few pages towards the end of the format manual that are golden. They tell you how to do a lot of common functions(loops, switches, etc.)
The instruction manual tells you how to format an instruction and what the response should be.
Don't have my PC fired up so I can't help with an example yet. Maybe later.
 
I PM'd the OP last week with some tips. I'll post that message here for the benefit of the group.
---

shiftedbits said:
Hello Kolyur,

Sorry to barge in,. This is in regard to your reply to the Post on serial Comms. You stated that you have done fe Redlion G3 comms using the Serial port. I would verymuch appreciate if you could share some samples. I have an up coming project where I would like to talk to a scale and get data from it (ASCII Fromat) rather than use the PLC to do it.

My requirments pretty simple. I send a command to the Device asking for data, then it replies back with several pieces of information. (i.e. Weight, Units, Tare weight etc.) I would also like to know how to do string parsing etc. on this G3. Comms will e continuous polling at around 1 sec. intervals. This is my first time around with a G3 and to it seems their manuals are somewhat lacking.

Your help would be gratly Appreciated.

Best-
Hey shifted, sorry for the delay in my reply. I can tell you that ASCII serial comms on the G3 are very solid but there are a few caveats. I can't provide my entire project file but I can post the scripts that are handling the serial communications. (I'm using Crimson 2 but C3 should be the same in this regard.)

I am communicating with a USB data logger. The following code sends a command to check if a USB drive is mounted, and waits for the response. This mount check has to be done continually, so this program is triggered from the universal "On Tick" action which causes it to run once every second. (Sounds like you have a similar requirement.)

Code:
int status;
cstring response;

USBmountAction := 1;
while(PortRead(4, 0) > -1) {}
PortPrint(4,"J\r");
response := PortInput(4,0,0,750,12);

if(response == ""){ USBmountCode := 0xEE; return; }

USBmountCode := TextToInt(Mid(response, 9, 2), 16);
if(USBmountCode > 0) return;

status := TextToInt(Mid(response, 5, 2), 16);

if(USBpresent && status==0){
    PlayRTTTL("Sound:d=4,o=5,b=500:c,c,c");
    USBpresent := false; }

if(!USBpresent && status>0)
{
    USBmountAction := 2;
    while(PortRead(4, 0) > -1) {}
    PortPrint(4,"U\r");
    response := PortInput(4,0,0,750,4);

    if(response == ""){ USBmountCode := 0xEE; return; }

    USBmountCode := TextToInt(Mid(response, 1, 2), 16);
    if(USBmountCode > 0) return;
    
    PlayRTTTL("Sound:d=4,o=5,b=500:c,c,c");
    USBpresent := true;
}

USBmountAction := 0xF;
USBmountCode := 0;
Some things to note:
1. The comms are handled mainly with the PortInput and PortPrint commands. Forget PortRead and PortWrite with one exception, below.
2. The "4" argument in the serial commands is the port number. After you configure your port for raw serial in the Commmunications window, the port number will be shown in the status bar at the bottom of the screen. (At least, that's how it works in C2.)
3. The "while" statement at the beginning is very important. What I discovered is that once a port is configured for raw serial, it has a buffer that continually looks for data on the port, even if you aren't using it. As a result, if you intend to send a command and wait for a response, the response data will be queued behind whatever junk happens to be in the buffer already. The while statement clears out the buffer before sending a command. (Thanks to Red Lion tech support for that.)


Here's another program from the same project:
Code:
int TS := ProfileTimeStamp[ProfileIndex];
int hourciv,change,i,line;
cstring ampm,datestr,response;

// DETERMINE 12-HOUR TIME VALUE
if(GetHour(TS) <= 12){
    if (GetHour(TS) == 0) hourciv:=12;
    else hourciv := GetHour(TS);
}
else hourciv := GetHour(TS) - 12;

if(GetHour(TS) < 12) ampm := "am";
else ampm := "pm";

// FORM TIME/DATE STRING
datestr := IntToText(GetMonth(TS),10,2);
datestr += "/";
datestr += IntToText(GetDate(TS),10,2);
datestr += "/";
datestr += IntToText(GetYear(TS),10,2);
datestr += " ";
datestr += IntToText(hourciv,10,2);
datestr += ":";
datestr += IntToText(GetMin(TS),10,2);
datestr += ampm;

// FORM ARRAY OF LINES FOR WRITING TO FILE
USBwriteArray[0] := "File: " + IntToText(ProfileIndex,10,3) + "\r\n";
USBwriteArray[1] := "Description: " + ProfileDesc[ProfileIndex] + "\r\n";
USBwriteArray[2] := "Last run: " + datestr + "\r\n";
USBwriteArray[3] := "\r\n";
USBwriteArray[4] := "Inches\tRaise\tLower\tChange\r\n";

for(i:=0; i<=19; i++){
    line := i + 5;
    change := CurrentRaiseForceArray[i] - CurrentLowerForceArray[i];
    USBwriteArray[line] := DecToText((i+1)*250,0,1,3,1,0) + "\t";
    USBwriteArray[line] += IntToText(CurrentRaiseForceArray[i],10,4) + "\t";
    USBwriteArray[line] += IntToText(CurrentLowerForceArray[i],10,4) + "\t";
    USBwriteArray[line] += DecToText(change,2,3,0,1,0) + "\r\n";
}

// CREATE & OPEN FILE FOR WRITING (HANDLE=1)
USBwriteAction := 1;
while(PortRead(4, 0) > -1) {}
PortPrint(4,"O 1W>" + MakeFilename() + "\r");
response := PortInput(4,0,0,1000,4);
if(response == ""){ USBwriteCode := 0xFF; return; }
USBwriteCode := TextToInt(Mid(response, 1, 2), 16);
if(USBwriteCode > 0) return;

// WRITE DATA TO FILE
for(i:=0; i<=24; i++){
    USBwriteAction := 0x10 + i;
    while(PortRead(4, 0) > -1) {}
    PortPrint(4,"W 1>" + IntToText(Len(USBwriteArray[i]),16,2) + "\r");
    response := PortInput(4,0,0,1000,4);
    if(response == ""){ USBwriteCode := 0xFF; return; }
    USBwriteCode := TextToInt(Mid(response, 1, 2), 16);
    if(USBwriteCode > 0) return;

    USBwriteAction := 0x30 + i;
    while(PortRead(4, 0) > -1) {}
    PortPrint(4,USBwriteArray[i]);
    response := PortInput(4,0,0,1000,14);
    if(response == ""){ USBwriteCode := 0xFF; return; }
    USBwriteCode := TextToInt(Mid(response, 11, 2), 16);
    if(USBwriteCode > 0) return;
}

// CLOSE FILE
USBwriteAction := 2;
while(PortRead(4, 0) > -1) {}
PortPrint(4,"C 1\r");
response := PortInput(4,0,0,1000,4);
if(response == ""){ USBwriteCode := 0xFF; return; }
USBwriteCode := TextToInt(Mid(response, 1, 2), 16);
if(USBwriteCode > 0) return;

USBwriteAction := 0xFF;
This one is triggered from a button press. It sends a series of commands to create a file on the USB drive, write data to it, then close the file. Much of the program consists of string commands to compile the data to be written (from variables set elsewhere in the system).

Another important point: When you specify a Length parameter in the PortInput command, you have to receive at least that many characters or it will return nothing. For example, if your Length is 10 but you only receive 9 characters, the function will return nothing (not the received 9 as you may expect). This tripped me up for awhile.

Concerning string parsing, the main commands you will use are Mid(), Left(), Right(), and Len(). There are also commands to convert numbers to text and vice versa. All of these commands are fully documented in the manual.
 
kolyur,

Wow!

Very much appreciate the details and you taking the time to post it. I can learn a lot from this. I was running late on the project I was working on and had to get it done using a serial Gateway (from RTA Automation). But definitely will try and decipher this so I have it for next time around.


Now I have to tend to another complain from the end user about lack of a vertical scale on the Trend Viewer (hence a another post to follow soon)

AGAIN, THANK YOU VERY MUCH!
 

Similar Topics

I lost my ethernet port on my l32e compactlogix how do i set up the serieal port to a redlion to switch my comms back to ip
Replies
3
Views
1,487
Good morning and new to the forum. Attempting to configure a RedLion CU30D using it's RS-232 port to log the input stream. "Raw" ASCII data is...
Replies
4
Views
1,648
Hey guys, I'm working on an existing system for a client where a GE RX3i at a tank/pump station is sending data to their office HMI through a...
Replies
1
Views
3,837
Working on a project where I need to set the voltage, current and read the status on some power supply's. There are 5 power supply's. Each has a...
Replies
8
Views
3,140
HI fella's, Would be great to get a few things fixed this end, if I could get some help. Using Crimson 2 - 521B on the PC, and release build...
Replies
1
Views
4,000
Back
Top Bottom