Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# PIC
hellow members
My c# program sent a string="123" as a password to pic
The pic16f883 job is to read this string and insure that it is "123"
I could not do that
Those are my problem
1- i guss there is no way to have the c# program sent a number throgh serialport
i can only sent string
2- in the other side the pic16f883 can not read a string,it can only read a number
 
the following is part of my c# program and pic program
any help please
 
//c# program
sp.WriteLine("123");
 
;pic program
list p=16f883	;,R=DEC  ;Use decimal system
#include <p16f883.inc>
s0	equ 20
t0 	equ 21
;
val	equ 22
.
.
.
	movf	RCREG,W		;flush receive data register buffer
	movf	RCREG,W		;flush receive data register buffer
	movf	RCREG,W		;flush receive data register buffer
	bsf		PORTC,4
 
	;Assume the Computer has has sent a string="123" through serialport
	
	movlw	.123				;Pic know the password = 123
	movwf	val
	call	Rci				;Dose Pc sent 123 ?
 
;
Rci	nop	
up	call	Rs
	movwf	PORTB
	goto	up
Rs  clrwdt
	btfss	PIR1,RCIF	;Bit5 check for receiced data
	goto	up
	movf	RCREG,W		;save received data in w
	SUBLW   val			;password from c# program
	btfss	STATUS,Z
	goto	up		;Z=0,continue waitting for number val from pc
	return 	;W-val=0  ,Z=1
	;
	END
Posted 31-Jul-12 11:03am
Edited 31-Jul-12 11:15am
v2
Comments
Sergey Alexandrovich Kryukov at 31-Jul-12 16:18pm
   
What you say (1, 2) is not true. Not at all.
--SA
Wes Aday at 31-Jul-12 16:19pm
   
The Write method for a serial port can also write a byte. You could try that.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

1) It's absolutely not a problem to send any kind of data to a COM port using .NET (#C or not):
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx[^], see Write members.
 
2) If something can read a number (what size, though?), it means it can read a string. It depends on string encoding though. If you can use ASCII, each character is encoded as one byte, and all string encoding formats are Unicode UTFs; UTF88 is byte-oriented, UTF16 presents a character as one 16-bit word or a pair of them (called surrogate pair), UTF32 presents a character as one 32-bit word. When you write WritelLine("123") you send 5 16-bit words (line is represented with the characters 13, 10 (LF, CR)), because in CLI memory strings are represented in UTF16LE encoding. Most likely, this is not what expected. You can always serialize a string as required using on of the System.Text.Encoding classes. Please see:
http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx[^].
 
—SA
  Permalink  
Comments
Khalid Sabtan at 31-Jul-12 16:46pm
   
my 5 is almost ready
do you think this might works ?
waitting for your answer
 
Byte[] buff=new Byte[5];
buff[0]=0x10;
buff[1]=0x11;
buff[2]=0x0D;//lf and cr
buff[3]=0x0A;
mySerialPort.Write(buff);
CPallini at 31-Jul-12 16:57pm
   
The PIC microcontroller, if I'm not wrong, expects ONE (1) byte with decimal value 123. Hence, I suppose, you should write

Byte[] buff = new Byte[1];
buff[0]=123;
mySerialPort.Write(buff);
Sergey Alexandrovich Kryukov at 31-Jul-12 17:08pm
   
Thank you very much again. I don't know the spec for this controller's communication, but that would be most reasonable. OP can refer the documentation or simply experiment with that. My 5. :-)
--SA
Sergey Alexandrovich Kryukov at 31-Jul-12 17:06pm
   
I don't think so. 0x10 and 0x11 are control characters (http://en.wikipedia.org/wiki/ASCII#ASCII_control_characters), and then CR, LF. There is no data. If should be either binary value of some size (1 byte, 2 bytes, 4 bytes?) or a string, depending on what's expected. If you need to send a string, use System.Text.Encoding to obtain an array of bytes.
--SA
CPallini at 31-Jul-12 16:51pm
   
My 5 is ready. :-)
Sergey Alexandrovich Kryukov at 31-Jul-12 17:01pm
   
Thank you, Carlo.
--SA
Khalid Sabtan at 31-Jul-12 17:01pm
   
To CPallini
your comment helped me but i am not sure if the 5 shall reach you , any way i will vote for solution 1
Sergey Alexandrovich Kryukov at 31-Jul-12 17:09pm
   
Thank you for your trust, but better test it. After you are done, please consider accepting the answer formally (green button) -- thanks.
If the problem is not yet solved, your follow-up questions are welcome.
--SA
Khalid Sabtan at 5-Aug-12 12:08pm
   
this works fine i diid not get any error
//this how to call the method
Send2PicNumber(9);//assume you want to send number 9
 
//and this is the method
public static void Send2PicNumber(int Num)
{
Byte[] encodedBytes = new Byte[10];// ascii.GetBytes(str);
encodedBytes[0] = (Byte)Num;
_serial.Write(encodedBytes, 0, 1);
}
Sergey Alexandrovich Kryukov at 5-Aug-12 17:08pm
   
No doubt it should work.
Will you accept this answer formally (green button)?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 5,130
1 DamithSL 4,237
2 Maciej Los 3,700
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,846


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 31 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100