广告广告
  加入我的最爱 设为首页 风格修改
首页 首尾
 手机版   订阅   地图  繁体 
您是第 5125 个阅读者
 
发表文章 发表投票 回覆文章
  可列印版   加为IE收藏   收藏主题   上一主题 | 下一主题   
oddy
数位造型
个人文章 个人相簿 个人日记 个人地图
路人甲
级别: 路人甲 该用户目前不上站
推文 x0 鲜花 x0
分享: 转寄此文章 Facebook Plurk Twitter 复制连结到剪贴簿 转换为繁体 转换为简体 载入图片
推文 x0
[C/C++][求助] 谁懂modbus 跟 ARM
各位大大 小弟因为工作上需要
刚开始碰到 modbus 的东西
也看了有关的协议内容
试着用些范例修改 但是在ARM板上执行后 却跑出一堆乱码 跟 数字0
..

访客只能看到部份内容,免费 加入会员 或由脸书 Google 可以看到全部内容



[ 此文章被oddy在2006-06-28 15:09重新编辑 ]


我会守护你的
因为我们之间存在着某种力量 它可以超越一切
无论种族 生死 时间 甚至光明与黑暗
献花 x0 回到顶端 [楼 主] From:台湾中华电信 | Posted:2006-06-28 15:04 |
cmovies
数位造型
个人文章 个人相簿 个人日记 个人地图
小人物
级别: 小人物 该用户目前不上站
推文 x0 鲜花 x20
分享: 转寄此文章 Facebook Plurk Twitter 复制连结到剪贴簿 转换为繁体 转换为简体 载入图片

Modbus 你用的哪个 Function?
Modbus 用的都是 binary 的方式来传送资料.
因此如果要显示的话需要转换一下.

如果可以把程式贴出来大家研究看看.


献花 x0 回到顶端 [1 楼] From:台湾中华电信 | Posted:2006-06-29 17:03 |
oddy
数位造型
个人文章 个人相簿 个人日记 个人地图
路人甲
级别: 路人甲 该用户目前不上站
推文 x0 鲜花 x0
分享: 转寄此文章 Facebook Plurk Twitter 复制连结到剪贴簿 转换为繁体 转换为简体 载入图片

嗯嗯 ok啊 可是程式码有点啰唆 满长的
而且因为才刚碰这东西 所以很多是拼拼凑凑的
主要是要用 02 read input atatus
          03 read holding registers
          04 read input registers     这三个 function
之前有搜寻过大陆网站 可能是简体字的关系 看不太懂
麻烦各位帮忙噜 表情

第一个是uart.c
复制程式
/*
 *       
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <asm/io.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "uart.h"

#define FALSE -1
#define TRUE   0

#define IS                     0x02
#define HR                0x03
#define IR                     0x04
#define READ_IS              0x02
#define READ_HR         0x03
#define READ_IR              0x04
#define PROTOCOL_EXCEPTION        0x81
#define PROTOCOL_ERR                1

const unsigned char auchCRCHi[] = {
         0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
         0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
         0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
         0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
         0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81,
         0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
         0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
         0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
         0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
         0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
         0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
         0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
         0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
         0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
         0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
         0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
         0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
         0x40
  } ;

const unsigned  char auchCRCLo[] = {
       0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4,
       0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
       0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD,
       0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
       0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7,
       0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
       0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE,
       0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
       0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2,
       0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
       0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB,
       0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
       0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91,
       0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
       0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88,
       0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
       0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80,
       0x40
};

//static int              devfd;

//设置序列埠通信速率 
int baud_rate[] = { B9600, B9600, B9600, B9600};
int name_speed[] = {9600, 9600, 9600, 9600};
void set_speed(int fd, int speed){
       int   i; 
       int   status; 
       struct termios   Opt;
       tcgetattr(fd, &Opt); 
       for ( i= 0;  i < sizeof(baud_rate) / sizeof(int);  i++) { 
              if  (speed == name_speed[i]) {
                     tcflush(fd, TCIOFLUSH);
                     cfsetispeed(&Opt, baud_rate[i]);
                     cfsetospeed(&Opt, baud_rate[i]);
                     status = tcsetattr(fd, TCSANOW, &Opt);
                     if  (status != 0) {        
                            perror("tcsetattr fd1");  
                            return;    
                     }    
                     tcflush(fd,TCIOFLUSH);   
              }  
       }
}
//设置序列埠资料位元,校验位和停止位元(8, N, 1)

int set_parity(int fd,int databits,int parity,int stopbits)
{
       struct termios options;
       if  ( tcgetattr( fd,&options)  !=  0)
       {
                perror("SetupSerial 1");
                return(FALSE);
         }
         options.c_cflag &= ~CSIZE;
         if (databits == 8)       options.c_cflag |= CS8;                     /*设置资料位元数为 8*/
       if (parity == 'N'){       options.c_cflag &= ~PARENB;          /* Clear parity enable 设为无效验*/
                                          options.c_iflag &= ~INPCK; }          /* Enable parity checking */
       if (stopbits == 2)       options.c_cflag |= CSTOPB;               /* 设置停止位 2*/
            
    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)
         {
                perror("SetupSerial 3");
              return (FALSE);
       }
         return (TRUE);
}

//打开介面
int OpenDev(char *Dev)
{
int       fd = open( Dev, O_RDWR );         //| O_NOCTTY | O_NDELAY
       if (-1 == fd)   //避免只用一个=等号的错误
              { 
                     perror("Can't Open Serial Port RS485");
                     return -1;
              }
       else
       return fd;
}

//使用crc验证
unsigned short crc(unsigned char *puchMsg , unsigned short usDataLen)  
{
    unsigned char uchCRCHi = 0xFF ; /* high byte of CRC initialized */
    unsigned char uchCRCLo = 0xFF ; /* low byte of CRC initialized */
    unsigned uIndex ;  /* will index into CRC lookup table */
                      
    while (usDataLen--) /* pass through message buffer */
    {
       uIndex = uchCRCHi ^ *puchMsg++ ; /* calculate the CRC */
       uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex];
       uchCRCLo = auchCRCLo[uIndex];
       }
       return (uchCRCHi << 8 | uchCRCLo);
}

//建立rtu模式
void construct_rtu_frm ( unsigned char *dst_buf, unsigned char *src_buf, unsigned char lenth)
{
       unsigned short crc_tmp = crc(src_buf, lenth);
       
              *(src_buf+lenth) = crc_tmp >> 8 ;
              *(src_buf+lenth+1) = crc_tmp & 0xff;
              lenth++;
              //lenth++;
              
              while (lenth--)
              {
                     *dst_buf = *src_buf;
                     dst_buf++;
                     src_buf++;
              }
}

//02 读取输入状态
int rtu_read_input_status(unsigned char device_adr, unsigned char *com_buf, int start_address, int lenth) 
{
           unsigned char tmp[64], tmp_lenth = 6;
               
               tmp[0] = device_adr;
                  tmp[1] = READ_IS;
                  tmp[2] = 0x00;
                     tmp[3] = start_address - 1;
                     tmp[4] = 0;
                     tmp[5] = lenth;
                     
                     construct_rtu_frm ( com_buf,tmp,tmp_lenth);
                     return 8;
}

//03 读取保持暂存器
int  rtu_read_hldreg ( unsigned char device_adr,unsigned char *com_buf,int start_address,int lenth)
{
           unsigned char tmp[64], tmp_lenth = 6;
               
               tmp[0] = device_adr;
                  tmp[1] = READ_HR;
                  tmp[2] = 0x00;
                     tmp[3] = start_address - 1;
                     tmp[4] = 0;
                     tmp[5] = lenth;
                     
                     construct_rtu_frm ( com_buf,tmp,tmp_lenth);
                     return 8;
}

// 4 发送读取输入暂存器
int rtu_read_inptreg( unsigned char device_adr,unsigned char *com_buf,int start_address,int lenth) 
      {
        unsigned char tmp[64], tmp_lenth = 6;
        tmp[0] = device_adr;
        tmp[1] = READ_IR;
        tmp[2] = 0x00;
              tmp[3] = start_address - 1;
              tmp[4] = 0;
              tmp[5] = lenth;
        
        construct_rtu_frm ( com_buf,tmp,tmp_lenth);
        return 8;
      }
 
/*//RTU  接收分析:
dest_p 接收到资料指标
sourc_p 串口接收缓冲区指标
data_start_address 开始地址*/

int rtu_data_anlys(int *dest_p, unsigned char *source_p, int data_start_address, int fr_lenth)
{
       unsigned short crc_result, crc_tmp;
       unsigned char tmp1, tmp2, shift;
       
       crc_tmp = *(source_p + fr_lenth-2);                             // crc  第一位元组
       crc_tmp = crc_tmp * 256 + *( source_p+fr_lenth-1);        // CRC 值
       crc_result = crc(source_p, fr_lenth-2);                      // 计算CRC 值
       
       if ( crc_tmp != crc_result ) // CRC 校验正确       
       {
              //hld_reg[0x31]++;
              return -1;
       }  
       switch ( *(source_p+1) ) // 功能码
       {                     
              case READ_IS: //读取输入状态
                     for ( tmp1=0; tmp1<*( source_p+2); tmp1++)
                     {
                            shift = 1;
                            for (tmp2=0; tmp2<8; tmp2 ++)
                            { 
                                   *(dest_p + data_start_address + tmp1*8 + tmp2) = shift & *( source_p+3);
                                   *( source_p+3)>>=1;                                   
                            }
                     }
                     break;
                     
              case READ_HR:  //读取保持寄存器
                     for ( tmp1=0; tmp1<*( source_p+2); tmp1+=2)
                     {
                            *(dest_p + data_start_address+ tmp1/2)= *( source_p+tmp1+3)*256 +  *( source_p+tmp1+4) ;
                            
                     }
                     break ;
                     
              case READ_IR:      //读取输入寄存器
                     for ( tmp1=0; tmp1<*( source_p+2); tmp1+=2)
                     {
                            *(dest_p + data_start_address+ tmp1/2) = *( source_p+tmp1+3)*256 +  *( source_p+tmp1+4) ;
                     }
                     break;

              case PROTOCOL_EXCEPTION:
                     return -1*PROTOCOL_ERR; 
                     //break;

              default:
                     return -1*PROTOCOL_ERR;
                     //break;
       }
       return 0;
}



//       2004/12/2       Milkyway.Lee
int read485reg_init(int brt)
{
       /*if (brt == 2400)
       {
              // Setup Baud rate 2400
              *(volatile unsigned short*) BRSR1       = (ARM_CLOCK/(16 * UART3_BIT_RATE)) - 1;
       }
       else if (brt == 4800)
       {
              // Setup Baud rate 4800
              *(volatile unsigned short*) BRSR1       = (ARM_CLOCK/(16 * UART2_BIT_RATE)) - 1;
       }
       else*/ if (brt == 9600)
       {
              //default: Setup Baud rate 9600
              *(volatile unsigned short*) BRSR1       = (ARM_CLOCK/(16 * UART1_BIT_RATE)) - 1;
       }
       *(volatile unsigned short*) MSR1       = 0x0000;                     // disable all interrupt
       *(volatile unsigned short*) RFCR1       = 0x8200;                     // clear Rx FIFO
       *(volatile unsigned short*) TFCR1       = 0x8200;                     // clear Tx FIFO       
       *(volatile unsigned short*) LCR1       = 0x0000;                     // no-BREAK
//       *(volatile unsigned short*) TFCR1       &= 0x7FFF;                     //set counter 0

       *(volatile unsigned short*) BITDIR0    &= 0xEFEF;              // GIO 12&4 Output

       PRINTK("Clear Count : %x\n", (*(volatile unsigned short*) RFCR1 & 0x003F));
       PRINTK("Rec Empty Flag : 0x%x\n", (*(volatile unsigned short*) SR1 & 0x0004));
       
       return 1;
}

int write485sd(char* sendbuf, unsigned short len)
{
       unsigned short usbuf;       unsigned int j=0;       unsigned short i;       
       PRINTK("sendbuf = %x %x %x %x %x %x\n",
                                   *sendbuf,*(sendbuf+1),*(sendbuf+2),*(sendbuf+3),*(sendbuf+4),*(sendbuf+5));
       *(volatile unsigned short*) TFCR1       &= 0x8000;       // Clear Tx FIFO
       *(volatile unsigned short*)BITSET0  = 0x1010;       // SET GIO12 & GIO4       
       i=len;
       while (i)
       {
              //check TX FIFO has data?
              usbuf = *(volatile unsigned short*) SR1 & 0x0001;
              if (usbuf == 1)
              {
                     *(volatile unsigned short*) DTRR1 = *sendbuf++;
                     i--;
              }
       }
       //usleep(8000);
       //while((*(volatile unsigned short*) SR1 & 0x0001)!=0x01 ){j++;}  //Send FIFO is empty status
       j=6;
       while (j)
       {
              //check TX FIFO has data?
              usbuf = *(volatile unsigned short*) SR1 & 0x0001;
       
              if (usbuf == 1)       
              {
                      *(volatile unsigned short*) RFCR1 &= 0x8000;         // clear Rx FIFO
//                     *(volatile unsigned short*)BITCLR0  = 0x1010;       //CLR GIO12 & GIO4 
                     j=0;
              }
       }
//       *(volatile unsigned short*) RFCR1 &= 0x8000;         // clear Rx FIFO
       *(volatile unsigned short*)BITCLR0 = 0x1010;       //CLR GIO12 & GIO4 
}

int read485sd(char* buf)
{
    unsigned int tmp=0,count=0,i;
       unsigned char Data[120];
       
       tmp=0;
       
       while(1)
       {
              if ((*(volatile unsigned short*) RFCR1 & 0x003F) != 0x0 )
              {
                     Data[tmp]= 0x00;
                     Data[tmp]= *(volatile unsigned short*) DTRR1 & 0xff;       // read data from dsc25 register
                     tmp++;
                     count = 0;
              }
              else
              {
                     count++;
                     for (i=0;i<500;i++)
                     {
                     }
                     if (count>3)
                     {
                            memcpy(buf,Data,tmp);
              /*              PRINTK("Data = %x %x %x %x %x %x\n",
                                                        Data[0],Data[1],Data[2],Data[3],Data[4],Data[5]);
                            *(volatile unsigned short*) RFCR1       = 0x8200;                     // clear Rx FIFO
                            *(volatile unsigned short*) RFCR1  &= 0x7FFF;
                            PRINTK("Received Byte = %d\n",tmp);
                            return tmp; */
                     }
              }
       }
}

int msec_delay(int m_sec)
{
       struct timeval       tv;

       tv.tv_sec = 0;
       tv.tv_usec = m_sec*1000;
       select(1,NULL,NULL,NULL,&tv);
       return 0;
}

第二个是main.c
复制程式
/*
 *      
 */
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <errno.h>
#include <linux/types.h>
#include <unistd.h>
#include <sys/times.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netdb.h>
#include <ctype.h>
#include <fcntl.h>
#include <termios.h>    /*PPSIX终端控制定义*/
#include <errno.h>

#include "uart.h"

#define PRINTK(str...) printf(str)

#define GIO_DO_ENABLE              0x8000              //GIO15
#define FALSE -1
#define TRUE   0

#define READ_IS                            02
#define READ_HR                       03
#define READ_IR                            04
#define PROTOCOL_EXCEPTION        0x81
#define PROTOCOL_ERR                1

int IS[64], HR[64], IR[64];
int read485reg_init(int brt);
int OpenDev(char *Dev);
void set_speed(int fd, int speed);
int set_parity(int fd,int databits,int parity,int stopbits);
int rtu_read_input_status(unsigned char device_adr, unsigned char *com_buf, int start_address, int lenth);
int rtu_read_hldreg ( unsigned char device_adr,unsigned char *com_buf,int start_address,int lenth);
int rtu_read_inptreg( unsigned char device_adr,unsigned char *com_buf,int start_address,int lenth); 
int rtu_data_anlys(int *dest_p, unsigned char *source_p, int data_start_address, int fr_lenth);
//char resp_cmd[120];

static char       resp_cmd[80];
int name_speed[4];

static int rd_result;

//       MAIN()
int main(int argc, char *argv[])
{
       int fd;
       char tx_buf[256], rx_buf[256], *dev ="/dev/ttyS0";
       fd = OpenDev(dev);
       
    *(unsigned short*)DIR0  = *(unsigned short*)DIR0 & (~GIO_DO_ENABLE);
       *(unsigned short*)BITCLR0 =  GIO_DO_ENABLE ;
       
       //set baud rate: 2400 4800 9600
       read485reg_init(9600);
       
          while(1)
          {                 
                 if ((*(volatile unsigned short*) RFCR1 & 0x003F) != 0x0 )
                 {
                        memset(resp_cmd,0x00,sizeof(resp_cmd));
                        rd_result = read485sd(resp_cmd);
                        PRINTK("Get from Sensor %d bytes:\n",rd_result);
                        PRINTK("Command %s:\n",resp_cmd);   
                 }
                 else
                 {
                        PRINTK("THERE is NOthing\n"); // no data
                        break;
              }
       }
       
       if (fd>0)
           set_speed(fd,*name_speed);
       else{
                     printf("Can't Open Serial Port!\n");
                     exit(0);
              }
         if (set_parity(fd, 8, 'N', 2)== FALSE)
                {
                  printf("Set Parity Error\n");
                  exit(1);
                }
         /*
         while(1)
         {
                 while((nread = read(fd,buff,512))>0)
                 {
                    printf("\nLen %d\n",nread);
                    buff[nread+1]='\0'
                    printf("\n%s",buff);
                  }
         }*/
         
         rtu_read_input_status(1,tx_buf,1,3);        
       write (fd, tx_buf, sizeof(tx_buf));
       //PRINTK("Command 02: [%x] [%x] [%x] [%x] [%x] [%x]\n",
              //tx_buf[0],tx_buf[1],tx_buf[2],tx_buf[3],tx_buf[4],tx_buf[5]);
       read  (fd, rx_buf, sizeof(rx_buf));
       rtu_data_anlys(IS, rx_buf, 1, 6);
       PRINTK("Response Data 02: [%x] [%x] [%x] [%x] [%x] [%x]\n",
              rx_buf[0],rx_buf[1],rx_buf[2],rx_buf[3],rx_buf[4],rx_buf[5]);       
              
       rtu_read_hldreg(1,tx_buf,1,10);        
       write (fd, tx_buf, sizeof(tx_buf));
       //PRINTK("Command 03: [%x] [%x] [%x] [%x] [%x] [%x]\n",
              //tx_buf[0],tx_buf[1],tx_buf[2],tx_buf[3],tx_buf[4],tx_buf[5]);
       read  (fd, rx_buf, sizeof(rx_buf));
       rtu_data_anlys(HR, rx_buf, 1, 6);       
       PRINTK("Response Data 03: [%x] [%x] [%x] [%x%x] [%x%x] [%x%x] [%x%x] [%x%x] [%x%x] [%x%x] [%x%x] [%x%x] [%x%x] [%x] [%x]\n",
              rx_buf[0],rx_buf[1],rx_buf[2],rx_buf[3],rx_buf[4],rx_buf[5],rx_buf[6],rx_buf[7],rx_buf[8],rx_buf[9],rx_buf[10],rx_buf[11],rx_buf[12],rx_buf[13],rx_buf[14],rx_buf[15],rx_buf[16],rx_buf[17],rx_buf[18],rx_buf[19],rx_buf[20],rx_buf[21],rx_buf[22],rx_buf[23],rx_buf[24]);
       
       rtu_read_inptreg(1,tx_buf,1,5);
       write (fd, tx_buf, sizeof(tx_buf));
       //PRINTK("Command 04: [%x] [%x] [%x] [%x] [%x] [%x]\n",
              //tx_buf[0],tx_buf[1],tx_buf[2],tx_buf[3],tx_buf[4],tx_buf[5]);
       read  (fd, rx_buf, sizeof(rx_buf));
       rtu_data_anlys(IR, rx_buf, 1, 6);
       PRINTK("Response Data 04: [%x] [%x] [%x] [%x%x] [%x%x] [%x%x] [%x%x] [%x%x] [%x] [%x]\n",
              rx_buf[0],rx_buf[1],rx_buf[2],rx_buf[3],rx_buf[4],rx_buf[5],rx_buf[6],rx_buf[7],rx_buf[8],rx_buf[9],rx_buf[10],rx_buf[11],rx_buf[12],rx_buf[13],rx_buf[14]);
       
    close(fd);
    return 0;
}


我会守护你的
因为我们之间存在着某种力量 它可以超越一切
无论种族 生死 时间 甚至光明与黑暗
献花 x0 回到顶端 [2 楼] From:台湾中华电信 | Posted:2006-06-29 17:26 |
cmovies
数位造型
个人文章 个人相簿 个人日记 个人地图
小人物
级别: 小人物 该用户目前不上站
推文 x0 鲜花 x20
分享: 转寄此文章 Facebook Plurk Twitter 复制连结到剪贴簿 转换为繁体 转换为简体 载入图片

Modbus 的规格可以从 Modbus 网站下载:
http://www.modbus.org/docs/Modbus_...tocol_V1_1a.pdf

看程式后有几个地方不太了解?
1. read 本身为 blocking 还是 non-blocking, 是否会 timeout?
2. main 执行完就直接 return, 一般 Embedded 应该不会这么做吧? 这会 return 到哪里?
3. construct_rtu_frm 中的 length++ 为何要少一个? 这样 crc 不就少一个吗?
4. 真的有收到资料吗? 有方式可以检查收到几个 bytes 及收到的资料为何吧!?


献花 x0 回到顶端 [3 楼] From:台湾中华电信 | Posted:2006-06-29 18:29 |
oddy
数位造型
个人文章 个人相簿 个人日记 个人地图
路人甲
级别: 路人甲 该用户目前不上站
推文 x0 鲜花 x0
分享: 转寄此文章 Facebook Plurk Twitter 复制连结到剪贴簿 转换为繁体 转换为简体 载入图片

呃 不好意思 这么久才来回文 表情

这几个问题 老实说 我不太会回答
1.这我不懂表情
2.这点我也是怀疑, 不过看了一些范例都是要return, 就跟着做了
3.当时是因为compile的时候, 讯息上显示这里有问题才注解掉的, 现在恢复原来的样子了
4.至于这个就是我最大的问题了... 应该是没收到。 不过在 rx_buf 的第一个array里会有数字, 但每次都
  不一样, 所以无法判断到底是什么 表情   至于...方式 我有用示波器看 结果好像没有反应 表情

还是不懂啊......这几天看了modbus的协议 看到快疯了 还是不会写 表情


我会守护你的
因为我们之间存在着某种力量 它可以超越一切
无论种族 生死 时间 甚至光明与黑暗
献花 x0 回到顶端 [4 楼] From:台湾中华电信 | Posted:2006-07-05 17:29 |
cmovies
数位造型
个人文章 个人相簿 个人日记 个人地图
小人物
级别: 小人物 该用户目前不上站
推文 x0 鲜花 x20
分享: 转寄此文章 Facebook Plurk Twitter 复制连结到剪贴簿 转换为繁体 转换为简体 载入图片

请问你用的是哪种系统?
你的 PRINTK 应该可以显示出一些讯息吧?!
read 的传回值就可以用来确定有没有收到资料

我想你的问题重点应该是不晓的怎么用目前的系统写通讯程式,
而不是 Modbus 的协议难懂吧!?
一般 Modbus 如果 Error Handler 不太要求的话, 应该很容易写出来!


献花 x0 回到顶端 [5 楼] From:台湾中华电信 | Posted:2006-07-05 18:42 |
oddy
数位造型
个人文章 个人相簿 个人日记 个人地图
路人甲
级别: 路人甲 该用户目前不上站
推文 x0 鲜花 x0
分享: 转寄此文章 Facebook Plurk Twitter 复制连结到剪贴簿 转换为繁体 转换为简体 载入图片

嗯 我想你说的应该是对的 表情

我再努力试试看 感谢你的提醒 表情


我会守护你的
因为我们之间存在着某种力量 它可以超越一切
无论种族 生死 时间 甚至光明与黑暗
献花 x0 回到顶端 [6 楼] From:台湾中华电信 | Posted:2006-07-06 10:19 |
benzgigi 手机
个人文章 个人相簿 个人日记 个人地图

初露锋芒
级别: 初露锋芒 该用户目前不上站
推文 x0 鲜花 x73
分享: 转寄此文章 Facebook Plurk Twitter 复制连结到剪贴簿 转换为繁体 转换为简体 载入图片

可以请问什么是ARM吗???谢谢!!


献花 x0 回到顶端 [7 楼] From:台湾亚太线上 | Posted:2007-01-20 10:06 |
codeboy 手机
个人文章 个人相簿 个人日记 个人地图
社区建设奖
小有名气
级别: 小有名气 该用户目前不上站
推文 x6 鲜花 x331
分享: 转寄此文章 Facebook Plurk Twitter 复制连结到剪贴簿 转换为繁体 转换为简体 载入图片

下面是引用benzgigi于2007-01-20 10:06发表的 :
可以请问什么是ARM吗???谢谢!!

http://www.a...om/



连结有问题请来信,我会尽快修正~^^
通报时请附上是"哪一篇失效"...这样我才能去修正~麻烦各位好心人士了~

[连结失效通报][ 网页.伺服.程式 讨论区]
----------------------在世界中心呼喊爱情----------------------
深深思念一个人的时候,我们不知不觉的地活在世界的中心...
献花 x0 回到顶端 [8 楼] From:未知地址 | Posted:2007-01-31 11:19 |
sqwzhandsome
数位造型
个人文章 个人相簿 个人日记 个人地图
路人甲
级别: 路人甲 该用户目前不上站
推文 x0 鲜花 x0
分享: 转寄此文章 Facebook Plurk Twitter 复制连结到剪贴簿 转换为繁体 转换为简体 载入图片



献花 x0 回到顶端 [9 楼] From:上海 | Posted:2007-02-13 12:42 |

首页  发表文章 发表投票 回覆文章
Powered by PHPWind v1.3.6
Copyright © 2003-04 PHPWind
Processed in 0.041266 second(s),query:16 Gzip disabled
本站由 瀛睿律师事务所 担任常年法律顾问 | 免责声明 | 本网站已依台湾网站内容分级规定处理 | 连络我们 | 访客留言