Page 1 of 1

How do I read rs232 and get correct byte into variable?

Posted: Mon Aug 06, 2012 7:08 pm
by markus747
Question 1
Let's say I want to send 5 bytes of data to control my cable reel. How do i ensure that i put correct byte into each variable?
Maybe first byte is always value 120?

Question 2
Also want to use a bit to tell on or off,and a bit to tell direction,and a bit to tell high gear,and a bit to tell if its low gear,
Can I write a decision to look at single bit?

Re: How do I read rs232 and get correct byte into variable?

Posted: Mon Aug 06, 2012 7:26 pm
by kersing
markus747 wrote:Question 1
Let's say I want to send 5 bytes of data to control my cable reel. How do i ensure that i put correct byte into each variable?
Maybe first byte is always value 120?
As you suggest sending a special first byte to signal the start of a sequence is a good way. Preferably a value that does not occur in the other bytes. Sending a checksum byte after the other bytes to ensure correct reception might be useful as well. (Simply add all bytes in a byte variable, ignoring overflow should work)
markus747 wrote: Question 2
Also want to use a bit to tell on or off,and a bit to tell direction,and a bit to tell high gear,and a bit to tell if its low gear,
Can I write a decision to look at single bit?
In the decision logic use something like

Code: Select all

( VAR & 0x10 ) = 0x10
to check if bit 5 is set. (0x01 for bit 1, 0x02 for bit 2, 0x04 for bit 3 etc)

Re: How do I read rs232 and get correct byte into variable?

Posted: Mon Aug 06, 2012 8:36 pm
by Enamul
Question 1
Let's say I want to send 5 bytes of data to control my cable reel. How do i ensure that i put correct byte into each variable?
Maybe first byte is always value 120?
Here is two versionsz of Tx for sending data to Rx and one of Rx...
Question 2
Also want to use a bit to tell on or off,and a bit to tell direction,and a bit to tell high gear,and a bit to tell if its low gear,
Can I write a decision to look at single bit?
Second question is answered by Kersing...

Enamul

Re: How do I read rs232 and get correct byte into variable?

Posted: Mon Aug 06, 2012 10:00 pm
by markus747
I am using RealTerm to send data to my pic and it seems a start bit is always used can I enable start Bit in the RS232 component macro?

Re: How do I read rs232 and get correct byte into variable?

Posted: Mon Aug 06, 2012 10:19 pm
by Enamul
markus747 wrote:I am using RealTerm to send data to my pic and it seems a start bit is always used can I enable start Bit in the RS232 component macro?
it's automatically done by pic usart. What do you mean by real term? did you mean real time?

Re: How do I read rs232 and get correct byte into variable?

Posted: Mon Aug 06, 2012 10:36 pm
by markus747
Enamul wrote:
markus747 wrote:I am using RealTerm to send data to my pic and it seems a start bit is always used can I enable start Bit in the RS232 component macro?
it's automatically done by pic usart. What do you mean by real term? did you mean real time?
RealRerm is a terminal software I am using to send a single byte out serial port, on my windows machine

Re: How do I read rs232 and get correct byte into variable?

Posted: Mon Aug 06, 2012 11:21 pm
by markus747
Enamul wrote:
markus747 wrote:I am using RealTerm to send data to my pic and it seems a start bit is always used can I enable start Bit in the RS232 component macro?
it's automatically done by pic usart. What do you mean by real term? did you mean real time?
even if you use 2 stop bits?

Re: How do I read rs232 and get correct byte into variable?

Posted: Tue Aug 07, 2012 12:44 am
by Enamul
RealRerm is a terminal software I am using to send a single byte out serial port, on my windows machine
Ok.,.,I got it., I have used terminal software which is good other than windows hyperterminal...so have you managed to transfer data from pc to your PIC board? By the way have you used matrix RS232 board or you have made any custom board to interface between PC and PIC as you have to use for voltage level conversion. PC uses +12.,-12CV swing in RS232 whereas PIC uses only 5V to 0V.

Regarding your post of stop bit..in pc you can send 1 or 2 stop bit but in flowcode default is (9600,N,1,8), so to change this I believe we have to change in c code., But first of all what you are trying to do...there might be an alternative to that., so please let us know what is your plan...
Enamul

Re: How do I read rs232 and get correct byte into variable?

Posted: Tue Aug 07, 2012 1:48 am
by markus747
I am using a ADM232LJN (max232 also makes similar chip)16 Pin dip chip to convert levels for pin 25 and 26 for either pic16f877 or pic16f887 and it seems to receive data good with 1 stop bit even seems to work with 2 stop bits but maybe some data is being tossed.

Another note my programming partner in other room got a pic to use standard pic levels 0 and +5 and had good data transfer without level converter!!! and have a small menu appear! only using a fet to invert incoming data and it worked fine.
I am still working on same reel project shown in active low thread and have made a lot of progress with this awesome program in just a week.

I don't see how the DATA[5] array is created I attempted to copy DATA = DATA0 into a calculation. from file RX_STR_RS232-1.FCF
I am still working on same reel project shown in active low thread and have made a lot of progress with this awesome program in just a week!!!
But it gives error "attempt to index non array object" when creating calculation
I = 0
VAR1 = 0
VAR2 = 0
VAR3 = 0
VAR4 = 0
VAR5 = 0
This above is another calculation but I don't see how this creates data[5] array
TIA

Re: How do I read rs232 and get correct byte into variable?

Posted: Tue Aug 07, 2012 12:00 pm
by Enamul
I don't see how the DATA[5] array is created I attempted to copy DATA = DATA0 into a calculation. from file RX_STR_RS232-1.FCF
But it gives error "attempt to index non array object" when creating calculation
I = 0
VAR1 = 0
VAR2 = 0
VAR3 = 0
VAR4 = 0
VAR5 = 0
This above is another calculation but I don't see how this creates data[5] array

I have created array as normal variable declaration..I am posting here a screen shoot showing how to declare array variable with different array size...In the create a new variable window, write your array name in Name of new variable box while string is selected in bottom..put array size in third bracket..
Also for the newbie..it's good to go through the following MM course as it's freeee....but nice for knowing some basic tips and tricks
http://www.matrixmultimedia.com/courses/itm/index.php
I am using a ADM232LJN (max232 also makes similar chip)16 Pin dip chip to convert levels for pin 25 and 26 for either pic16f877 or pic16f887 and it seems to receive data good with 1 stop bit even seems to work with 2 stop bits but maybe some data is being tossed.

Yes. ADM232LJN is similar to MAX232 and that's fine. It's better to use 1 stop bit as PIC is following 1 stop bit protocol..
Another note my programming partner in other room got a pic to use standard pic levels 0 and +5 and had good data transfer without level converter!!! and have a small menu appear! only using a fet to invert incoming data and it worked fine.

Actually you can do the level conversion using FET or transistor...I guess your friend is doing the that or he's using a board which doing that. It's pretty simple when dealing with TX from PC, send that to transistor base and collector supply will be 5V...then PIC will see 5V..on the contrary, for PIC Tx, send that to another transistor base whose power supply is 12V...so PC will see that 12V..
Enamul