串口通信的问题

九个太阳2023 2010-07-16 01:21:06
在网上修改了串口通信的代码,编译正确,打开串口和参数设置都是正确的,但是就是在发送数据的时候出现问题,板子发送的数据PC收不到,PC发送的数据板子也收不到,不知道是什么原因,详情各位大哥看看~~~
附上我的主要代码:

/***@brief 设置串口通信速率
*@param fd 类型 int 打开串口的文件句柄
*@param speed 类型 int 串口速度
*@return void*/

int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300,
38400, 19200, 9600, 4800, 2400, 1200, 300, };
void set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for(i= 0;i<sizeof(speed_arr)/sizeof(int);i++)
{
if(speed == name_arr[i])
{
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if(status != 0)
perror("tcsetattr fd1");
return;
}
tcflush(fd,TCIOFLUSH);
}
}


/**
*@brief 设置串口数据位,停止位和效验位
*@param fd 类型 int 打开的串口文件句柄*
*@param databits 类型 int 数据位 取值 为 7 或者8*
*@param stopbits 类型 int 停止位 取值为 1 或者2*
*@param parity 类型 int 效验类型 取值为N,E,O,,S
*/
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;

if(tcgetattr(fd,&options)!= 0)
{
printf("SetupSerial 1");
return(FALSE);
}

options.c_cflag &= ~CSIZE;
switch (databits) /*设置数据位数*/
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return (FALSE);
}

switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 转换为偶效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'S':
case 's': /*as no parity*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity\n");
return (FALSE);
}

/* 设置停止位*/
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return (FALSE);
}

/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;

options.c_cc[VTIME] = 150; // 15 seconds
options.c_cc[VMIN] = 0;

tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
printf("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}


/**
*@breif 打开串口
*/
int OpenDev(char *Dev)
{
int fd = open(Dev,O_RDWR | O_NOCTTY); //| O_NOCTTY | O_NDELAY
if (-1 == fd)
{
printf("Can't Open Serial Port");
return -1;
}
return fd;
}


/**
*@breif main()
*/
int main(int argc, char **argv)
{
int fd;
int nread;
int nRet = 0;
char buff[512] = {"this is Serial Port Test!\r\n"};
//char buff[512];
char *dev ="/dev/ttyS0";

fd = OpenDev(dev);
if (fd>0)
{
printf("Open Serial Port Successful!\n");
set_speed(fd,19200);
printf("set_speed Successful!\n");
}
else
{
printf("Can't Open Serial Port!\n");
exit(0);
}

if (set_Parity(fd,8,1,'N')== FALSE)
{
printf("Set Parity Error\n");
exit(1);
}

printf("Set Parity Successful!\n");

#if 1
//while(1)
//{
nRet = write(fd,buff,512);
if(-1 == nRet)
{
printf("Write Error!\n");
}
printf("Write Successful!\n");
//}
#endif

#if 0
while(1)
{
while((nread = read(fd,buff,512))>0)
{
printf("\nLen %d\n",nread);
buff[nread+1]='\0';
printf("\n%s",buff);
}
}
#endif
nRet = close(fd);
if(-1 == nRet)
{
printf("Close Port Error!\n");
}
printf("Close Port Successful!\n");
}

...全文
696 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
kulleng 2012-11-10
  • 打赏
  • 举报
回复
怎么这个问题一直没有解决吗?我现在也是用ompL138,但是ttyS0,无法发出数据,ttyS1可以。
Jocerly 2011-05-17
  • 打赏
  • 举报
回复
1、判断你的串口线是否正确
2、判断你所用的串口设备是否正确
3、判断你的板子和PC之间的串口设置是否一致
还不行的话,建议用示波器进行判断:
当板子发送数据时,用示波器看看串口线上是否有波形。
九个太阳2023 2010-11-01
  • 打赏
  • 举报
回复
[Quote=引用 30 楼 kuangmengjie 的回复:]

我的是dm355的板子,不过这个问题我也解决了是串口线没有交叉。但是现在又有个问题就是,串口每次只能接收8个字节,速度特别的慢!不知道是什么原因!我想问一下用串口能满足视频的传输吗?
[/Quote]
串口基本满足不了你的视频传输的要求,视频传输一般用网络比较好
kuangmengjie 2010-11-01
  • 打赏
  • 举报
回复
我的是dm355的板子,不过这个问题我也解决了是串口线没有交叉。但是现在又有个问题就是,串口每次只能接收8个字节,速度特别的慢!不知道是什么原因!我想问一下用串口能满足视频的传输吗?
九个太阳2023 2010-10-22
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 kuangmengjie 的回复:]

版主,我也遇到了 和你一样的问题!请问你解决了没有啊~
也是打开和设置串口都没问题
但是发送后收端收不到
我用两个电脑连起来试了下,在Windows下用串口助手也收不到,不知道是什么原因
发端显示发送了,但是收端没有反应。而且线应该没问题
[/Quote]
你把你的硬件平台说下,我的是因为串口驱动的问题
kuangmengjie 2010-10-21
  • 打赏
  • 举报
回复
版主,我也遇到了 和你一样的问题!请问你解决了没有啊~
也是打开和设置串口都没问题
但是发送后收端收不到
我用两个电脑连起来试了下,在Windows下用串口助手也收不到,不知道是什么原因
发端显示发送了,但是收端没有反应。而且线应该没问题
黑呼乎 2010-08-06
  • 打赏
  • 举报
回复
学习 学习了
tobypy 2010-08-04
  • 打赏
  • 举报
回复
不懂,帮你顶
wwwunix 2010-08-04
  • 打赏
  • 举报
回复
重点查查串口驱动中的初始化部分,看看你要用的串口的配置寄存器是否正确。或者你的驱动中只配置了UART0和UART1,而你却要用到UART3。
九个太阳2023 2010-08-04
  • 打赏
  • 举报
回复
顶起来,问题还是没有解决·····
九个太阳2023 2010-07-30
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 garefield 的回复:]
int set_Parity(int ai_com,uchar ai_databits,uchar ai_stopbits,uchar ai_parity)
{
struct termios options;
if ( tcgetattr( ai_com,&options) != 0)
{
return ERROR_LOAD_COMINFO;
}
op……
[/Quote]
万分感谢~~我试试先~
garefield 2010-07-30
  • 打赏
  • 举报
回复
int set_Parity(int ai_com,uchar ai_databits,uchar ai_stopbits,uchar ai_parity)
{
struct termios options;
if ( tcgetattr( ai_com,&options) != 0)
{
return ERROR_LOAD_COMINFO;
}
options.c_cflag |= CLOCAL | CREAD;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;
options.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
/*options.c_cflag &= ~CSIZE;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST; */

switch (ai_databits) /*设置数据位数*/
{
case 5:
options.c_cflag |= CS5;
break;
case 6:
options.c_cflag |= CS6;
break;
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
return ERROR_DATABITS;
}
switch (ai_parity)
{
/*case 'n':
case 'N':*/
case 1:
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
/*case 'o':
case 'O':*/
case 2:
options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
/*case 'e':
case 'E':*/
case 3:
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 转换为偶效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
/*case 'S':
case 's': /*as no parity*/
case 4:
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
return ERROR_PARITY;
}
/* 设置停止位*/
switch (ai_stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
return ERROR_STOPBITS;
}

/* Set input parity option */
/* if (ai_parity != 'n')*/
if (ai_parity != 1)
options.c_iflag |= INPCK;
tcflush(ai_com,TCIFLUSH);
options.c_cc[VTIME] = 0; /* 设置超时15 seconds*/
options.c_cc[VMIN] = 1; /* define the minimum bytes data to be readed*/
if (tcsetattr(ai_com,TCSANOW,&options) != 0)
{
/*printf("set_Parity error\n");*/
return (FALSE);
}
/*printf("set_Parity success\n");*/
return (TRUE);
}
这是我的set_Parity,用在modem拨号程序上,做了些修改以对应数据库里的参数,大体上和你用的差不多
九个太阳2023 2010-07-29
  • 打赏
  • 举报
回复
顶起来~~再看串口驱动~~感觉好乱~~
九个太阳2023 2010-07-29
  • 打赏
  • 举报
回复
Serial: 8250/16550 driver, 3 ports, IRQ sharing enabled
serial8250.0: ttyS0 at MMIO 0x1c42000 (irq = 25) is a 16550A
serial8250.0: ttyS1 at MMIO 0x1d0c000 (irq = 53) is a 16550A
serial8250.0: ttyS2 at MMIO 0x1d0d000 (irq = 61) is a 16550A
console [ttyS2] enabled
这些是有关串口的信息
九个太阳2023 2010-07-29
  • 打赏
  • 举报
回复
附上我的启动信息,大家看看哪里出现了问题:
Linux......................................................................................................................... done, booting the kernel.
Linux version 2.6.32-rc6 (root@lihao-desktop) (gcc version 4.3.3 (Sourcery G++ Lite 2009q1-203) ) #1 PREEMPT Thu Jul 29 16:15:15 CST 2010
CPU: ARM926EJ-S [41069265] revision 5 (ARMv5TEJ), cr=00053177
CPU: VIVT data cache, VIVT instruction cache
Machine: DaVinci DA850/OMAP-L138 EVM
Memory policy: ECC disabled, Data cache writeback
DaVinci da850/omap-l138 variant 0x0
Built 1 zonelists in Zone order, mobility grouping on. Total pages: 16256
Kernel command line: mem=64M console=ttyS2,115200n8 root=/dev/nfs nfsroot=192.168.0.200:/opt/workspace/SEED-DEC138/nfs rw ip=192.168.0.201:192.168.0.1:255.255.255.0
PID hash table entries: 256 (order: -2, 1024 bytes)
Dentry cache hash table entries: 8192 (order: 3, 32768 bytes)
Inode-cache hash table entries: 4096 (order: 2, 16384 bytes)
Memory: 64MB = 64MB total
Memory: 60980KB available (3476K code, 269K data, 144K init, 0K highmem)
SLUB: Genslabs=11, HWalign=32, Order=0-3, MinObjects=0, CPUs=1, Nodes=1
Hierarchical RCU implementation.
NR_IRQS:245
Console: colour dummy device 80x30
Calibrating delay loop... 149.50 BogoMIPS (lpj=747520)
Mount-cache hash table entries: 512
CPU: Testing write buffer coherency: ok
DaVinci: 144 gpio irqs
NET: Registered protocol family 16
da850_lcd_hw_init++++++++++++++++++++++++++++++++
da850_mtd_nand_init++++++++++++++++++++++++++++++++
bio: create slab <bio-0> at 0
SCSI subsystem initialized
usbcore: registered new interface driver usbfs
usbcore: registered new interface driver hub
usbcore: registered new device driver usb
Switching to clocksource timer0_1
musb_hdrc: version 6.0, cppi4.1-dma, host, debug=0
Waiting for USB PHY clock good...
musb_hdrc: USB Host mode controller at fee00000 using DMA, IRQ 58
musb_hdrc musb_hdrc: MUSB HDRC host driver
musb_hdrc musb_hdrc: new USB bus registered, assigned bus number 1
usb usb1: configuration #1 chosen from 1 choice
hub 1-0:1.0: USB hub found
hub 1-0:1.0: 1 port detected
NET: Registered protocol family 2
IP route cache hash table entries: 1024 (order: 0, 4096 bytes)
TCP established hash table entries: 2048 (order: 2, 16384 bytes)
TCP bind hash table entries: 2048 (order: 1, 8192 bytes)
TCP: Hash tables configured (established 2048 bind 2048)
TCP reno registered
NET: Registered protocol family 1
RPC: Registered udp transport module.
RPC: Registered tcp transport module.
RPC: Registered tcp NFSv4.1 backchannel transport module.
EMAC: RMII PHY configured, MII PHY will not be functional
McBSP:Probed McBSP1
JFFS2 version 2.2. (NAND) 漏 2001-2006 Red Hat, Inc.
msgmni has been set to 119
io scheduler noop registered
io scheduler anticipatory registered (default)
da8xx_lcdc da8xx_lcdc.0: GLCD: Found INNOLUX_AT050TN22 panel
Console: switching to colour frame buffer device 100x30
Serial: 8250/16550 driver, 3 ports, IRQ sharing enabled
serial8250.0: ttyS0 at MMIO 0x1c42000 (irq = 25) is a 16550A
serial8250.0: ttyS1 at MMIO 0x1d0c000 (irq = 53) is a 16550A
serial8250.0: ttyS2 at MMIO 0x1d0d000 (irq = 61) is a 16550A
console [ttyS2] enabled
brd: module loaded
ahci ahci: forcing PORTS_IMPL to 0x1
ahci ahci: AHCI 0001.0100 32 slots 1 ports 3 Gbps 0x1 impl SATA mode
ahci ahci: flags: ncq sntf pm led clo only pmp pio slum part ccc
scsi0 : ahci
ata1: SATA max UDMA/133 irq 67
NAND device: Manufacturer ID: 0xec, Chip ID: 0xdc (Samsung NAND 512MiB 3,3V 8-bit)
NAND_ECC_NONE selected by board driver. This is not recommended !!
Bad block table not found for chip 0
Bad block table not found for chip 0
Scanning device for bad blocks
Bad eraseblock 1456 at 0x00000b600000
Bad eraseblock 1680 at 0x00000d200000
Bad eraseblock 1776 at 0x00000de00000
Bad eraseblock 1966 at 0x00000f5c0000
Bad eraseblock 2038 at 0x00000fec0000
Bad eraseblock 3108 at 0x000018480000
Creating 6 MTD partitions on "davinci_nand.1":
0x000000000000-0x000000020000 : "u-boot env"
0x000000020000-0x000000040000 : "UBL"
0x000000040000-0x0000000c0000 : "u-boot"
0x000000300000-0x000000500000 : "kernel"
0x000000500000-0x000002500000 : "filesystem"
0x000002500000-0x000020000000 : "user"
davinci_nand davinci_nand.1: controller rev. 2.5
davinci SPI Controller driver at 0xfef0e000 (irq = 56) use_dma=1
console [netcon0] enabled
netconsole: network logging started
ohci_hcd: USB 1.1 'Open' Host Controller (OHCI) Driver
ohci ohci.0: DA8xx OHCI
ohci ohci.0: new USB bus registered, assigned bus number 2
ohci ohci.0: irq 59, io mem 0x01e25000
usb usb2: configuration #1 chosen from 1 choice
hub 2-0:1.0: USB hub found
hub 2-0:1.0: 1 port detected
Initializing USB Mass Storage driver...
usbcore: registered new interface driver usb-storage
USB Mass Storage support registered.
mice: PS/2 mouse device common for all mice
ads7846 spi1.0: touchscreen, irq 200
input: ADS7846 Touchscreen as /devices/platform/dm_spi.1/spi1.0/input/input0
omap_rtc omap_rtc: rtc core: registered omap_rtc as rtc0
omap_rtc: RTC power up reset detected
omap_rtc: already running
i2c /dev entries driver
watchdog watchdog: heartbeat 60 sec
cpuidle: using governor ladder
cpuidle: using governor menu
davinci_mmc davinci_mmc.0: Using DMA, 4-bit mode
usbcore: registered new interface driver usbhid
usbhid: v2.6:USB HID core driver
TCP cubic registered
NET: Registered protocol family 17
Clocks: disable unused emac
Clocks: disable unused mcasp
davinci_emac_probe: using random MAC addr: 4e:e6:15:55:b8:76
emac-mii: probed
omap_rtc omap_rtc: setting system clock to 2000-01-02 09:15:50 UTC (946804550)
ata1: SATA link down (SStatus 0 SControl 300)
eth0: attached PHY driver [Generic PHY] (mii_bus:phy_addr=1:00, id=22161a)
IP-Config: Guessing netmask 255.255.255.0
IP-Config: Gateway not on directly connected network.
Looking up port of RPC 100003/2 on 192.168.0.200
PHY: 1:00 - Link is Up - 100/Full
Looking up port of RPC 100005/1 on 192.168.0.200
VFS: Mounted root (nfs filesystem) on device 0:13.
Freeing init memory: 144K

INIT: version 2.86 booting

Please wait: booting...
Starting udev
udev: starting version 141
Remounting root file system...
WARNING: Couldn't open directory /lib/modules/2.6.32-rc6: No such file or directory
FATAL: Could not open /lib/modules/2.6.32-rc6/modules.dep.temp for writing: No such file or directory
modprobe: FATAL: Could not load /lib/modules/2.6.32-rc6/modules.dep: No such file or directory


modprobe: FATAL: Could not load /lib/modules/2.6.32-rc6/modules.dep: No such file or directory


root: mount: mounting rootfs on / failed: No such file or directory
Setting up IP spoofing protection: rp_filter.
Configuring network interfaces... done.

INIT: Entering runlevel: 5

Starting telnet daemon.
modprobe: FATAL: Could not load /lib/modules/2.6.32-rc6/modules.dep: No such file or directory


Starting syslogd/klogd: done
Starting thttpd.

******** ********** ********** ********
** ** ** ** ** ** ** **
** ** ** ** ** ** **
**** ****** ****** ** **
** ** ** ** ** ** **
** ** ** ** **
** ** ** ** ** ** ** **
******** ********** ********** ********

SEED http://www.seeddsp.com seed ttyS2

SEED 2009.11 seed ttyS2

seed login: root
九个太阳2023 2010-07-29
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 garefield 的回复:]
我也用过这段代码,也是无法通信,后来网上查了下,在set_Parity的设置options的设置改成如下就可以了
options.c_cflag |= CLOCAL | CREAD;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;
options.c……
[/Quote]
能把你的set_Parity函数贴出来么???
谢谢了
garefield 2010-07-29
  • 打赏
  • 举报
回复
我也用过这段代码,也是无法通信,后来网上查了下,在set_Parity的设置options的设置改成如下就可以了
options.c_cflag |= CLOCAL | CREAD;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;
options.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
garefield 2010-07-29
  • 打赏
  • 举报
回复
用什么用户执行的,root用户有读写串口文件的权限,但如果是用其它用户执行的,可能没读写的权限。先用cat和echo试一下能不能发数据,然后再用程序试
九个太阳2023 2010-07-29
  • 打赏
  • 举报
回复
我在系统起来查看设备的时候,在串口上是这么显示的:
root@seed:~# cd /dev
root@seed:/dev# ls -al ttyS*
crw-rw---- 1 root dialout 4, 64 Jan 1 03:52 ttyS0
crw-rw---- 1 root dialout 4, 65 Jan 1 03:52 ttyS1
crw------- 1 root root 4, 66 Jan 1 18:19 ttyS2
是不是要添加进dialout组才可以使用ttyS0呢???
请教高手
九个太阳2023 2010-07-26
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 tony142857 的回复:]

楼主,我也是遇到跟你一样的问题,你现在解决了没有哇?
[/Quote]
没有啊,郁闷几天了,单步调试也是没用····我决定看看串口的驱动有没有问题~~~~~~
加载更多回复(13)

23,223

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧