lydtz 发表于 2010-6-11 13:01:34

bascom-avr的485通讯例子

在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.

maxims 发表于 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.



大概就是这样了
页: [1]
查看完整版本: bascom-avr的485通讯例子