data sending from PIC18 to rs232 to visual studio c#

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
vEnOm
Posts: 5
Joined: 27 May 2009 13:03

data sending from PIC18 to rs232 to visual studio c#

#1 Post by vEnOm » 27 May 2009 13:19

hi all

Im currently trying to write a program using mikroc(or any other software using C LANGUAGE,if u know please let mi know) to communicate with visual studio c#. My visual studio c# have the library and code to communicate with my pic, however, i couldnt program my pic18f452 to communicate with VS C# as i dont see any code in mirkoc that allows me to do that.

I wanted to send data from my pic to visual studio and make a windows application from there using the data i receive from my pic18f452.

Does anyone knows how/wads the code/library in mikroc for PIC to communicate with c#? or is there any software that works like mikroc that can transfer/receive data from visual studio?

any help or rely will be deeply appreciated, thanks for reading:D

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

#2 Post by drdoug » 27 May 2009 13:53

Look at the "Software Uart" library in the help section. I don't think the 452 has a built in UART. The example from the help file can also be found in the the mC examples that were installed with the program.

I use chips with the built in uart and it is incredibly easy but the software uart can work well too especially is just sending from PIC and not receiveing to PIC.

There

dobova
Posts: 200
Joined: 13 Feb 2005 16:23
Location: Italy

Re: data sending from PIC18 to rs232 to visual studio c#

#3 Post by dobova » 27 May 2009 16:25

vEnOm wrote:

I wanted to send data from my pic to visual studio and make a windows application from there using the data i receive from my pic18f452.

any help or rely will be deeply appreciated, thanks for reading:D
I don't get exactly what you mean... but I suppose you'd like to write a program in C# to read serial data from PIC.
If you are using VS2005-8 you have ".NET" Framework, that contains library to manage RS232 and COM ports. In previous VS this doesn't exist and you need some external lib. VS doesn't need to know nothing about PIC, just develop your protocol and implement on both sides.
Ciao
Dome

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

#4 Post by Mince-n-Tatties » 27 May 2009 16:37

18F452 has hardware UART, pins RC6(RX) & RC7(TX) please ref to data sheet which you can find on microchip site.

There is no specific library in mikroC to send data to C#.

in simple terms this is what you need to do...

1. Write software (aka Firmware in embedded world) for your PIC using mikroC, which will send some data out of the PIC to be collected on the PC.
2. Write software in C# to read data from the serial port (real or virtual USB type).

ideas as to what you could/should be looking to do...

so you need to look at PIC UART examples then decide are you going to send just some known data variable or something useful like an ADC temperature reading via the PIC UART. Should it be able to take data from the PC as well as send data to the PC. Tx & Rx

on the PC your C# code will be either a dumb data collector which would capture from the serial port or it could be more useful like have a graph and visual display of temperature data being sent by PIC.

hardware you need at the very least...

PIC18F452, RS232 IC circuit (MAX232) 9 pin D serial connector, serial cable between pic and PC, and of course you need a PC with RS232 port or a USB to RS232 cable.

Are you using mikroElektronika hardware (EASYPIC 3,4,5)?
what version of C# are you using 2003, 2005, 2008?
Is this your first attempt at working with pic's?
Is this your first attempt at working with mikroC?

JohannesJ
Posts: 20
Joined: 25 Jan 2009 11:45
Location: Belgium Sint Genesius Rode

#5 Post by JohannesJ » 27 May 2009 19:51

Hello VenOm

Try this link
http://www.tetraedre.com/advanced/serial/

With C++Builder Borland it works fine
Pic ->C++ and C++->Pic

Just "Rebuild the library a litle" and it should work on C#

Enjoy

Regards

JohannesJ :wink:
EasyPic6 and some other stuff
Nice to play with!!

vEnOm
Posts: 5
Joined: 27 May 2009 13:03

#6 Post by vEnOm » 28 May 2009 04:11

Hello all.

Thanks for replying my post. :D

I'm using visual studio 2005 professional editions for programming my c# and mikroc/mikroc pro for programming my pic. I had e rs232 connected to port d and rs232 connected to my pc.

I had actually did programming to my 18f452 before, using mikroc and tiny bootloader and it works. However, i wanted to learn how to send data from my pic to c# now.

For now, I only wanted to sent a int (data) from my pic to my c# application to start off first for my project before i proceed any further.

I had write a program on my c# and mikroc and it dont seems to work

Programming in my mikroc

Code: Select all

....
unsigned ttt=0;
Soft_Uart_Init(PORTC6,7,9600,0); 

//i dont understand this part but i had read the data sheet, and know that //Pin RC6 and RC7 houses the USART which enable asynchronous
//and synchronous serial transmission.Im using baud rate 9600. so
// is my "Soft_Uart_Init(PORTC6,7,9600,0); " correct?


while(1){
.....
   WordToStr(ttt,out);
Soft_Uart_Write(out);       
// so do i send it out like this?
}
Programming in my c#

Code: Select all

using System;
using System.IO.Ports;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace temp_pd1
{
    public partial class Form1 : Form
    {
        SerialPort myserialport = new SerialPort();
        public Form1()
        {
            InitializeComponent();
        }
        private void setSerialFormat()
        {   if
            (myserialport.IsOpen)
        { myserialport.Close(); }
        myserialport.PortName = "COM2";
        myserialport.BaudRate = 9600;
        myserialport.Parity = Parity.None;
        myserialport.DataBits = 8;
        myserialport.StopBits = StopBits.One;
        myserialport.Open();
        }
        private void read()
        {
            string i;
            byte[] buffer = new byte[3];
            try
            {
                myserialport.Read(buffer, 0, 3).ToString();
                i = buffer[3].ToString();
                label2.Text = i;
            }
            catch
            {
                MessageBox.Show("error");
            }

        }
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                setSerialFormat();
                read();
            }
            catch { MessageBox.Show("error"); }

        }

for the c#, i believe my code for read is wrong too,
can any one guide mi and or roughly show me the code it should be for c# and mikroc?

Thanks to you guys once again.. :D

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

#7 Post by Mince-n-Tatties » 28 May 2009 10:15

soft_uart is a software defined (bit bang) uart which can be implemented on any port, you have a pic with a hardware UART and you are using the hardware UART port (C).

so search the help file for UART (UART1) rather than soft_uart.

also be sure to tick the UART selection in the library manager!

with mikroC any functions prefixed with "soft_" are software emulation of hardware functions.

soft_uart_init <----- software defined emulation (bit bang)

uart1_init <--- real hardware function

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

#8 Post by drdoug » 28 May 2009 15:51

I'll rate my previous post a D+ for content and try to do better this time. As always, Mince-n-Tatties has some very good information to offer

I should have guided you toward using the hardware uart instead of software uart.

As MNT said, you can use

Code: Select all

Uart1_Init(9600);

for establishing your uart hardware port. This will make the appropriate settings for pin directions and baud rate. There is a lot going on "behind the scenes and if you really want to understand it, check out the datasheet and the asm file created. Also, it is N,8,1.

I have not written any programs in VC# but I have done several in VB 2005. You need to do a couple things for communication as I am sure you know but let me ask you a question.

Do you want to send a continuous/timed stream of data or request the data from the PIC?

If the PIC will send out periodic data (i.e. an integer value every 5 minutes):
1. Have the PC open the com port (if not already open)
a. Have the PC check the serial port for received data every 5 minutes.
b. Process the data. - Go back to a.
You may also choose to close the com port after your read and open it each time before a read.

If the PIC is sending continuous data, you need to read at regular intervals (every second) or perhaps use Delegates (which I am unsuccessful at implementing to date). The faster you send the data, the faster you need to check the data.


You have two options for sending integer data your data from the PIC.
The integer value is 16 bits wide and you can only send 8 bits through the uart so it needs to be broken into 2-8 bit sections.

This code is untested but I think it's close.

Code: Select all

unsigned int myint;
unsigned char mychar;
...
mychar  = myint;  // assign lower 8 bits to mychar
Uart1_Write(mychar);  // send lower 8 bits
mychar = myint >>8; // shift upper 8 bits to mychar.
Uart1_Write(mychar);  // send upper 8 bits
On the PC receive side, you can simply recombine the 2 bytes to use your integer. With this example you only send 2 bytes!

Code: Select all

**** Visual Basic Code*****
rxInt_Lo = SerialPort1.ReadByte
rxInt_Hi = SerialPort1.ReadByte
rxAll = (rxInt_Hi << 8) + rxInt_Lo
TextBox1.Text = CStr(rxAll) 
The second option is to send your data as a text string which I think is what you have done with your code. With this method, you will send 3 or 4 bytes of data instead of 2. The recombination at the PC is not too hard with VC#.

The PC serial port will save incoming data until the receive buffer is full (2K?) as long as the serial port is open.

If you want to request the data from the PIC, you will need to add some additional code on the PIC to read and interpret the data.

Also you may find some of these VB programs helpful for the VC# software since the VB concepts are carried across languages. http://www.vidgetech.com/html/downloads.html

Let me know if you need more *pointers

vEnOm
Posts: 5
Joined: 27 May 2009 13:03

#9 Post by vEnOm » 29 May 2009 09:03

Firstly, thanks you guys for replying and helping me.
to drdoug: I dont really know how to program VB, but i will try, thanks :D

im trying to send a string to my VS from my pic, i had just change to

uart1_init(9600);

Uart1_Write(out); // where out is a string

where it seems to work. however now its my c# that i doesnt know how to write the write() to read the string from my pic

Code: Select all

private void read()
        {
            string i;
            byte[] buffer = new byte[3];
            try
            {
                myserialport.Read(buffer, 0, 3).ToString();
                i = buffer[3].ToString();
                label2.Text = i;
            }
            catch
            {
                MessageBox.Show("error");
            } 
i realize there is a problem with my read(), can anyone help me?
im sending a string from my pic, and i want my vs c# to receive it and the display out

florinsmadu
Posts: 9
Joined: 18 Jul 2006 07:49

#10 Post by florinsmadu » 29 May 2009 11:46

On Visual Studio you have the namespace System.IO wich contains classes for work with ports.
You need to drag a serialport component from toolbox onto your form.
Look at the properties of that object and locate the "ReceivedBytesThreshold". Setting this will set at wich number of received bytes the Read event is fired.
On the DataReceived event you should write some code to manage the received bytes.

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

#11 Post by drdoug » 29 May 2009 15:26

Even though you don't know VB, alot of the setup is similar. As florinsmadu said, setup the serial port first. Then you have a few options for reading the data, .ReadExisting, .ReadTo, .ReadLine etc.
You can download my VB programs and see the components that you might include to get a working solution.

geeth
Posts: 10
Joined: 20 Feb 2009 16:30

#12 Post by geeth » 01 Jul 2009 21:07

Hi vEnOm

I'm trying to send some data to PIC from VISUAL STUDIO . do you have any idea about this. using MIKROC

Any help will be deeply appreciated.


Thanks

Post Reply

Return to “mikroC PRO for PIC General”