Click here to Skip to main content
11,577,258 members (59,959 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C#4.0
could not receive string from serial port which is connected to Pic p16f883,i have used str=sp.readline();
the string that i have sent from the pic is x y ,i mean x then space then y then CR THEN LF
the following is complete code for both assembly and c# program
the frequency in both program is 9600
using System;
using System.Collections;//.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace graphic1
    public partial class Form1 : Form
    {   SerialPort sp = new SerialPort();
        int i,j;
        public Form1()
            sp.PortName = "com1";
            //sp.PortName = "com2";
            sp.BaudRate = 9600;// 115200;// 9600;
            sp.DataBits = 8;
            sp.Parity = Parity.None;
            sp.StopBits = StopBits.One;
            sp.Handshake = Handshake.None;
            sp.ReadTimeout = 5000;// Timeout.Infinite;//500;
        private void StartButtonClick(object sender, EventArgs e)    
            string str;
                for (j = 0; j < 32000; j++)
                            str= sp.ReadLine();//example   x y
        protected override void OnClosing(CancelEventArgs e)
            if (sp.IsOpen) sp.Close();
	list p=16f883	;,R=DEC  ;Use decimal system
	#include <>
s0	equ 20
t0 	equ 21		;for eeprom-
j			equ 22
i			equ 23
	clrf	PORTA
	clrf	PORTB
	clrf	PORTC
	clrf	ADCON0
	bcf		STATUS,RP0	;
	bsf		STATUS,RP1	;WE ARE in bank2 ( 1 0 )
	clrf 	CM1CON0		;No comparator c1
	clrf	CM2CON0		;No 1ST comparator c2
	clrf	CM2CON1		;No 2ND comparator c2
						;for controlling the interaction with Timer1
	bsf		STATUS,RP1	;
	bsf		STATUS,RP0	;BANK3 ( 1 1 )
	clrf	ANSEL		;configure anlog chanal as Digital input
	clrf	ANSELH
	bcf		STATUS,RP1  ;
	bsf		STATUS,RP0	; bank1 ( 0 1 )
	movlw	b'00000000'
	movwf	TRISA		;Porta is output,bit1 is to turn pc on
	clrf	TRISB
	movlw	b'10000000'	;C7 RX(I/P)
	movwf	TRISC
	movlw	b'00000000'
	movwf	ADCON1		;bit7=0=left justified,
						;bit5=0=voltage refrece bit ,vss=0
						;bit4=0=voltage refrece bit ,vdd=0
						;rest are un implemented
	movlw	b'00001000'
	movwf	TRISE
	movlw	b'10001000'
	movlw	0x19		;x=25 for 9600 baud rate,9600=16000000/(25+1)x,solve for x,x=25,  x=0x19 hex
						;BAUD RATE=9600 NO PARITY,1 stop bit
	movwf	SPBRG		;0x19=9600 bps (ox0c=19200bps,where 19200=16000000/(25+1)*x ,solve for x,x=12,x=0x0c
	movlw	b'00100100' ;brgh= high (2)
	movwf	TXSTA	    ;enable Async Transmission,set brgh
    					;bit7=0=slave,bit6=0=8bit,bit5=1=Transmit enable,bit2=1 high speed

	bcf		STATUS,RP1
	bcf		STATUS,RP0	;BANK0 ( 0 0 )
	clrf	PORTB
	clrf	PORTC
	clrf	PORTA
	bcf		STATUS,RP1
	bcf		STATUS,RP0	;BANK 0 ( 0 0 )
	movlw	b'10010000'	;enable Async Reciption
	movwf	RCSTA	;get error flage
	;bcf	PIE1,TXIE	;disable tx interrupt
settle		clrwdt
			decfsz	s0
			goto	settle
		movlw	.6	
		movwf	i		
		call	delay
	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
	call	delay
	bcf		PORTC,4
top2	nop
		movlw	0x78	;x in hex =0x78 ,y in ascii 120
		call	Send	;send 0x78 to computer,computer shall read string=x
		movlw   0x20;	Space IN ASCII=32 =0x20 in hex 
		call    Send	;Send w
		movlw	0x79	;y in hex =0x79 ,y in ascii 121
		call	Send	;send 0x79 to computer
		call 	PicSendCRLF
		goto 	top2
PicSendCRLF		movlw	0x0D	;CR
				call	Send	;Send CR
				movlw	0x0A	;LF
				call	Send	;Send LF
Send				movwf	TXREG			;PicSendMessage data in W
TransWt				bsf		STATUS,RP0		;RAM PAGE1
WtHere				clrwdt
					goto	WtHere
					bcf		STATUS,RP0		;RAM PAGE 0
delay	movlw	.20
		movwf	s0
del 	sleep
		decfsz	s0
		goto	del
Rci		clrwdt
		btfss	PIR1,RCIF	;Bit5 check for receiced data
		goto	Rci
		movf	RCREG,W		;save received data in w
Posted 18-May-13 11:27am
Edited 18-May-13 12:02pm
Richard MacCutchan at 19-May-13 3:41am
Use your debugger to find out what data is being read. You could also try ReadByte to see what happens, as it may be that your device does not respond in the way you expect.

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

  Print Answers RSS
0 DamithSL 155
1 Afzaal Ahmad Zeeshan 125
2 OriginalGriff 115
3 Richard MacCutchan 100
4 Abhinav S 90
0 OriginalGriff 820
1 Sergey Alexandrovich Kryukov 631
2 Abhinav S 528
3 F-ES Sitecore 420
4 Suvendu Shekhar Giri 365

Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 18 May 2013
Copyright © CodeProject, 1999-2015
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