PIC Vietnam

Go Back   PIC Vietnam > Truyền thông > Giao tiếp cổng COM và LPT

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Tìm Kiếm Bài Trong Ngày Ðánh Dấu Ðã Ðọc Vi điều khiển

Giao tiếp cổng COM và LPT RS232, RS485 và LPT là những giao tiếp cơ bản và kinh điển khi mới học về vi điều khiển...

Trả lời
 
Ðiều Chỉnh Xếp Bài
Old 16-04-2009, 06:39 PM   #1
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
Old 16-04-2009, 07:14 PM   #2
namqn
Trưởng lão PIC bang
 
Tham gia ngày: Feb 2006
Nơi Cư Ngụ: Tp. HCM, Việt Nam
Bài gửi: 3,025
:
Send a message via Yahoo to namqn
Trích:
Nguyên văn bởi tinhgiac_vp View Post
...
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?
Câu hỏi giống như thế này đã được tôi trả lời ở một luồng khác, nhưng tôi sẽ nói lại. Với PIC 8-bit, khi có ngắt nhận dữ liệu từ USART, bạn cần đọc thanh ghi RCREG để giải phóng nó và xóa cờ ngắt RCIF (đây là cách duy nhất để xóa cờ ngắt này). Nếu bạn không thực hiện việc đọc RCREG (đọc vào một biến tạm nào đó nếu không cần dùng giá trị thanh ghi) thì cờ ngắt RCIF sẽ không được xóa, dẫn đến ngắt nhận dữ liệu từ USART liên tục như bạn đã thấy.

Thân,
__________________
Biển học mênh mông, sức người có hạn.

Đang gặp vấn đề cần được giúp đỡ? Hãy dành ra vài phút đọc luồng sau:
http://www.picvietnam.com/forum/showthread.php?t=1263
namqn vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Trả lời

Ðiều Chỉnh
Xếp Bài

Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 07:03 PM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam