View Single Post
Old 16-04-2009, 06:39 PM   #41
tinhgiac_vp
Đệ tử 2 túi
 
Tham gia ngày: Oct 2007
Bài gửi: 42
:
Em đang làm giao tiếp RS232 sử dụng C#. Nhưng chương trình không xử lý lệnh serialport1.close(). Mong mọi người chỉ giáo.
Đây là code C#.
Code:
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace viduRS232
{
    public partial class Form1 : Form
    {
        public string s;
        public ArrayList arr = new ArrayList();
        public int id;
        public Form1()
        {
            InitializeComponent();
            id = 0;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            serialPort1.Close();
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            this.Invoke(new EventHandler(display));
        }
        private void display(object sender, EventArgs e)
        {
            int flag;
            int c; 
            c = serialPort1.ReadChar();
            //string s1 = "";
            s = "";
            flag = 0;
            if (c == 's')
            {
                flag = 1;
                while (flag == 1)
                {
                    c =serialPort1.ReadChar();
                    if (c == 'S')
                    {
                        //textBoxdisplay.Text = s1;
                        flag = 0;
                    }
                    else
                        s += Convert.ToChar(c);
                }
                arr.Add(s);
                id++;
                if (id == 2)
                {
                    serialPort1.Write("S");
                }
                //serialPort1.Write("s");
                
                //this.textBoxdisplay.Text = s;
            }
        }

        private void buttonRead_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            serialPort1.DataReceived +=new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
            serialPort1.Write("s");
            //id = 1;
            //while (id == 1) ;
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            //this.Dispose();
            //this.serialPort1.Close();
            this.Close();
        }

        private void buttondisplay_Click(object sender, EventArgs e)
        {
            this.textBoxdisplay.Text = Convert.ToString(arr[0]);
            this.textBox1.Text = Convert.ToString(arr[1]);
            this.textBox2.Text = Convert.ToString(id);
            //serialPort1.Write("s");
            this.serialPort1.Close();
        }
    }
}
Còn đây là code PIC
Code:
#include <18f4331.h>
#device *=16 ADC=8
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP
#use delay(clock=20000000)
#use rs232(baud = 9600,parity = n,xmit = pin_C6,rcv=pin_C7)
#include <LMB162A.c>
int flag;
void lcd_putstr(char *buffer);
#int_rda
void ngat_rs232()
{
   if(flag==0)
   {
      printf("sxyzS");
   }
   if(flag==1)
   {
      printf("sabcS");
   }
   if(flag==2)
   {
      printf("s123S");
   }
   if(flag==3)
   {
      printf("s456S");
   }
   putc('\0');
   flag++;
}
void main()
{
   int id;
   char c;
   char s[] = "hello";
   //char s1[] ="s123456789S";
   //char s2[] = "s987654321";
   enable_interrupts(global);
   enable_interrupts(int_rda);
   delay_ms(50);
   lcd_init();
   delay_ms(10);
   printf(lcd_putc,"hello");
   flag = 0;
   //while(flag==0)
   //{
     // printf("s123456S");
   //}
   //lcd_putc('\f');
   id = 1;
   while(id==1)
   {
      c = getc();
      if(c=='S')
      {
         id = 0;
         //sable_interrupts(int_rda);
      }
   }
   //able_interrupts(int_timer1);
   printf(lcd_putc,"xong");
}
void lcd_putstr(char *buffer)
{
    unsigned int i = 0;
    while (buffer[i] != '\0')
    {
        lcd_putc(buffer[i]); /* calling another function */
                                /* to write each char to the lcd module */
        i++;
        delay_us(10);
        
    }
}
Cho em hỏi thêm là hàm ngắt RDA hình như ngắt liên tục không dừng. Em disable_interrupts(int_rda) thì nó không chạy được hàm lcd_putc(), hình như nó tắt timer gì đó. Các cao thủ giải thích vấn đề này giúp em với, và khắc phục nó như thế nào?
tinhgiac_vp vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn