Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Different Data on Serieal Port.

Status
Not open for further replies.

Ayne

New Member
PIC is doing different functions (like Voltage measurement and level measurement, frequency measurement etc etc) and sending these values to a PC via serial port one after another.

Now my question is, How PC detect that data is coming from PIC is the data of voltage or Level or frequency.
 
The PC doesn't care, since it only receives a binary numerical data on what the information is from the PIC. You code your PIC to tell the PC what type of data it is. You tag some kind of identifier right before you send the data, and this identifier tells the PC what the type of data is coming next.
 
But the identifier can mix with the orignal data.
i mean like this
identifier for voltage 11111111 and the result of ADC is also 111111111 then??
 
Convert the values to ASCII (either decimal or HEX), and prefix it with a letter, so your voltage value above could be sent as VFF (HEX) or V255 (decimal), I would suggest you also send CR/LF after each value. It's simple for the PC program to them decide what the reading is, check the first letter (and then throw it away), and convert the string back to a number.

This also cures any potential problems over using binary values.
 
CR is carriage return (0x0D) and LF is line feed (0x0A). These ASCII (American Standard Code for Information Interchange) control characters date from the days of mechanical teletype machines (ca. 1960's) and were appended to the end of each line of text. Their use has survived the death of the machines which spawned them. In the earlier five level Baudot code (ca. 1874) used on the the RO-28 there were also a carriage return (0x08) and a linefeed (0x02) character.

https://en.wikipedia.org/wiki/ASCIIhttps://en.wikipedia.org/wiki/ASCIIhttps://en.wikipedia.org/wiki/ASCII
https://en.wikipedia.org/wiki/Baudot_code
 
I have an example written in VB05, you can get VB05 for free and to interface a pic with the PC is quite easy.

When receiving data from the PIC, the serial port control will continually store data in its buffer until it receives a line feed, then it will assume it has received the entire message.

You can modify the first byte of each message with a byte containing an ASCII code ( see http://en.wikipedia.org/wiki/ASCII and scroll down to ASCII printable characters ) and just say V meant voltage and F meant frequency then the data stream would look something like this from the PIC to the PC;

V1023LFF558LFV1022LFF559


**broken link removed** gives a brief example of how to use pic basic with PIC to PC interfacing via a DS275
 
Now i am confuse in "decimal or HEX"
Final_Voltage = (ADC_Value * 40) / 1023 'our range is 40V maximum
USART_write(Final_Voltage) ' send data via USART
in above example i am sending data in decimal,
how can i send this data in HEX????
 
You do realize that the ADC_Value is actually in binary but you are representing it in decimal in your equation right? (since you have 1023 in the equation) rather than 1111111111. I think you're just getting caught up in semantics. The compiler should be able to deal with this quite easily.
 
Unless he's trying to send the HEX value as an ascii string.
 
Ayne:

Just send the data out of the PIC and let the PC do all the conversion. The main solution is for YOU to decide how to delimit (distinguish) your data types. Summing up others and some new tips

Either of these:
* If data interval is regular and structured, send out a fixed length burst (Volts, Level1, Level2, Frequency). First byte recieved is Volts, second byte is Level2, third byte id Level2 and so on...

The below are better suited for random transmission:
* Use double byte data pairs for each measurement with first byte indicating the data type. 'V' 255, 'L' 128, 'F' 64 .... Insert a pause between each data pair to create an "end-of-packet" marker.

* Use double byte data pairs and insert "end-of-packet" markers like CR or LF.
* Use double byte data pairs and toggle hardware lines like RTS or CTS as "end-of-packet" markers.

* If measures are larger than a single byte then use X-bye data pairs with the first byte and the data type indicator.

The code on the PC side is structured according to the method you chose to send data on the PIC side.
 
Hi

From experience I would recommend the following principles when
sending numeric data to a PC, for possible data logging, via the PC's
serial port.

1. Use ASCII characters, not bin/hex

2. Prefix each data parameter with an alpha character, choose
the prefix character that best represents the parameter.

3. Keep the individual parameter strings a fixed length, that is send leading
zero's and trailing zero's as required.
[choose a parameter string length that hold the maximum value you may
expect for that parameter]

4. Pre format the parameter within the PIC program, if a decimal
point is required by the parameter, send the decimal point.

5. Consider the use of the serial parity bit.

6. In more critical applications consider using a CRC, a cyclic redundancy
character at the end of your parameter strings.

The reason for these recommendations, is that you should always
take into consideration what use the data is going to serve when its
been received by the PC.

If you keep the data parameter in ascii format, with the data
fields prefixed, with fixed length, it makes the data easier to check.
I would expect that the incoming data will logged by the PC as well
as being displayed as its received. If its logged as a ascii text file
a text editor can used to dislay/edit the data.

Simple error checking can be carried in the receiving PC.
eg; prefix char, string length, deci point position, crlf
The users I have dealt with, want to see the leading and trailing zero's
'just to make sure' that a digit has not been dropped.


examples:
if a voltage parameter is being sent followed by say a frequency parameter
V12.30<crlf>
F01234<crlf>
etc,etc

the <crlf> notation after the ASCII text represents a carriage return code
followed by a line feed code, 0x0D,0x0A

Hope this helps.

Regards
EricG
 
What will be the instruction cycle time for 20Mhz clock??? PIC16F877A.

I think,
20000000 / 4 = 5000000
1 / 5000000 = 0.0000002 = 200nSec
Am i right??
 
hi ayne,
for ref: a 16f877 at 4mHz, Tcyc= 1uSec
so at 20mHz its 5 times shorter ie; 1/5= 0.2uSec = 200nSec.

Regards
EricG
 
You ask a question.
Many people take time to answer your question.
You then ask a totally unrelated question.
You answer your own question.

I can only conclude that you are either an idiot, a troll or a Walter.

Mike.
 
Pommie said:
You ask a question.
Many people take time to answer your question.
You then ask a totally unrelated question.
You answer your own question.

I can only conclude that you are either an idiot, a troll or a Walter.

Mike.


Thats fairly harsh, remember its a support forum, who are you to make calls like that?.?

The chap probably just feels like this is 'his thread' and is not familiar with forums systems, thus the second query.

Perhaps next time just suggest to open a new thread if its an unrelated question.

And good on him for at least trying to figure out the answer first, if only 90% of the other people out there did the same thing before posting.
 
If he tried to figure out the answer first he wouldn't have posted the original thread.. Not to be harsh, but 99.9% of the posts I think I should make here I don't because I investigate them a little bit. The other .1% are all opinion based questions. Gotta have thick skin to be in forums on the net. No reason not to make posts, but don't take offense at criticism, it's just as useful as anything else.
 
I understand now that how things is going on.
below is my example. Plz tell me i am right or wrong.

Supose i want to transmitt two values.
1. Voltage = 27
2. Current = 03

Now i will chose a letter that will be best representer for my vlaues.
Suppose i chose V for Voltage and C for current. Right!
Then i will convert my numaric value into ASCII.

Now i will transmitt,
For Voltage
i will send
Code:
(ASCII value of V)-(ASCII value of LF)-(ASCII value of 2)-(ASCII value of 7) (ASCII value of LF)-(ASCII value of CR)

For Current
i will send
Code:
(ASCII value of C)-(ASCII value of LF)-(ASCII value of 0)-(ASCII value of 3)-(ASCII value of LF)-(ASCII value of CR)

The whole code will look like this
Code:
(ASCII value of V)-(ASCII value of LF)-(ASCII value of 2)-(ASCII value of 7) (ASCII value of LF)-(ASCII value of CR)--(ASCII value of C)-(ASCII value of LF)-(ASCII value of 0)-(ASCII value of 3)-(ASCII value of LF)-(ASCII value of CR)

Am i right???
 
Ayne said:
I understand now that how things is going on.
below is my example. Plz tell me i am right or wrong.

Supose i want to transmitt two values.
1. Voltage = 27
2. Current = 03

Now i will chose a letter that will be best representer for my vlaues.
Suppose i chose V for Voltage and C for current. Right!
Then i will convert my numaric value into ASCII.

Now i will transmitt,
For Voltage
i will send
Code:
(ASCII value of V)-(ASCII value of LF)-(ASCII value of 2)-(ASCII value of 7) (ASCII value of LF)-(ASCII value of CR)

For Current
i will send
Code:
(ASCII value of C)-(ASCII value of LF)-(ASCII value of 0)-(ASCII value of 3)-(ASCII value of LF)-(ASCII value of CR)

The whole code will look like this
Code:
(ASCII value of V)-(ASCII value of LF)-(ASCII value of 2)-(ASCII value of 7) (ASCII value of LF)-(ASCII value of CR)--(ASCII value of C)-(ASCII value of LF)-(ASCII value of 0)-(ASCII value of 3)-(ASCII value of LF)-(ASCII value of CR)

Am i right???

No, don't send LF beween the characters, send it as a complete string terminated by LF/CR like this:

V27C03LF/CR

This will result in the string 'V27C03' at the PC, it's then trivial to seperate them and convert back to numbers (if you need to).
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top