پاسخ : مشکل با rs485
با سلام و عذرخواهی از اینکه دیر جواب دادم. داده ای که قصد ارسالش رو دارید چه نوعیه؟ رشته یا باینری؟
نوشته اصلی توسط soh3il
$regfile = "m8def.dat" $crystal = 1000000 $hwstack = 50 $swstack = 50 $framesize = 50 $baud = 9600 Config Adc = Single , Prescaler = Auto Start Adc Config Portd.2 = Output $lib "modbus.lbx" const field_start = 253 const field_end = 254 '-------------------------------------------------------- Dim A As Long Dim B As Long Dim C As Byte Dim T(10) As String * 10 Dim T1 As Word Dim T2 As String * 10 Dim W As Byte Dim Adc1 As Word Dim Adc0 As Word Dim Adc2 As Word Dim Adc3 As Word Dim Singlee0 As Word Dim Singlee1 As Word Dim Temp As Byte , Sta As Byte , Fin As Byte Dim Temp2 As Byte Dim Temp3 As Word Dim Temp4 As Byte Dim Stringg As String * 1 Dim Stringg2 As String * 8 dim data_arr(4) as byte '------------------------------------------ Portd.2 = 0 Temp = 0 '------------------------------------------ Do Temp = Temp + 1 If Temp > 99 Then Temp = 0 data_arr(1) = field_start data_arr(2) = Temp data_arr(3) = Temp data_arr(4) = field_end for i = 1 to 4 Printbin data_arr(i) waitms 10 next i Waitms 500 Loop End '------------------------------------------
$regfile = "m16def.dat" $crystal = 1000000 $hwstack = 50 $swstack = 50 $framesize = 50 $baud = 9600 $lib "modbus.lbx" '------------------------------------------------------- Config Portc = Output Config Portd.2 = Output Portd.2 = 0 Config Portd.3 = Output Config Portd.4 = Output Config Portd.5 = Output Config Portd.6 = Output Config Portb.0 = Output Config Portb.1 = Output Config Portb.2 = Output Config Portb.3 = Output Config Pina.4 = Input Config Pina.5 = Input Config Pina.6 = Input Config Pina.7 = Input const field_start = 253 const field_end = 254 '-------------------------------------------------------- Dim serial_idx As Byte, data as byte, data1 as byte, data2 as byte Dim A As Byte , K As Byte Dim Aaa As Byte , Ii As Byte , Jj As Byte , Highi As Byte , Highj As Byte Dim Bbb As Byte , I As Byte , J As Byte , Lowi As Byte , Lowj As Byte Dim Ax As Word Dim B As Byte , Sta As Byte Dim D As Byte Dim Bx As Word Dim W As Byte , G As Bit Dim S As Byte , F As Byte Dim X As Word Dim Sa As Word Dim Ma As Eram Word Dim Mi As Eram Word Dim Maa As Eram Word Dim Mii As Eram Word Dim Bb As Word , Hight As Word Dim Cc As Word , Lowt As Word Dim B_high As Word , Hight_high As Word Dim B_low As Word , Hight_low As Word Dim C_high As Word , Lowt_high As Word Dim C_low As Word , Lowt_low As Word Dim T As Byte , Tw As Byte , Ttw As Byte Dim Ww As Bit , Www As Bit , Aw As Bit , Bw As Bit , Cw As Bit , Dw As Bit Bb = Ma Cc = Mi Hight = Maa Lowt = Mii '--------------------------------------------- Set_key Alias Pina.6 Up Alias Pina.4 Down Alias Pina.7 Esc Alias Pina.5 Relayh Alias Portb.2 Relayl Alias Portb.3 Le1 Alias Portd.3 Le2 Alias Portd.5 Le3 Alias Portd.4 Le4 Alias Portd.6 T = 1 K = 0 W = 11 '-------------------------------------------- On Urxc Rec0_isr Enable Urxc Enable Interrupts '--------------------------------------------- Portd.2 = 0 Waitms 2000 Portb.1 = 1 '-------------------------------------------- Getdata: Do if serial_idx = 4 then 'data is ready now serial_idx = 0 if data1 = data2 then 'data is true S = data1 If S = 5 Then Relayh = 1 If S = 15 Then Relayh = 0 Relayl = 1 End If If S = 30 Then Relayh = 1 Relayl = 0 End If If S = 45 Then Relayh = 0 Relayl = 1 End If If S = 60 Then Relayh = 1 Relayl = 0 End If If S = 85 Then Relayh = 0 Relayl = 1 End If If S = 95 Then Relayh = 1 Relayl = 0 End If Portc = Lookup(10 , Digit) Le1 = 1 Le1 = 0 Portc = Lookup(1 , Digit) Le2 = 1 Le2 = 0 If S < 10 Then Portc = Lookup(0 , Digit) Le3 = 1 Le3 = 0 Portc = Lookup(s , Digit) Le4 = 1 Le4 = 0 End If If S >= 10 Then Aaa = S / 10 Portc = Lookup(aaa , Digit) Le3 = 1 Le3 = 0 '--------------------------------------- Bbb = S Mod 10 Portc = Lookup(bbb , Digit) Le4 = 1 Le4 = 0 Waitms 100 End If end if end if Loop '--------------------------------------- Rec0_isr: inputbin data select case serial_idx case 0: if data = field_start then incr serial_idx case 1: if data < 100 then incr serial_idx data1 = data else serial_idx = 0 end if case 2: if data < 100 then incr serial_idx data2 = data else serial_idx = 0 end if case 3: if data = field_end then incr serial_idx else serial_idx = 0 end if end select Return '--------------------------------------- Digit: Data &B01110111 '0 Data &B00100100 '1 Data &B01011110 '2 Data &B01101110 '3 Data &B00101101 '4 Data &B01101011 '5 Data &B01111011 '6 Data &B00100110 '7 Data &B01111111 '8 Data &B01101111 '9 Data &B00111111 'A Data &B00001000 '- Data &B01010001 'L Data &B00111101 'H '--------------------------------------- Digit1: Data &B00111111 '0 Data &B00000110 Data &B01011011 Data &B01001111 Data &B01100110 Data &B01101101 Data &B01111100 Data &B00000111 Data &B01111111 Data &B01100111 '9 Data &B01110111 Data &B01000000 Data &B00111000 Data &B01110110
$regfile = "m8def.dat" $crystal = 1000000 $hwstack = 50 $swstack = 50 $framesize = 50 $baud = 9600 Config Adc = Single , Prescaler = Auto Start Adc Config Portd.2 = Output $lib "modbus.lbx" const field_start = 253 const field_end = 254 '-------------------------------------------------------- Dim A As Long Dim B As Long Dim C As Byte Dim T(10) As String * 10 Dim T1 As Word Dim T2 As String * 10 Dim W As Byte Dim Adc1 As Word Dim Adc0 As Word Dim Adc2 As Word Dim Adc3 As Word Dim Singlee0 As Word Dim Singlee1 As Word Dim Temp As Byte , Sta As Byte , Fin As Byte Dim Temp2 As Byte Dim Temp3 As Word Dim Temp4 As Byte Dim Stringg As String * 1 Dim Stringg2 As String * 8 dim data_req(5) as byte dim data_res(5) as byte dim serial_idx as byte, slave_address as byte '------------------------------------------ Portd.2 = 0 Temp = 0 On Urxc Rec0_isr Enable Urxc Enable Interrupts '------------------------------------------ Do Temp = Temp + 1 If Temp > 99 Then Temp = 0 'to slave 1 slave_address = 1 Portd.2 = 0 data_req(1) = field_start data_req(2) = slave_address 'slave address<=== data_req(3) = Temp data_req(4) = Temp data_req(5) = field_end for i = 1 to 5 Printbin data_req(i) waitms 10 next i 'wait for response serial_idx = 0 Portd.2 = 1 Waitms 1000 'check response if slave_idx = 5 then 'ok else 'error end if 'to slave 2 slave_address = 2 Portd.2 = 0 data_req(1) = field_start data_req(2) = slave_address 'slave address<=== data_req(3) = Temp data_req(4) = Temp data_req(5) = field_end for i = 1 to 5 Printbin data_req(i) waitms 10 next i 'wait for response serial_idx = 0 Portd.2 = 1 Waitms 1000 if slave_idx = 5 then 'ok else 'error end if Loop End '------------------------------------------ Rec0_isr: inputbin data select case serial_idx case 0: if data = field_start then incr serial_idx case 1: if data = slave_address then incr serial_idx else serial_idx = 0 end if case 2: if data < 100 then incr serial_idx data1 = data else serial_idx = 0 end if case 3: if data < 100 then incr serial_idx data2 = data else serial_idx = 0 end if case 4: if data = field_end then incr serial_idx else serial_idx = 0 end if end select Return
$regfile = "m16def.dat" $crystal = 1000000 $hwstack = 50 $swstack = 50 $framesize = 50 $baud = 9600 $lib "modbus.lbx" '------------------------------------------------------- Config Portc = Output Config Portd.2 = Output Portd.2 = 0 Config Portd.3 = Output Config Portd.4 = Output Config Portd.5 = Output Config Portd.6 = Output Config Portb.0 = Output Config Portb.1 = Output Config Portb.2 = Output Config Portb.3 = Output Config Pina.4 = Input Config Pina.5 = Input Config Pina.6 = Input Config Pina.7 = Input const field_start = 253 const field_end = 254 const slave_address = 1 '-------------------------------------------------------- Dim serial_idx As Byte, data as byte, data1 as byte, data2 as byte Dim data_arr(5) As Byte Dim A As Byte , K As Byte, i as byte Dim Aaa As Byte , Ii As Byte , Jj As Byte , Highi As Byte , Highj As Byte Dim Bbb As Byte , I As Byte , J As Byte , Lowi As Byte , Lowj As Byte Dim Ax As Word Dim B As Byte , Sta As Byte Dim D As Byte Dim Bx As Word Dim W As Byte , G As Bit Dim S As Byte , F As Byte Dim X As Word Dim Sa As Word Dim Ma As Eram Word Dim Mi As Eram Word Dim Maa As Eram Word Dim Mii As Eram Word Dim Bb As Word , Hight As Word Dim Cc As Word , Lowt As Word Dim B_high As Word , Hight_high As Word Dim B_low As Word , Hight_low As Word Dim C_high As Word , Lowt_high As Word Dim C_low As Word , Lowt_low As Word Dim T As Byte , Tw As Byte , Ttw As Byte Dim Ww As Bit , Www As Bit , Aw As Bit , Bw As Bit , Cw As Bit , Dw As Bit Bb = Ma Cc = Mi Hight = Maa Lowt = Mii '--------------------------------------------- Set_key Alias Pina.6 Up Alias Pina.4 Down Alias Pina.7 Esc Alias Pina.5 Relayh Alias Portb.2 Relayl Alias Portb.3 Le1 Alias Portd.3 Le2 Alias Portd.5 Le3 Alias Portd.4 Le4 Alias Portd.6 T = 1 K = 0 W = 11 '-------------------------------------------- On Urxc Rec0_isr Enable Urxc Enable Interrupts '--------------------------------------------- Portd.2 = 0 Waitms 2000 Portb.1 = 1 '-------------------------------------------- Getdata: Do if serial_idx = 5 then 'data is ready now serial_idx = 0 if data1 = data2 then 'data is true 'create response Portd.2 = 0 data_arr(1) = field_start data_arr(2) = slave_address data_arr(3) = data1 data_arr(4) = data1 data_arr(5) = field_end 'send response for i = 1 to 5 printbin data_arr(i) next i Portd.2 = 1 S = data1 If S = 5 Then Relayh = 1 If S = 15 Then Relayh = 0 Relayl = 1 End If If S = 30 Then Relayh = 1 Relayl = 0 End If If S = 45 Then Relayh = 0 Relayl = 1 End If If S = 60 Then Relayh = 1 Relayl = 0 End If If S = 85 Then Relayh = 0 Relayl = 1 End If If S = 95 Then Relayh = 1 Relayl = 0 End If Portc = Lookup(10 , Digit) Le1 = 1 Le1 = 0 Portc = Lookup(1 , Digit) Le2 = 1 Le2 = 0 If S < 10 Then Portc = Lookup(0 , Digit) Le3 = 1 Le3 = 0 Portc = Lookup(s , Digit) Le4 = 1 Le4 = 0 End If If S >= 10 Then Aaa = S / 10 Portc = Lookup(aaa , Digit) Le3 = 1 Le3 = 0 '--------------------------------------- Bbb = S Mod 10 Portc = Lookup(bbb , Digit) Le4 = 1 Le4 = 0 Waitms 100 End If end if end if Loop '--------------------------------------- Rec0_isr: inputbin data select case serial_idx case 0: if data = field_start then incr serial_idx case 1: if data = slave_address then incr serial_idx else serial_idx = 0 end if case 2: if data < 100 then incr serial_idx data1 = data else serial_idx = 0 end if case 3: if data < 100 then incr serial_idx data2 = data else serial_idx = 0 end if case 4: if data = field_end then incr serial_idx else serial_idx = 0 end if end select Return '--------------------------------------- Digit: Data &B01110111 '0 Data &B00100100 '1 Data &B01011110 '2 Data &B01101110 '3 Data &B00101101 '4 Data &B01101011 '5 Data &B01111011 '6 Data &B00100110 '7 Data &B01111111 '8 Data &B01101111 '9 Data &B00111111 'A Data &B00001000 '- Data &B01010001 'L Data &B00111101 'H '--------------------------------------- Digit1: Data &B00111111 '0 Data &B00000110 Data &B01011011 Data &B01001111 Data &B01100110 Data &B01101101 Data &B01111100 Data &B00000111 Data &B01111111 Data &B01100111 '9 Data &B01110111 Data &B01000000 Data &B00111000 Data &B01110110
[$regfile = "m8def.dat" $crystal = 11059200 $hwstack = 50 $swstack = 50 $framesize = 50 $baud = 9600 Config Adc = Single , Prescaler = Auto Start Adc Config Portd.2 = Output Const Field_start = 253 Const Field_end = 254 Const Slave_address = 1 '-------------------------------------------------------- Dim A As Long Dim B As Long Dim C As Byte , I As Byte Dim T(10) As String * 10 Dim T1 As Word Dim T2 As String * 10 Dim W As Byte Dim Adc1 As Word Dim Adc0 As Word Dim Adc2 As Word Dim Adc3 As Word Dim Singlee0 As Word Dim Singlee1 As Word Dim Temp As Byte , Sta As Byte , Fin As Byte Dim Temp2 As Byte Dim Temp3 As Word Dim Temp4 As Byte Dim Stringg As String * 1 Dim Stringg2 As String * 8 Dim Data0 As Byte , Data1 As Byte , Data2 As Byte Dim Data_arr(5) As Byte Dim Data_res(5) As Byte Dim Serial_idx As Byte '------------------------------------------ Temp = 0 On Urxc Rec0_isr Enable Urxc Enable Interrupts '------------------------------------------ Do Portd.2 = 0 Temp = Temp + 1 Waitms 400 If Temp > 99 Then Temp = 0 If Serial_idx = 3 Then 'data is ready now Serial_idx = 0 Portd.2 = 1 'data is true 'create response Portd.2 = 1 Data_arr(1) = Field_start Data_arr(2) = Slave_address Data_arr(3) = Temp Data_arr(4) = Temp Data_arr(5) = Field_end 'send response For I = 1 To 5 Printbin Data_arr(i) Next I End If Loop End '------------------------------------------ Rec0_isr: Inputbin Data0 Select Case Serial_idx Case 0: If Data0 = Field_start Then Incr Serial_idx Case 1: If Data0 = Slave_address Then Incr Serial_idx Else Serial_idx = 0 End If Case 2: If Data0 = Field_end Then Incr Serial_idx Else Serial_idx = 0 End If End Select Return '---- ]
دیدگاه