CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Serial Port

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Blackjackmaster



Joined: 14 May 2023
Posts: 30

View user's profile Send private message

Serial Port
PostPosted: Sun May 14, 2023 1:05 pm     Reply with quote

This is the code I am using to turn on the Green and Yellow LED from the Serial Monitor from the CCS IDE and works as it should. I run this code in a C# program and it does not work. The LED does turn on but does not toggle, will not turn off. Is there any Sample Code between CCS and C# Serial Port?
Code:

#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay (clock = 20000000)
#use rs232(baud=9600,xmit=PIN_C6, rcv=PIN_C7)

#define Green pin_a5
#define Yellow pin_B5

unsigned int8 x;

void main()
{
   output_b (0b00000000);
   
   while(1)
   {
   printf("\n\r Hit a key (1-4) to toggle LED");
   x=getc();
   printf("\n\r You Hit  "); putc(x);
   if (x=='1')              output_toggle(Green);
   else if (x=='2')    Output_toggle(yellow);
   
   }
}

C# Code: Button 1 Send, Button 2 Receive
Code:

 private void button1_Click(object sender, EventArgs e)
        {
            SerialPort1.WriteLine(textBox1.Text);
            textBox1.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                textBox2.Text = SerialPort1.ReadLine();
            }
            catch(TimeoutException)
            {
                textBox2.Text = "Timeout Exception";
            }
        }
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon May 15, 2023 12:55 am     Reply with quote

No,
they are fundamentally different languages. C# is an event driven language,
C is not.
If you have problems with C# you really need to ask on a C# forum, not here.
However looking at what you post, I see nothing in either button event,
doing anything to an LED. So how is it meant to work?.
jeremiah



Joined: 20 Jul 2010
Posts: 1314

View user's profile Send private message

PostPosted: Mon May 15, 2023 8:00 am     Reply with quote

It's a poorly worded question. They are trying to say they want to use a C# program to control the micro and are showing the micro code that receives serial commands and the C# code that sends serial commands.


Side note, C# is not an event driven language explicitly. It definitely has a lot of native support for events, but it also does structural programming among other programming styles. C does support events (ISRs for example are events), however, it doesn't have language built ins that facilitate it more easily like C# does. Just adding clarity for folks for later on when this thread is read years later.

For the OP:
I would start with print statements to ensure your C# code is firing the correct lines, then use an oscilloscope to verify the serial data is on the line and at the correct baud.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon May 15, 2023 9:13 am     Reply with quote

He seems though to be saying that he has the C code working, and the
C#  program does not do the same.
However he is not doing the same things in the handlers for the buttons
that he does in the C program. No wonder it does not do the same.... Sad
C# fundamentally has event handlers for things like buttons, that are
called when these events happen. The core of C does not have this. You can
generate similar behaviour with interrupts (he could have his buttons
interrupt driven), but as shown the two programs are not even remotely
equivalent.
If he wants to learn C# programming then he really does need to be
asking on a C# forum, not here.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon May 15, 2023 9:51 am     Reply with quote

I see what you are saying.
The same comments still apply though. If you look at his C program this
expects to receive '1' or '2' sent. If he wants the C# program to control
this, then the button1 handler has to send '1', and the button2 handler send
'2'. Not what is being done :(
jeremiah



Joined: 20 Jul 2010
Posts: 1314

View user's profile Send private message

PostPosted: Tue May 16, 2023 7:52 am     Reply with quote

In their C# code, they have a button click handler with:
Code:
SerialPort1.WriteLine(textBox1.Text);


which takes a value from a text box and sends it over serial. My thoughts are that they use this to send the values, especially since the CCS code is looking for ASCII 1 and 2 instead of raw 1 and 2.

It might be wrong code and you are correct they should be looking at a C# forum for that part.

I think my suggestions above are worth trying though.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Tue May 16, 2023 9:39 am     Reply with quote

That means he would have to go to that specific textbox, type '1' into it,
then hit button 1. Or for the other LED type '2' into it, and then hit button 1
again.
Doesn't really make sense.... Sad
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue May 16, 2023 9:47 am     Reply with quote

I've always used a 'terminal program' like RealTerm for 'basic-see-if-it-works' communication to-from a PC. When that works, THEN tackle the 'fancy' code cutting.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 16, 2023 11:44 am     Reply with quote

temtronic, the OP has done that. From his first post:
Quote:
This is the code I am using to turn on the Green and Yellow LED
from the Serial Monitor from the CCS IDE and works as it should.

In other words, he has tested the PIC code by sending chars to it with
the CCS SIOW.exe program.
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue May 16, 2023 3:50 pm     Reply with quote

hmm, I misinterpretted the statement, thinking it was some internal custom software like the debugger uses, not that he'd used SIOW.
Since that does work, then it's just incorrect C# program.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed May 17, 2023 1:15 am     Reply with quote

Exactly.
And the point his the C# program makes no sense at all. It doesn't look
for the serial message till you push 'button2', and then as already outlined
you have to type the right value into a textbox, and then press button1
to do the toggle.
It really should be using the serial receive event, to clear the text box
when a line feed is received, and then write the message into this.
Then two buttons, one simply sends '1' and the other '2'.
Result a program that can work.
Nothing at all to do with CCS C, he needs basic help in writing a C#
program. He confuses things more by talking as is he wants to translate
code between the two languages.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group