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 CCS Technical Support

Read a byte[] sent through usb to dev board

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



Joined: 25 Aug 2013
Posts: 28

View user's profile Send private message

Read a byte[] sent through usb to dev board
PostPosted: Wed Sep 03, 2014 1:06 pm     Reply with quote

I have a pic18f4550 dev board, and have successfully connected it to my laptop. I'm sending a byte[], through usb to this dev board, using c#, but nothing happens. I think the problem is in the code that I'm using for the dev board:
Code:

#include <ex_usb_common.h>
#include <usb_desc_cdc.h>   
#include <usb_cdc.h>

static void my_cdc_demo(void)
{
int8 received_char;
 
if(!usb_cdc_kbhit())
   return;

received_char = usb_cdc_getc();


if(received_char == '1')
  {
   output_high(PIN_B6);   // 1 = Turn on LED
  }

if(received_char == '0')  // 0 = Turn off LED
  {
   output_low(PIN_B6);
  }
 
// All characters other than '0' or '1' are ignored.

}
//===========================
void main(void)
{
output_low(PIN_B0);

usb_init_cs();

while(TRUE)
  {
   usb_task();

   if(usb_enumerated())
     {
      my_cdc_demo();
      delay_ms(1);
     }
  }
}


I think the problem is in this line:
Code:

int8 received_char;

this is because I'm sending a byte[], and in the dev board, I'm expecting an int8.
This is the code I'm using to send the byte[] in c#:
Code:

int sd = 0;
int to = 1;
byte[] sd2 = BitConverter.GetBytes(sd);
byte[] to2 = BitConverter.GetBytes(to);
ComPort.Write(sd2,0,4);
//and
ComPort.Write(to2,0,4);

Can someone please tell me how to fix this?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Sep 03, 2014 1:17 pm     Reply with quote

I think you still don't know the difference between an integer 1 and
and an ASCII 1. The integer's value is 0x01. The ASCII value is 0x31.
Look it up. They are not the same.

Also, you need to know how the C language represents an integer 1
and an ASCII 1. An integer can be represented as 1, 0x01, etc.
An ASCII 1 is put in single quotes '1', if you are using it as an individual
byte. This is all critical, elementary, basic, essential C knowledge.
The byte values are 0x01 for the integer value and 0x31 for the ASCII.
If you compare them, they will not be equal.

If you send an integer 1 by USB, then test for integer 1 in your PIC code.
Or, if you send an ASCII 1 by USB, then test for an ASCII 1 in your PIC.

The same thing is true for an integer 0 and an ASCII 0.
nando88



Joined: 25 Aug 2013
Posts: 28

View user's profile Send private message

PostPosted: Wed Sep 03, 2014 7:06 pm     Reply with quote

I changed my code in the desktop application, to:
Code:

ComPort.Write(BitConverter.GetBytes('0'),0,4);
ComPort.Write(BitConverter.GetBytes('1'),0,4);

This is because I need to send an ascii character inside a byte.
But I still can't turn on the led with my pic dev board.
Can someone please help me?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Sep 04, 2014 12:58 am     Reply with quote

I have no idea about C#. I searched with Google and quickly found a
sample application for C++:
http://www.orgler.it/visualc01.htm

I compiled it with MSVC++ vs. 6.0, and it worked for its default setting
of Com1 as the comport. So I know that code sample app works.

I then loaded my 18F4550 test board with the USB software that I posted
in your previous thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=52722&start=8

I ran that program and went to Windows Device Manager and looked
in the Com ports section and found that USB 18F4550 board was assigned
to Com5.

I then used "Find in Files" in MSVC++ to search the sample C++ program
for COM1. I found that line, and edited it to use COM5 instead.
I compiled the app in Release mode and made the RS232.exe file
and I ran it.

I then typed '1' in the text box and clicked the "Send text to RS232" button
and the LED on the 18F4550 board turned on. I then typed '0' and sent
it, and the LED turned off. It's working.

-----------------

This is not a C# or C++ forum. I did all this stuff above just to show you
how easy it is to do it. You need use Google, find a C# or C++ forum
and do it. Or use the source code from the website link above and learn
how to do it.
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