【问题标题】:Serial communication between linux and 8051 microcontroller board using rs-232 in clinux和8051微控制器板在c中使用rs-232进行串行通信
【发布时间】:2012-06-20 18:53:06
【问题描述】:

我在使用 RS-232 从 linux OS 向 uc 8051 串行发送数据时遇到问题。 8051设置:

波特率 = 9600;
端口 = 端口 1。
奇偶校验 = 无
停止位 = 一个

// my code for receiving data on 8051 uc
#include <reg51.h>

unsigned char value;
int i,j;

void ini()     
{
    TMOD=0x20;  //Timer1, mode 2, baud rate 9600 bps
    TH1=0XFD; 
    SCON=0x50;
    TR1=1;
}

void delay()
{
    for(i=0;i<=1000;i++)
    for (j=0;j<=300;j++);
}

void recieve() 
{
    unsigned char value;
    while(RI==0);
    value=SBUF;
    P1=value;
    RI=0;
}

void main()
{
    while(1)
    {
        ini();
        recieve();
    }
}

// and code which run on linux is as following
#include <stdio.h> // standard input / output functions
#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 definitionss
#include <time.h>   // time calls

int open_port(void)
{
    int fd; // file description for the serial port

    fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);

    if (fd == -1) // if open is unsucessful
    {
        //perror("open_port: Unable to open /dev/ttyS0 - ");
        printf("open_port: Unable to open /dev/ttyS0. \n");
    }
    else
    {
        fcntl(fd, F_SETFL, 0);
        printf("port is open.\n");
    }

    return(fd);
} //open_port

int configure_port(int fd)      // configure the port
{
    struct termios port_settings;      // structure to store the port settings in

    cfsetispeed(&port_settings, B9600);    // set baud rates
    cfsetospeed(&port_settings, B9600);

    port_settings.c_cflag &= ~PARENB;    // set no parity, stop bits, data bits
    port_settings.c_cflag &= ~CSTOPB;
    port_settings.c_cflag &= ~CSIZE;
port_settings.c_cflag |= CS8;

    tcsetattr(fd, TCSANOW, &port_settings);    // apply the settings to the port
    return(fd);

} //configure_port

int query_modem(int fd)   // query modem with an AT command
{
    char n;
    fd_set rdfs;
    struct timeval timeout;

    // initialise the timeout structure
    timeout.tv_sec = 10; // ten second timeout
    timeout.tv_usec = 0;

    // Create byte array
    unsigned char send_bytes[] = {  0x00, 0xff};

    write(fd, send_bytes, 2);  //Send data
    printf("Wrote the bytes. \n");

    // do the select
    n = select(fd + 1, &rdfs, NULL, NULL, &timeout);

    // check if an error has occured
    if(n < 0)
    {
        perror("select failed\n");
    }
    else if (n == 0)
    {
        puts("Timeout!");
    }
    else
    {
        printf("\nBytes detected on the port!\n");
    }
    return 0;
} //query_modem

int main(void)
{ 
    int fd = open_port();
    configure_port(fd);
    query_modem(fd);
    return(0);
} // main

但是我有问题..所以请帮助我并告诉我 linux 以哪种格式通过 rs-232 发送数据。也接收8051单片机的格式。 需要 C 中的示例代码。

【问题讨论】:

  • 我们需要您的代码和您遇到的错误
  • 先生,我已经发布了我的代码......
  • 如果你运行它会发生什么?这是一个非常具体的问题,因此您可能应该尽可能多地提供有关正在发生的事情的信息,以期有人做过类似的事情。
  • 更改 8051 代码以不断传输 0x55 字符,在 8n1 模式下是方波。在示波器上查看并验证时钟速率是否正确。在主机端拿一个回形针和短插座 2 和 3 并验证您发送的内容会返回。一旦所有这些都完成并更正,您至少应该有一种通信方式,您可以使用它打印寄存器内容等,并通过在 8051 上接收字符来调试您的方式。

标签: c linux serial-port microcontroller embedded-linux


【解决方案1】:

我推荐基本的故障查找

  • 确保使用正确的电缆(2 和 3 被划掉,开始时没有握手线)
  • 分离设备,即将你的 Linux 机器连接到运行终端的 PC 程序并尝试建立双向通信 - 在 Linux / C 代码上工作,直到你实现
  • 将您的 8051 连接到运行终端程序的 PC ....
  • 仅当 Linux 和 8051 都适用于已知且经过验证的设备时才连接它们

如果您的 8051 代码示例代表所有代码 .... 8051 将如何响应 AT .... 它只能接收。

祝你好运

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多