Home AMX User Forum NetLinx Studio

Controlling RS232

When sending strings to a RS232 port to a device, should I wait until the device respond's !
How should I program it ?

How can control the incoming responds on a Rs232 port, example take a byte and evaluate it on bit level

Comments

  • viningvining Posts: 4,368
    The answer would be yes, no, sometimes and always! In other word it depends on the device and what your doing with it. Whith out details there's no way to really answer your question but as a generally rule you send a command and then respond to its reply. That response could be as simple as feebback to the TP or as complex as it needs to be for the desired task.
  • DHawthorneDHawthorne Posts: 4,584
    This varies very much with the device you are controlling. Some actually require you wait for a response, but in most cases you can get away with sending the commands out blindly. Typically, I buffer my commands and wait for a response, unless the device never responds at all. That way I can be reasonably sure it will act on the command. But even a valid response is not a guarantee the device is actually ready. Especially with things like projectors and receivers, you often have to add a considerable delay after first turning the unit on, for example. Some TV's and projectors require an additional delay when changing inputs before you do anything else (like adjusting aspect ratio, etc.). It's something of an art form, writing an RS-232 control block - very often the protocol documents are unclear or incomplete. Basically, you have to make your best guess, then see how it works.
  • joelwjoelw Posts: 175
    pvs wrote:
    When sending strings to a RS232 port to a device, should I wait until the device respond's !
    How should I program it ?

    Most devices fall under a specific category of how to handle. So is the data bursty, does it have a end of packet delimiter, does it send ascii with control codes, etc., etc. The short answer is it all depends on the device you are attempting to communicate with. I've been toying with writing a book on the concept.
    pvs wrote:
    How can control the incoming responds on a Rs232 port, example take a byte and evaluate it on bit level

    Well it comes in as a string (array of chars) and you have to act upon the string.
    There are certain functions used to manipulate the data.

    If you want to extract bit information from a byte, then bitwise operators are your friend.


    This is just a simple example to give you the flavor of what's involved. From a driver I recently wrote:
    DEFINE_FUNCTION onBinaryData (CHAR s[])
    {
        LOCAL_VAR INTEGER i, len
        LOCAL_VAR tmp[50]
        
        len = LENGTH_STRING(s)
    
        IF(len = 6)	{ // lens msg
    
    	IF(RAMP) { 
    	    sendLensCmd( LENS_CMD[RAMP] )
    	}
    	
        } ELSE IF(len = 10) { // fault msg
        
    	CLEAR_BUFFER tmp
    	for( i=1; i<=len; i++ )
    	    tmp = "tmp,itohex(mid_string(s,i,1)),' '"
    	SEND_STRING 0,"'SELF DIAGNOSIS [ ',tmp,']'"
    
        } ELSE {
    
    	CLEAR_BUFFER tmp
    	for( i=1; i<=len; i++ )
    	    tmp = "tmp,itohex(mid_string(s,i,1)),' '"
    	SEND_STRING 0,"'UNKNOWN BINARY MSG [ ',tmp,']'"
        }
    }
    
    DATA_EVENT[dvDev]
    {
    
        STRING:
        {
    	LOCAL_VAR CHAR cmd[1]
    	LOCAL_VAR INTEGER pkt_len, offset, len, start, end
    	
    	BUFF = "DATA.TEXT"
    
    	pkt_len = LENGTH_STRING(BUFF)
    	offset = 1
    
    	while(offset < pkt_len)
    	{
    	    start = FIND_STRING( BUFF, "STX", offset )
        
    	    IF(start)
    	    {
    		IF( MID_STRING( BUFF, start+1, 1 ) >= '0' )	// ASCII Packet
    		{
    		    end   = FIND_STRING( BUFF, "ETX", start )
    		    onAsciiData( MID_STRING( BUFF, start+1, end-(start+1) ) )
    		    offset = end + 1
    		}
    		ELSE						// Binary Packet
    		{
    		    cmd = mid_string( BUFF, start+2, 1 )
    		    len = getBinaryLen( cmd[1] )
    		    onBinaryData( MID_STRING( BUFF, start+1, len-2 ) )
    		    offset = start + len
    		}
    	    }
    	}
        }
    }
    

    Good luck my friend!
Sign In or Register to comment.