|
电脑串行端口(serial port) 和单晶片通讯
[复制链接]
|
|
发表于 19-5-2010 05:50 PM
|
显示全部楼层
回复 RudeRabbit
很难回答你必要吗。
看你要怎样的mcu.能符合你要做到的东西就可以了。
用 ...
fritlizt 发表于 19-5-2010 02:10 PM
CCSC 是要给钱的吗?。 |
|
|
|
|
|
|
|
发表于 20-5-2010 11:17 AM
|
显示全部楼层
之前忙着 final exam, 现在拿了cisco networking course, 这project要暂时暂停了,虽然可以 display 在 webpages 了,可是还有很多地方可以再改进 |
|
|
|
|
|
|
|
楼主 |
发表于 25-5-2010 11:34 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 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 编写的:
- #include <stdio.h> /* Standard input/output definitions */
- #include <string.h> /* String function definitions */
- #include <unistd.h> /* UNIX standard function definitions */
- #include <fcntl.h> /* File control definitions */
- #include <errno.h> /* Error number definitions */
- #include <termios.h> /* POSIX terminal control definitions */
- #include <stdlib.h>
- int write_to_port(int fdi,char* const writebuf,const int numBytes);
- int open_port(void);
- int main(void)
- {
- int n;
- int fdi;
- char* bufptr;
- char buffer[255];
- char writebuf[255];
- int i=0;
- fdi=open_port();
- printf("Please enter a string of not more than 250 characters : "); /*request user input*/
- fgets(writebuf, sizeof(writebuf), stdin);
- fflush(stdin);
- tcflush(fdi,TCIOFLUSH); /*flush input and output buffer*/
- write_to_port(fdi,writebuf,250);
- bufptr=buffer; /*read from port*/
- printf("Start reading input...");
- read(fdi,bufptr,250); /*get 250 chars from the port buffer*/
- *(buffer+249)=0;
- printf("The read result: \n%s\n",buffer);
- printf("Closing Serial Port.....");
- close(fdi);
- printf("Port Closed.\n");
- return 0;
- }
- /*
- * 'open_port()' - Open serial port 1.
- * sets all settings here, including baud rate and etc.
- * Returns the file descriptor on success or -1 on error.
- */
- int open_port(void)
- {
- int fd; /* File descriptor for the port */
- struct termios options; /*this is to store the settings*/
- fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);/*set R/W, not controlling terminal, no waiting for DCD*/
- if (fd == -1)
- {
- /* Could not open the port.*/
- perror("open_port: Unable to open /dev/ttyS0 - ");
- }
- else
- {
- fcntl(fd, F_SETFL, 0); /*REPLACE 0 WITH FNDELAY if necessary*/
- tcgetattr(fd,&options); /*get the current settings from fd port*/
- cfsetispeed(&options,B19200); /*input speed*/
- cfsetospeed(&options,B19200); /*output speed*/
- options.c_cflag|= (CLOCAL|CREAD); /*enable receiver and do not change the owner of port to this program, prevent sporadic HUP*/
- /*8N1 settings*/
- options.c_cflag &= ~PARENB; /*disable parity*/
- options.c_cflag &= ~CSTOPB; /*set 1 stop bit*/
- options.c_cflag &= ~CSIZE;
- options.c_cflag |= CS8;
- options.c_cflag &= ~CRTSCTS;
- tcsetattr(fd,TCSAFLUSH,&options); /*apply settings to fd instantly*/
- }
- return (fd);
- }
- int write_to_port(int fdi,char * const writebuf,const int numBytes)
- { /* writebuf points to the string, numBytes specify the number of bytes to output.*/
- int i=0;
- int n;
- while (writebuf[i]) i++; /*detect the position of terminating null*/
- writebuf[i-1]=0; /*replace the terminating newline character by fgets */
- writebuf[i]='\r'; /*required by the normal operation of write()*/
- if ((i+1)>numBytes) /*detect if \r is not in the string to be output*/
- {
- fputs("Error: String longer than number of bytes specified. Write not performed.\n", stderr);
- return 1;
- }
- else
- {
- printf("Ready to send: %s\n",writebuf);
- n = write(fdi, writebuf, numBytes); /*write a string of chars*/
- if (n < 0)
- {
- fputs("write() failed!\n", stderr);
- return 1;
- }
- else
- {
- printf("Write to port success!\n");
- printf("after write to port");
- }
- }
- return 0;
- }
复制代码 问题就是,当我没有把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的。。 |
|
|
|
|
|
|
|
发表于 10-5-2011 04:56 PM
|
显示全部楼层
终于找到答案了: fflush(stdout) after printf() ......还是自己摸索比较好 |
|
|
|
|
|
|
|
发表于 10-5-2011 09:57 PM
|
显示全部楼层
分享我最后finalized 的code ..:
- #include <stdio.h> /* Standard input/output definitions */
- #include <string.h> /* String function definitions */
- #include <unistd.h> /* UNIX standard function definitions */
- #include <fcntl.h> /* File control definitions */
- #include <errno.h> /* Error number definitions */
- #include <termios.h> /* POSIX terminal control definitions */
- #include <stdlib.h>
- int write_to_port(int fdi,char* const writebuf,const int numBytes);
- int open_port(void);
- int main(void)
- {
- int fdi; /*file descriptor*/
- char* bufptr; /*pointer to buffer*/
- char buffer[255]; /*input buffer*/
- char writebuf[255]; /*output/write buffer*/
- fdi=open_port();
- printf("Please enter a string of not more than 250 characters : "); /*request user input*/
- fgets(writebuf, sizeof(writebuf), stdin);
- fflush(stdin);
- tcflush(fdi,TCIOFLUSH); /*flush input and output buffer*/
- write_to_port(fdi,writebuf,250);
- printf("sleeping ...\n");
- sleep(2); /*sleep for 2 seconds ... haha*/
- bufptr=buffer; /*read from port*/
- printf("Start reading input...");fflush(stdout);
- read(fdi,bufptr,250); /*get 250 chars from the port buffer*/
- *(buffer+249)=0;
- printf("The read result: \n%s\n",buffer);
- tcflush(fdi,TCIOFLUSH);
- printf("Closing Serial Port.....");fflush(stdout);
- close(fdi);
- printf("Port Closed.\n");
- return 0;
- }
- /*
- * 'open_port()' - Open serial port 1.
- * sets all settings here, including baud rate and etc.
- * Returns the file descriptor on success or -1 on error.
- */
- int open_port(void)
- {
- int fd; /* File descriptor for the port */
- struct termios options; /*this is to store the settings*/
- fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY /*| O_NDELAY*/);/*set R/W, not controlling terminal, no waiting for DCD*/
- if (fd == -1)
- {
- /* Could not open the port.*/
- perror("open_port: Unable to open /dev/ttyS0 - ");
- }
- else
- {
- fcntl(fd, F_SETFL, 0); /*REPLACE 0 WITH FNDELAY for non-blocking behaviour*/
- tcgetattr(fd,&options); /*get the current settings from fd port*/
- cfsetispeed(&options,B19200); /*input speed*/
- cfsetospeed(&options,B19200); /*output speed*/
- options.c_cflag|= (CLOCAL|CREAD); /*enable receiver and do not change the owner of port to this program, prevent sporadic HUP*/
- /*8N1 settings*/
- options.c_cflag &= ~PARENB; /*disable parity*/
- options.c_cflag &= ~CSTOPB; /*set 1 stop bit*/
- options.c_cflag &= ~CSIZE;
- options.c_cflag |= CS8;
- options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*disable echo and canonical input(use raw instead)*/
- options.c_cflag &= ~CRTSCTS; /*disable hardware flow control*/
- options.c_cc[VMIN]=0; /*VMIN;wait for N characters, here is 0*/
- options.c_cc[VTIME]=20; /*VTIME, time in tenths of a second, to wait for input read if VMIN is zero.*/
- tcsetattr(fd,TCSAFLUSH,&options); /*apply settings to fd instantly*/
- }
- return (fd);
- }
- int write_to_port(int fdi,char * const writebuf,const int numBytes)
- { /* writebuf points to the string, numBytes specify the number of bytes to output.*/
- /*this function only accepts strings returned by fgets()*/
- /*if the the string input is normal null-terminated string, comment out: writebuf[i-1]=0; */
- int i=0;
- int n;
- while (writebuf[i]) i++; /*detect the position of terminating null*/
- writebuf[i-1]=0; /*replace the terminating newline character by fgets */
- writebuf[i]='\r'; /*required by the normal operation of write()*/
- if ((i+1)>numBytes) /*detect if \r is not in the string to be output*/
- {
- fputs("Error: String longer than number of bytes specified. Write not performed.\n", stderr);
- return 1;
- }
- else
- {
- printf("Ready to send: %s\n",writebuf);
- n = write(fdi, writebuf, numBytes); /*write a string of chars*/
- if (n < 0)
- {
- fputs("Write() failed!\n", stderr);
- return 1;
- }
- else
- {
- printf("Write to port success!\n");
- }
- }
- return 0;
- }
复制代码 |
|
|
|
|
|
|
|
发表于 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
|
显示全部楼层
|
|
|
|
|
|
|
发表于 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? |
|
|
|
|
|
|
|
发表于 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;}
}
}
谢了 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|