搜索
bottom↓
回复: 1

bascom-avr的485通讯例子

[复制链接]

出0入0汤圆

发表于 2010-6-11 13:01:34 | 显示全部楼层 |阅读模式
在BASCOM-AVR帮助里面有一段485的例程,但是英文的,请英文好并懂BASCOM编程的朋友讲解讲解!

$regfile = "m162def.dat"                                    ' specify the used micro

$crystal = 8000000

$baud = 19200                                               ' use baud rate

$hwstack = 42                                               ' default use 32 for the hardware stack

$swstack = 40                                               ' default use 10 for the SW stack

$framesize = 40                                             ' default use 40 for the frame space



$lib "modbus.lbx"

Config Print1 = Portb.1 , Mode = Set              ' use portb.1 for the direction

Rs485dir Alias Portb.1

Config Rs485dir = Output

Rs485dir = 0   ' go to receive mode

Portc.0 = 1    ' a switch is connected to pinc.0 so activate pull up resistor

'             TX    RX

' COM0   PD.1   PD.0   monitor

' COM1   PB.3   PB.2   rs485

'            PB.1           data direction rs485





Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

Config Com2 = 9600 , Synchrone = 0 , Parity = Even , Stopbits = 1 , Databits = 8 , Clockpol = 0  ' MUST MATCH THE SLAVE



'use OPEN/CLOSE for using the second UART

Open "COM2:" For Binary As #1



'dimension some variables

Dim B As Byte

Dim W As Word

Dim L As Long



W = &H4567   ' set some values

L = &H12345678





Print "RS-485 MODBUS master"

Do

  If Pinc.0 = 0 Then                                       ' test button

     Waitms 500                                            ' delay since we want to send just 1 frame

     Print "send request to slave/server"            ' to debug terminal

    ' Print #1 , Makemodbus(2 , 3 , 8 , 2);                  'slave 2, function 3, start address 8, 2 bytes

    ' Print #1 , Makemodbus(2 , 6 , 8 , W);                 'slave 2, function 6, address 8  , value of w

      Print #1 , Makemodbus(b , 16 , 8 , L);                'send a long

  End If

  If Ischarwaiting(#1) <> 0 Then  'did we got something back?

    B = Waitkey(#1)  ' yes so get it

    Print Hex(b) ; ",";  ' print it

  End If

Loop



A slave would simply listen to data, and once enough data received, send it back.

The MODBUS slave code is available as a commercial add on.

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

发表于 2013-1-21 23:39:24 | 显示全部楼层
$regfile = "m162def.dat"                                    ' specify the used micro
定义CPu的类型文件
$crystal = 8000000
定义cpu的工作频率
$baud = 19200                                               ' use baud rate
定义串口波特率
$hwstack = 42                                               ' default use 32 for the hardware stack
堆栈相关
$swstack = 40                                               ' default use 10 for the SW stack
堆栈相关
$framesize = 40                                             ' default use 40 for the frame space
无法解释这个……


$lib "modbus.lbx"
引用modbus协议文件
Config Print1 = Portb.1 , Mode = Set              ' use portb.1 for the direction
定义端口
Rs485dir Alias Portb.1
定义端口
Config Rs485dir = Output
定义端口
Rs485dir = 0   ' go to receive mode
定义端口
Portc.0 = 1    ' a switch is connected to pinc.0 so activate pull up resistor
定义端口
'             TX    RX

' COM0   PD.1   PD.0   monitor
定义端口
' COM1   PB.3   PB.2   rs485
定义端口
'            PB.1           data direction rs485
定义端口




Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
设置串口工作模式
Config Com2 = 9600 , Synchrone = 0 , Parity = Even , Stopbits = 1 , Databits = 8 , Clockpol = 0  ' MUST MATCH THE SLAVE
设置串口工作模式


'use OPEN/CLOSE for using the second UART

Open "COM2:" For Binary As #1
设置串口工作模式


'dimension some variables
变量定义
Dim B As Byte

Dim W As Word

Dim L As Long



W = &H4567   ' set some values

L = &H12345678





Print "RS-485 MODBUS master"
输出内容
Do
主程序开始
  If Pinc.0 = 0 Then                                       ' test button
测试按键值
     Waitms 500                                            ' delay since we want to send just 1 frame
延时500ms
     Print "send request to slave/server"            ' to debug terminal
输出内容
    ' Print #1 , Makemodbus(2 , 3 , 8 , 2);                  'slave 2, function 3, start address 8, 2 bytes

    ' Print #1 , Makemodbus(2 , 6 , 8 , W);                 'slave 2, function 6, address 8  , value of w

      Print #1 , Makemodbus(b , 16 , 8 , L);                'send a long

  End If
按键测试短完整
  If Ischarwaiting(#1) <> 0 Then  'did we got something back?

    B = Waitkey(#1)  ' yes so get it

    Print Hex(b) ; ",";  ' print it

  End If

Loop



A slave would simply listen to data, and once enough data received, send it back.

The MODBUS slave code is available as a commercial add on.



大概就是这样了
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-24 05:16

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

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