Click here to Skip to main content
12,824,787 members (42,345 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# 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()
        {
            InitializeComponent();
            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;
            sp.Open();
        }
       //-----------------------------------
        private void StartButtonClick(object sender, EventArgs e)    
        {
            string str;
                for(i=0;i<32000;i++)
                for (j = 0; j < 32000; j++)
                {
                            str= sp.ReadLine();//example   x y
                            MessageBox.Show(str);
 
                }//i,j
        }

        protected override void OnClosing(CancelEventArgs e)
        {  
            if (sp.IsOpen) sp.Close();
            Application.Exit();
        }

    }
}

list p=16f883   ;,R=DEC  ;Use decimal system
  #include <p16f883.inc>
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'
   movwf   OPTION_REG
    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
              return
;---------------------------------------
Send              movwf   TXREG           ;PicSendMessage data in W
TransWt              bsf     STATUS,RP0      ;RAM PAGE1
WtHere              clrwdt
                    btfss   TXSTA,TRMT      ;(1) TRANSMISSION IS COMPLETE IF HI
                   goto    WtHere
                    bcf     STATUS,RP0      ;RAM PAGE 0
                   return
;---------------------      
delay movlw   .20
       movwf   s0
del     sleep
     decfsz  s0
        goto    del
       return
;********************************************
Rci      clrwdt
        btfss   PIR1,RCIF   ;Bit5 check for receiced data
     goto    Rci
       movf    RCREG,W     ;save received data in w
      return
;===============================    
  END

Posted 18-May-13 12:27pm
EgrK277
Updated 18-May-13 13:02pm
v3
Comments
Richard MacCutchan 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 18 May 2013
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