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
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
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.
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!