佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

楼主: fritlizt

电脑串行端口(serial port) 和单晶片通讯

  [复制链接]
发表于 19-5-2010 05:50 PM | 显示全部楼层
回复  RudeRabbit


   

很难回答你必要吗。
看你要怎样的mcu.能符合你要做到的东西就可以了。
用 ...
fritlizt 发表于 19-5-2010 02:10 PM


CCSC 是要给钱的吗?。
回复

使用道具 举报


ADVERTISEMENT

发表于 20-5-2010 11:17 AM | 显示全部楼层
之前忙着 final exam, 现在拿了cisco networking course, 这project要暂时暂停了,虽然可以 display 在 webpages 了,可是还有很多地方可以再改进
回复

使用道具 举报

 楼主| 发表于 25-5-2010 11:34 PM | 显示全部楼层
回复 82# xkore


   
嗯。。。楼主加油。
回复

使用道具 举报

发表于 15-10-2010 09:33 PM | 显示全部楼层
先从简单的loopback 程序开始。

这个是这个贴的example program可以下载, 自行修改
http://www.mediaf ...
fritlizt 发表于 7-8-2009 11:22 PM

版主少了 TRY Catch Statement 哦。

可惜 SerialPort 的 Library 在
WPF 之下不能直接拉了放进Form, 只有自己手动引用,非常可惜。

不过多谢分享。
回复

使用道具 举报

发表于 10-5-2011 04:13 PM | 显示全部楼层
本帖最后由 电子达人 于 10-5-2011 04:23 PM 编辑

我刚用C写read/write serial port 的Linux console application,测试loopback ... 虽然是可以用,但是有小小的问题,搞了老半天还是解不开的问题。下面的code,是根据http://www.easysw.com/~mike/serial/serial.html 编写的:

  1.    #include <stdio.h>   /* Standard input/output definitions */
  2.     #include <string.h>  /* String function definitions */
  3.     #include <unistd.h>  /* UNIX standard function definitions */
  4.     #include <fcntl.h>   /* File control definitions */
  5.     #include <errno.h>   /* Error number definitions */
  6.     #include <termios.h> /* POSIX terminal control definitions */
  7.     #include <stdlib.h>

  8.     int write_to_port(int fdi,char* const writebuf,const int numBytes);
  9.     int open_port(void);



  10.     int main(void)
  11.     {
  12.         int n;
  13.         int fdi;
  14.         char* bufptr;
  15.         char buffer[255];
  16.         char writebuf[255];
  17.         int i=0;


  18.         fdi=open_port();

  19.         printf("Please enter a string of not more than 250 characters : "); /*request user input*/
  20.         fgets(writebuf, sizeof(writebuf), stdin);
  21.         fflush(stdin);
  22.         tcflush(fdi,TCIOFLUSH);  /*flush input and output buffer*/
  23.         write_to_port(fdi,writebuf,250);


  24.         bufptr=buffer;              /*read from port*/
  25.         printf("Start reading input...");
  26.         read(fdi,bufptr,250);      /*get 250 chars from the port buffer*/
  27.         *(buffer+249)=0;
  28.         printf("The read result: \n%s\n",buffer);
  29.         printf("Closing Serial Port.....");
  30.         close(fdi);
  31.         printf("Port Closed.\n");

  32.         return 0;
  33.     }
  34.     /*
  35.      * 'open_port()' - Open serial port 1.
  36.      * sets all settings here, including baud rate and etc.
  37.      * Returns the file descriptor on success or -1 on error.
  38.      */
  39.     int open_port(void)
  40.     {
  41.         int fd; /* File descriptor for the port */
  42.         struct termios options; /*this is to store the settings*/

  43.         fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);/*set R/W, not controlling terminal, no waiting for DCD*/
  44.         if (fd == -1)
  45.         {
  46.         /* Could not open the port.*/
  47.             perror("open_port: Unable to open /dev/ttyS0 - ");
  48.         }
  49.         else
  50.         {
  51.             fcntl(fd, F_SETFL, 0); /*REPLACE 0 WITH FNDELAY if necessary*/
  52.             tcgetattr(fd,&options); /*get the current settings from fd port*/

  53.             cfsetispeed(&options,B19200); /*input speed*/
  54.             cfsetospeed(&options,B19200);  /*output speed*/
  55.             options.c_cflag|= (CLOCAL|CREAD); /*enable receiver and do not change the owner of port to this program, prevent sporadic HUP*/
  56.             /*8N1 settings*/
  57.             options.c_cflag &= ~PARENB; /*disable parity*/
  58.             options.c_cflag &= ~CSTOPB; /*set 1 stop bit*/
  59.             options.c_cflag &= ~CSIZE;
  60.             options.c_cflag |= CS8;
  61.             options.c_cflag &= ~CRTSCTS;

  62.             tcsetattr(fd,TCSAFLUSH,&options); /*apply settings to fd instantly*/
  63.         }
  64.       return (fd);
  65.     }

  66.     int write_to_port(int fdi,char * const writebuf,const int numBytes)
  67.     {   /* writebuf points to the string, numBytes specify the number of bytes to output.*/
  68.         int i=0;
  69.         int n;

  70.         while (writebuf[i]) i++; /*detect the position of terminating null*/
  71.         writebuf[i-1]=0; /*replace the terminating newline character by fgets */
  72.         writebuf[i]='\r';   /*required by the normal operation of write()*/

  73.         if ((i+1)>numBytes)  /*detect if \r is not in the string to be output*/
  74.         {
  75.             fputs("Error: String longer than number of bytes specified. Write not performed.\n", stderr);
  76.             return 1;
  77.         }
  78.         else
  79.         {
  80.             printf("Ready to send: %s\n",writebuf);
  81.             n = write(fdi, writebuf, numBytes);      /*write a string of chars*/
  82.             if (n < 0)
  83.             {
  84.                 fputs("write() failed!\n", stderr);
  85.                 return 1;
  86.             }
  87.             else
  88.             {
  89.                 printf("Write to port success!\n");
  90.                 printf("after write to port");
  91.             }
  92.         }

  93.         return 0;
  94.     }

复制代码
问题就是,当我没有把TX 和RX SHORT 在一起时,program 只能run到 write_to_port() 的                printf("Write to port success!\n"); ,接下来的printf("after write to port"); 和 printf("Start reading input..."); 都不能 run....为什么呢?照理来说,应该是run到read(fdi,bufptr,250);  才停的吧。。。真是不明白!!
回复

使用道具 举报

发表于 10-5-2011 04:20 PM | 显示全部楼层
本帖最后由 电子达人 于 10-5-2011 04:53 PM 编辑

把printf("after write to port " ); 和 printf("Start reading input... " );  改成printf("after write to port\n " ); 和 printf("Start reading input...\n " );  终于发现原来program 真的停在read() 那边。。。。。。。

有什么方法能让printf 不需要 \n 就可以output to stdout stream ?我只知道,C++ 是用endl的。。
回复

使用道具 举报

Follow Us
发表于 10-5-2011 04:56 PM | 显示全部楼层
终于找到答案了: fflush(stdout) after printf() ......还是自己摸索比较好
回复

使用道具 举报

发表于 10-5-2011 09:57 PM | 显示全部楼层
分享我最后finalized 的code ..:

  1.     #include <stdio.h>   /* Standard input/output definitions */
  2.     #include <string.h>  /* String function definitions */
  3.     #include <unistd.h>  /* UNIX standard function definitions */
  4.     #include <fcntl.h>   /* File control definitions */
  5.     #include <errno.h>   /* Error number definitions */
  6.     #include <termios.h> /* POSIX terminal control definitions */
  7.     #include <stdlib.h>

  8.     int write_to_port(int fdi,char* const writebuf,const int numBytes);
  9.     int open_port(void);


  10.     int main(void)
  11.     {
  12.         int fdi;          /*file descriptor*/
  13.         char* bufptr;     /*pointer to buffer*/
  14.         char buffer[255]; /*input buffer*/
  15.         char writebuf[255];  /*output/write buffer*/

  16.         fdi=open_port();

  17.         printf("Please enter a string of not more than 250 characters : "); /*request user input*/
  18.         fgets(writebuf, sizeof(writebuf), stdin);
  19.         fflush(stdin);
  20.         tcflush(fdi,TCIOFLUSH);  /*flush input and output buffer*/
  21.         write_to_port(fdi,writebuf,250);

  22.         printf("sleeping ...\n");
  23.         sleep(2); /*sleep for 2 seconds ... haha*/

  24.         bufptr=buffer;              /*read from port*/
  25.         printf("Start reading input...");fflush(stdout);
  26.         read(fdi,bufptr,250);      /*get 250 chars from the port buffer*/
  27.         *(buffer+249)=0;
  28.         printf("The read result: \n%s\n",buffer);

  29.         tcflush(fdi,TCIOFLUSH);

  30.         printf("Closing Serial Port.....");fflush(stdout);
  31.         close(fdi);
  32.         printf("Port Closed.\n");

  33.         return 0;
  34.     }
  35.     /*
  36.      * 'open_port()' - Open serial port 1.
  37.      * sets all settings here, including baud rate and etc.
  38.      * Returns the file descriptor on success or -1 on error.
  39.      */
  40.     int open_port(void)
  41.     {
  42.         int fd; /* File descriptor for the port */
  43.         struct termios options; /*this is to store the settings*/

  44.         fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY /*| O_NDELAY*/);/*set R/W, not controlling terminal, no waiting for DCD*/
  45.         if (fd == -1)
  46.         {
  47.         /* Could not open the port.*/
  48.             perror("open_port: Unable to open /dev/ttyS0 - ");
  49.         }
  50.         else
  51.         {
  52.             fcntl(fd, F_SETFL, 0); /*REPLACE 0 WITH FNDELAY for non-blocking behaviour*/
  53.             tcgetattr(fd,&options); /*get the current settings from fd port*/

  54.             cfsetispeed(&options,B19200); /*input speed*/
  55.             cfsetospeed(&options,B19200);  /*output speed*/
  56.             options.c_cflag|= (CLOCAL|CREAD); /*enable receiver and do not change the owner of port to this program, prevent sporadic HUP*/
  57.             /*8N1 settings*/
  58.             options.c_cflag &= ~PARENB; /*disable parity*/
  59.             options.c_cflag &= ~CSTOPB; /*set 1 stop bit*/
  60.             options.c_cflag &= ~CSIZE;
  61.             options.c_cflag |= CS8;

  62.             options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*disable echo and canonical input(use raw instead)*/
  63.             options.c_cflag &= ~CRTSCTS; /*disable hardware flow control*/
  64.             options.c_cc[VMIN]=0; /*VMIN;wait for N characters, here is 0*/
  65.             options.c_cc[VTIME]=20; /*VTIME, time in tenths of a second, to wait for input read if VMIN is zero.*/

  66.             tcsetattr(fd,TCSAFLUSH,&options); /*apply settings to fd instantly*/
  67.         }
  68.       return (fd);
  69.     }

  70.     int write_to_port(int fdi,char * const writebuf,const int numBytes)
  71.     {   /* writebuf points to the string, numBytes specify the number of bytes to output.*/
  72.         /*this function only accepts strings returned by fgets()*/
  73.         /*if the the string input is normal null-terminated string, comment out: writebuf[i-1]=0; */
  74.         int i=0;
  75.         int n;

  76.         while (writebuf[i]) i++; /*detect the position of terminating null*/
  77.         writebuf[i-1]=0; /*replace the terminating newline character by fgets */
  78.         writebuf[i]='\r';   /*required by the normal operation of write()*/

  79.         if ((i+1)>numBytes)  /*detect if \r is not in the string to be output*/
  80.         {
  81.             fputs("Error: String longer than number of bytes specified. Write not performed.\n", stderr);
  82.             return 1;
  83.         }
  84.         else
  85.         {
  86.             printf("Ready to send: %s\n",writebuf);
  87.             n = write(fdi, writebuf, numBytes);      /*write a string of chars*/
  88.             if (n < 0)
  89.             {
  90.                 fputs("Write() failed!\n", stderr);
  91.                 return 1;
  92.             }
  93.             else
  94.             {
  95.                 printf("Write to port success!\n");
  96.             }
  97.         }

  98.         return 0;
  99.     }

复制代码
回复

使用道具 举报


ADVERTISEMENT

发表于 11-5-2011 11:13 AM | 显示全部楼层
分享我最后finalized 的code ..:
电子达人 发表于 10-5-2011 09:57 PM



回复你一下,谢谢分享
回复

使用道具 举报

发表于 12-5-2011 03:55 PM | 显示全部楼层
*我不是一个职业programmer,会的都是以前和senior学的, 再加上自修得到的结果。有任何错误可以直接点出来, 以免我误人子弟。
fritlizt 发表于 7-8-2009 10:35 PM


放心,我敢保证你不会误人子弟的。
回复

使用道具 举报

发表于 28-5-2011 07:53 PM | 显示全部楼层
用pl2303....
回复

使用道具 举报

发表于 28-5-2011 11:44 PM | 显示全部楼层
用pl2303....
dfamd 发表于 28-5-2011 07:53 PM


你在用PL2303?
如果你愿意试新东西, 介绍你 Microchip 的 MCP2200.
   
回复

使用道具 举报

发表于 29-5-2011 12:01 AM | 显示全部楼层
回复 92# pic


   我不会也,只是说说,看到很多人都用pl2303
   哈哈。。

pl2303和mcp2200什么分别呢?
回复

使用道具 举报

发表于 29-5-2011 12:15 AM | 显示全部楼层
我不会也,只是说说,看到很多人都用pl2303
   哈哈。。

pl2303和mcp2200什么分别呢?
dfamd 发表于 29-5-2011 12:01 AM

学习, 如果是知其然, 不知所以然的话, 很难进步的。
你看, 我一问, 你就倒了。。

MCP2200 比PL2303 好, 他还含8 个I/O 。
MCP2200 有ID, 所以你的COM port 的号码不会因为插入不同的usb port 而改变。

PL2303 是廉价。。而且被大量的使用在USB 《--》 RS232 转换器。
回复

使用道具 举报

发表于 29-5-2011 12:33 AM | 显示全部楼层
回复 94# pic


    ic ic...

没办法,我这人很容易丧志的
回复

使用道具 举报

发表于 31-5-2011 11:29 PM | 显示全部楼层
回复 94# pic


    MCP2200要不要另外加个MAX232?
回复

使用道具 举报


ADVERTISEMENT

发表于 1-6-2011 01:52 AM | 显示全部楼层
回复  pic


    MCP2200要不要另外加个MAX232?
dfamd 发表于 31-5-2011 11:29 PM

看你的应用啊。。
如果是MCU USART--》 MCP2200  --》 USB  --》PC

如果是要RS232
RS232  --》MAX232 ---》 MCP2200  --》 USB  --》PC
回复

使用道具 举报

发表于 1-6-2011 11:01 AM | 显示全部楼层
回复 97# pic


  关于RS232

可以只单单用MAX232,不和MCP2200或PL2303一起用的吗?

RS232  --》MAX232 ---》USB  --》PC
回复

使用道具 举报

发表于 1-6-2011 10:30 PM | 显示全部楼层
回复  pic
  关于RS232

可以只单单用MAX232,不和MCP2200或PL2303一起用的吗?

RS232  --》MAX ...
dfamd 发表于 1-6-2011 11:01 AM

要用USB 的话, 一定要有类似MCP2200或PL2303的 IC (还有FTDI chip, 其他的内建USB controller的MCU 等等)
回复

使用道具 举报

发表于 6-12-2011 09:54 PM | 显示全部楼层
楼主,看到你放的例子是ccs的,楼主会mplab的吗?
因为我只会mplab,ccs我不是很会用。
我现在做着project,sensor被启动后,alarm会响,同时间电脑的hyperterminal会出现Help这个字。我现在不会写programming for microcontroller to Computer。
这是我的code给我的alarm system,就差serial去电脑那里而已.
#include <pic.h>
__CONFIG(0x3F32);

#define sensor RA0
#define sw RA1
#define buzzer RC5
#define led RC4

void main (void)
{
ADCON1 = 0x06;
TRISA=0b11111111;
TRISC=0b00000000;

sensor=0;
sw=0;
buzzer=0;
led=0;

while(1)
{
        if ((sensor == 0) && (sw ==0))
                {        led=1,buzzer=1;}
                else if ((sensor == 0) && (sw ==1))
                        {        led=0,buzzer=0;}
                else {led =0,buzzer =0;}
}
}
谢了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 28-3-2024 09:50 PM , Processed in 0.067742 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表