Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
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
C#
sp.WriteLine("123");


C#
;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
Updated 31-Jul-12 10:15am
v2
Comments
Sergey Alexandrovich Kryukov 31-Jul-12 16:18pm    
What you say (1, 2) is not true. Not at all.
--SA
[no name] 31-Jul-12 16:19pm    
The Write method for a serial port can also write a byte. You could try that.

1 solution

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
 
Share this answer
 
Comments
Khalid Sabtan 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 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 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 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 31-Jul-12 16:51pm    
My 5 is ready. :-)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900