Click here to Skip to main content
13,090,503 members (35,493 online)
Rate this:
Please Sign up or sign in to vote.
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

;pic program
list p=16f883	;,R=DEC  ;Use decimal system
#include <>
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
Posted 31-Jul-12 10:03am
Updated 31-Jul-12 10:15am
Sergey Alexandrovich Kryukov 31-Jul-12 16:18pm
What you say (1, 2) is not true. Not at all.
Wes Aday 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
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):[^], 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:[^].

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[2]=0x0D;//lf and cr
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];
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. :-)
Sergey Alexandrovich Kryukov 31-Jul-12 17:06pm
I don't think so. 0x10 and 0x11 are 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.
CPallini 31-Jul-12 16:51pm
My 5 is ready. :-)
Sergey Alexandrovich Kryukov 31-Jul-12 17:01pm
Thank you, Carlo.
Khalid Sabtan 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 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.
Khalid Sabtan 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);
No doubt it should work.
Will you accept this answer formally (green button)?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 31 Jul 2012
Copyright © CodeProject, 1999-2017
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