Page 1 of 1

RS232 and integers

Posted: Wed Nov 09, 2011 9:49 pm
by Jan Lichtenbelt
I use RS232 (software, only TX and RX) between 2 PIC's. I like to send/receive integers. Is that possible? Up to now it seems to me that only the first 8 bits of the integer are send.

Kind regards

Jan Lichtenbelt

Re: RS232 and integers

Posted: Wed Nov 09, 2011 10:03 pm
by dbasnett
I think you can send two bytes, the order being up to you.

byte = integer
send byte
byte = integer right shift 8
send byte

Re: RS232 and integers

Posted: Thu Nov 10, 2011 10:55 am
by Benj
Hi Jan,

RS232 in hardware mode can only transfer 8 or 9 data bits in one transaction. The 9th data bit is normally used for addressing using say the RS485 protocol.

This article may help on splitting and recombining large variables into multiple bytes.
http://www.matrixmultimedia.com/article.php?a=366

Re: RS232 and integers

Posted: Thu Nov 10, 2011 2:12 pm
by dbasnett
The link Ben posted is exactly what I meant, but explained far better.

Re: RS232 and integers

Posted: Thu Nov 10, 2011 4:17 pm
by Jan Lichtenbelt
Is there a flowcode (send and receive part) for RS232 (software based) between 2 PIC's for INTEGERS? I tried it but without succes up to now.

Kind regards

Jan

Re: RS232 and integers

Posted: Thu Nov 10, 2011 8:45 pm
by Dan81
hello Jan

This flowchart seems to be OK (within Proteus).
It is only for sending, for now.
Can test it in real live ?

Daniel

Re: RS232 and integers

Posted: Thu Nov 10, 2011 10:05 pm
by Jan Lichtenbelt
Dear Daniel,

I'm afraid that does not work. What you do is to change a byte into an integer (in some way) and than sending this integer by RS232 in one step. But RS232 will send only lower_byte and not the integer (if I'm correct). If you make a receiver, you will find only the low-byte part of the integer at the receiver site.
But thanks for helping finding a solution.

Kind regards

Jan

Re: RS232 and integers

Posted: Fri Nov 11, 2011 6:57 am
by Mikat
Hi.

Why you just can't split the int in 2 bytes, send them and combine them back to int? At least that is what I do, I have an Eberspacher ecu, done by flowcode, and it uses rs232 (at the moment, the pcb to join that eber to CAN network, is under way) to communicate with fan controller unit, there is at least 5 int to sent, an it works fine..
I mean like this

byte = int >> 8
sent byte
byte = int
sent byte
and other device

rec byte
int = byte << 8
rec byte
int = int + byte
Like Ben says, the rs232 specs support only up to 9bit transfer...
Even in CAN I need to break int to bytes, to sent int, it's probably possible, by the modification of the software rs232 c code, but it's not rs232 anymore after that...

Mika

Re: RS232 and integers

Posted: Fri Nov 11, 2011 8:26 am
by medelec35
Hi Jan,

I only send 10bit integers that are displayed in hyperterminal, but I send them as ACSII string.
RS232 component has to be set to Characters and not bytes.
To send several different integers I use the format as in the screen shot below:
send int rs232.png
(11 KiB) Downloaded 5761 times
Using a temp string Variable saves on ROM being used.

Is is possible for you to send as a string, then the receiver can convert the string to integer using StringToInt$() function
You can use a decision to separate integers by waiting for a ASCII space.

You may not want to do it this way, but I thought I would show an alternative to splitting integers into bytes.
In theory this sending and receiving would work, but not tried it out on real hardware.
I have only ever use sending.
Martin

Re: RS232 and integers

Posted: Fri Nov 11, 2011 9:20 am
by Dan81
Hello Jan

In my previous flowchart , the calculation create a variable value with 16 bits.
In this version , I send the (integer) value of the ADC, 16 bits are sent but the last 6 are always "0".

I can only test with Proteus, I don't have an oscilloscope at home.

Daniel

Re: RS232 and integers

Posted: Fri Nov 11, 2011 1:16 pm
by Jan Lichtenbelt
Dear Martin,

Thanks for the simple solution. It works. Please find the two Flowcodes used.

Kind regards,

Jan

Re: RS232 and integers

Posted: Fri Nov 11, 2011 2:22 pm
by medelec35
Hi Jan,
Glad you have got what you want to work.
I have made a slight alteration to your flowchart so you can receive all the integer range including 0
Since on your flowchart you can’t receive a 0
Thanks for sharing your soultions and for letting us know.

Martin

Re: RS232 and integers

Posted: Sat Nov 12, 2011 9:33 pm
by Jan Lichtenbelt
string length should be >0 or >=1, instead of >1.

Jan

Re: RS232 and integers

Posted: Sat Nov 12, 2011 10:10 pm
by medelec35
Jan Lichtenbelt wrote:string length should be >0 or >=1, instead of >1.

Jan
The reason I used >1 is when stepping within flowcode simulator and there is nothing in rs232 buffer, then string always has 1 char that looks like a square which when converts to int produces a 0....hence >1 to get around that. If on real hardware this does not happen then fair enough use >0. I am relying on Flowcode simulator since not tried it on real hardware.
Thanks for letting me know.

Martin

Re: RS232 and integers

Posted: Mon Nov 14, 2011 10:01 am
by Jan Lichtenbelt
With Length>1 you miss all numbers 1-9.

Re: RS232 and integers

Posted: Mon Nov 14, 2011 10:03 am
by Jan Lichtenbelt
Can someone tell me, when sending data in this way, there is a need for a delay between two RS232 calls. 10 msec fails and 100 msec is enough. Why is there a delay necessary? And what is the shortest time needed?

Kind regards

Jan Lichtenbelt

Re: RS232 and integers

Posted: Mon Nov 14, 2011 10:33 am
by medelec35
Jan Lichtenbelt wrote:With Length>1 you miss all numbers 1-9.
That’s Fair enough.
It’s just a case of Flowcode simulator differs from results of real hardware.

Martin

Re: RS232 and integers

Posted: Mon Nov 14, 2011 12:51 pm
by Benj
Hello
Can someone tell me, when sending data in this way, there is a need for a delay between two RS232 calls. 10 msec fails and 100 msec is enough. Why is there a delay necessary? And what is the shortest time needed?
I have had a look at your program and you don't seem to be sending any data. You should not need any delays in between RS232 component macro calls in Flowcode.

Re: RS232 and integers

Posted: Tue Nov 15, 2011 6:32 am
by Mikat
Jan Lichtenbelt wrote:Can someone tell me, when sending data in this way, there is a need for a delay between two RS232 calls. 10 msec fails and 100 msec is enough. Why is there a delay necessary? And what is the shortest time needed?

Kind regards

Jan Lichtenbelt
No delays should be needed, at least the eber ecu where I use rs-232 sends about 15 bytes with no delays, and it works like charm..
The other end has UART incoming int, and it reads also without delays...
If I don't remember wrong, the bus it tested at least 9600-38600 baud rates, I am not sure about 115200, but I might test it too.


Mika

Re: RS232 and integers

Posted: Wed Nov 16, 2011 12:47 pm
by Jan Lichtenbelt
I tried some different conditions and found that:
1) Timeout must be 1 (I do not know why) for the best results. 0 and higher values give bad results.
2) Increasing the baut rate improves the results
3) Sending with time delays < 1 msec gives bad results

Kind regards

Jan Lichtenbelt