Beijer Electronics (formerly QSI Corporation)

Manufacturer of Mobile Data and Human Machine Interface Terminals.
It is currently Mon Nov 20, 2017 2:19 am

All times are UTC - 7 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Fri May 28, 2010 7:05 am 
Offline

Joined: Fri May 28, 2010 6:35 am
Posts: 22
I have some industrial devices on RS-232 or RS-422 which use my own protocol. I need to send a serial command and get a response that can include a structure of various values. Each command and response has a 16 bit CRC.

When get the data from a command I would like to display that on the terminal, often with some units scaling, and be able to edit the values before sending the command to write it back to my device.

Can I do this with Qlarity?

If I can, this would be perfect for me as my customers are wary of using a PDA device for security reasons (networking and USB and memory chip sockets).

thanks,

jv


Top
 Profile  
 
PostPosted: Tue Jun 01, 2010 6:29 am 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
Yes, in fact the situation you describe is precisely why we created Qlarity.

You will need to write a little bit of Qlarity code to generate commands in the format you need, as well as some to parse the responses. Most of the rest is simply setting object properties. We also have a function that will calculate CRCs in various formats for you, as well as the ability to extract arbitrary big or little endian bytes from a stream and convert them to 8, 16 or 32 bit integers or 32 bit floating point values. You can also convert textual strings to numbers as well, depending on how you have your protocol set up.

You can also edit values and send them back to the device, although the techniques you will use will vary depending on whether you are interested in a keypad or touch screen Qlarity device.

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
PostPosted: Wed Jun 30, 2010 2:45 pm 
Offline

Joined: Fri May 28, 2010 6:35 am
Posts: 22
Excellent so I have a terminal and I am trying to figure out my polynomial but I didn't create it. This is code carried over for many years.

Any idea what parameters to use with the Qlarity function?

(C code for the CRC is below)


The next question is how to make an object to use my protocol. I created a new library and copied over the SerialProtocol functions to use as a starting point. I need to create a function that will take an array or bytes, a command and an address and then use that generate a new array that includes that information along with size of the data and the data and then a crc of the entire new array of bytes. I can then send this out to a COM port and get a similar packet in response.

Then I can check the crc on the incoming packet along with the size and response fields and ultimately return an array of bytes for the data.

The next step is another object that will create the array of bytes from a set of variables or a structure. In C this is a structure and i can just treat it as a chunk of memory. In python I have a dictionary of properties that I pack and unpack into the binary that gets transmitted.

In Qlarity I might do the same thing. Make a method for each command and have properties for each variable field in the structure. I will have to pack and unpack the structure each time.

Does that make sense or is there an easier way? Can I get a structure like in C and treat that as a chunk of memory or array of bytes by the lower level send and recieve functions?

Thanks.



C Code for my CRC creation:

CRC16_Init(void)
{
int i,j,k;

if (crc16rdy)
return;

for(i = 0; i < CRC16TBL_SIZE; i++) {
j = 0;
for(k = i; k != 0; k /= 2) {
j = (j + k) % 2;
}
crc16tbl[i] = (i << 7) ^ (i << 6) ^ (j * 0xC001);
}
crc16rdy = 1;
}


//
// CRC16_Byte
//
// Description:
// compute CRC-16 one byte at a time
//
// History:
//
// Return Value:
// accumulated new crc value
//

unsigned short CRC16_Byte(unsigned short byte, unsigned short crc)
{
return (crc16tbl[0xff & (byte ^ crc)] ^ (crc >> 8));
}

void CRC16_Add(unsigned short* str, unsigned long length)
{
str[length] = ~CRC16_Str(str, length,0);
}

int CRC16_Valid(unsigned short *str, unsigned long length)
{
return (CRC16_Str(str, length,0) == 0xB001);
}


Top
 Profile  
 
PostPosted: Wed Jun 30, 2010 3:09 pm 
Offline
QSI Support
QSI Support
User avatar

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 881
Location: Salt Lake City, Utah
Regarding your CRC question:
I am not sure what polynomial, etc. to use. While, I can use the Qlarity CRC functions, I am not deeply versed in CRC theory. I would suggest trying either the CRC-16/CITT or the CRC-16 sample values. I have not actually encountered a 16-bit CRC that was not one of those two types, with the CRC-16 being more common and the CITT version somewhat rare.

As far as taking a set of variables and converting them to their binary bytes equivilent and back, here is how I might approach it. I put this code in the Advanced Code section, although you could paste it into the Global code section and it will work too.

Code:
define object type BasicPacket
    'Create an object template with this contents:
    'Error checking omitted for brevity
    dim dataVal1 as integer
    dim dataVal2 as unibyte
    dim fltVal as float
    dim text as string

    func LoadFromBytes(data[] as byte)
        FromBytes(dataVal1, mid(data, 0, 4), true)
        FromBytes(dataVal2, mid(data, 4, 2), true)
        FromBytes(fltVal, mid(data, 6, 4), true)
        text = mid(data, 10, -1)
    endfunc

    func GetBytesFromData() returns byte[]
        return GetBytes(dataVal1, true) + GetBytes(dataVal2, true) + GetBytes(fltVal) + text
    endfunc
enddef

declare bpi as BasicPacket
enddec


To use it, call bpi.LoadFromBytes() on whatever data you have received from the serial port (after trimming off the command headers and CRC). For transmitting values, set the data members of bpi, then call bpi.GetBytesFromData (and then add the command headers and CRC).

I would create one of these object templates for every unique packet type you might receive.

_________________
Jeremy
http://www.beijerinc.com


Top
 Profile  
 
PostPosted: Mon Jul 12, 2010 11:18 am 
Offline

Joined: Fri May 28, 2010 6:35 am
Posts: 22
Ok, I have working code that can talk to my devices:

Code:
func SendCommand(cmd as byte, data[] as byte) returns byte

    dim size as Unibyte
    dim c as Unibyte

    ' format the output string
    '
    size = len(data) + 6
    senddata = GetBytes(address,FALSE) + GetBytes(cmd,FALSE) +  GetBytes(size,FALSE) + data
    c = calculatecrc(crc16,16, TRUE, 0, TRUE, 0, senddata)
    c = ! c
    senddata = senddata + GetBytes(c,FALSE)

    ' now send to the serial port
    '
    BasicSerial_1.senddata =  senddata

    ' get the response
    '
    BasicSerial_1.Buffer = nodata


    return 0

endfunc


I haven't worked out the receive logic yet. It looks like there is no threading? So my Receive function will have to be some kind of state machine in the receiver callback? Then once it gets a complete return packet it can then call a function registered when the command was sent. Does that sound reasonable?

And as far as threading, can I assume that any event callbacks won't be called while I am already handling an event?

Thanks,

jv


Top
 Profile  
 
PostPosted: Mon Jul 12, 2010 12:30 pm 
Offline
User avatar

Joined: Thu Mar 02, 2006 2:12 pm
Posts: 487
Location: Salt Lake City, Utah
Yes you are correct, no other event callbacks will occur while you are still executing code in an event. This is due to Qlarity's single threadedness.

This forum topic should help get you started with receiving data on the serial port.
viewtopic.php?f=4&t=128

_________________
Ron L.

http://www.beijerelectronicsinc.com/


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 7 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group