Hi nochmal
irgendwie funktioniert es trotzdem nicht so wie es soll. Also so hier mal der Code für Server auf dem Raspberry Pi:
Code:
from socket import *
import serial
import sys
import time
ECHO_PORT = 55555
BUFSIZE = 1024
H = 1
send = "0;0;00000/70 \n"
liste = [""]
m1 = ""
m2 = ""
r1 = ""
r2 = ""
r3 = ""
r4 = ""
rnotaus = ""
akku1 = ""
A = "A\r\n"
zeilenumbruch = "\r\n"
def main():
s = socket(AF_INET, SOCK_STREAM)
s.bind(('', ECHO_PORT))
s.listen(1)
serialport = serial.Serial("/dev/ttyUSB0", 9600)
print "Server gestartet"
conn, (remotehost, remoteport) = s.accept()
print 'Verbunden mit %s:%s' % (remotehost, remoteport)
while H == 1:
data = conn.recv(BUFSIZE)
liste = data.split(";")
del liste[9]
x = liste[0]
y = liste[1]
s1 = liste[2]
s2 = liste[3]
b1 = liste[4]
b2 = liste[5]
b3 = liste[6]
b4 = liste[7]
bnotaus = liste[8]
serialport.write(A)
serialport.write(x + "\r\n")
serialport.write(y + "\r\n")
serialport.write(s1 + "\r\n")
serialport.write(s2 + "\r\n")
serialport.write(b1 + "\r\n")
serialport.write(b2 + "\r\n")
serialport.write(b3 + "\r\n")
serialport.write(b4 + "\r\n")
serialport.write(bnotaus + "\r\n")
print "gosd" + x + y + s1 + s2 + b1 + b2 +b3 + b4 + bnotaus
m1 = serialport.readline()
m1l = m1.split("\r\n")
m1s = m1l[0]
m2 = serialport.readline()
m2l = m2.split("\r\n")
m2s = m2l[0]
r1 = serialport.readline()
r1l = r1.split("\r\n")
r1s = r1l[0]
r2 = serialport.readline()
r2l = r2.split("\r\n")
r2s = r2l[0]
r3 = serialport.readline()
r3l = r3.split("\r\n")
r3s = r3l[0]
r4 = serialport.readline()
r4l = r4.split("\r\n")
r4s = r4l[0]
rnotaus = serialport.readline()
rnotausl = rnotaus.split("\r\n")
rnotauss = rnotausl[0]
akku1 = serialport.readline()
akku1l = akku1.split("\r\n")
akku1s = akku1l[0]
print m1s + ";" + m2s + ";" + r1s + r2s + r3s +r4s + rnotauss + "/" + akku1s + "\n"
conn.send(m1s + ";" + m2s + ";" + r1s + r2s + r3s + r4s + rnotauss + "/" + akku1s + "\n")
if not data:
# Die Verbindung wurde getrennt
# Abbruch
serialport.close()
break
# Sende die Daten zurück zum client
if __name__ == '__main__':
main()
hier der Bascom Code:
Code:
$regfile = "m2560def.dat"
$hwstack = 82
$framesize = 68
$swstack = 68
$crystal = 16000000
$baud = 19200
'______Onboard-LED___________
Config Pind.5 = Output
Led Alias Portd.5
'----------------------------
'__________Relais _________
Config Pina.0 = Output
Relais1_l Alias Porta.0
Config Pina.1 = Output
Relais1_r Alias Porta.1
Config Pina.2 = Output
Relais2_r Alias Porta.2
Config Pina.3 = Output
Relais2_l Alias Porta.3
'----------------------------
'_________Motor Ports____________
Config Pinl.1 = Output
Motor1_reset Alias Portl.1
Config Pinl.0 = Output
Motor1_richtung Alias Portl.0
Config Pinl.5 = Output
Motor2_reset Alias Portl.5
Config Pinl.4 = Output
Motor2_richtung Alias Portl.4
Config Pinl.2 = Input
Pinl.2 = 1
F1_m1 Alias Portl.2
Config Pinl.3 = Input
Pinl.3 = 1
F2_m1 Alias Portl.3
Config Pinl.6 = Input
Pinl.6 = 1
F1_m2 Alias Portl.6
Config Pinl.7 = Input
Pinl.7 = 1
F2_m2 Alias Portl.7
'--------------------------------
Config Timer5 = Timer , Prescale = 8 'timer für Servos
Enable Timer5
Timer5 = 62535
Config Portc = Output
Portc.0 = 0 'hier hängt servo1
Portc.1 = 0 'hier hängt servo2
Portc.2 = 0 'hier hängt servo3
Portc.3 = 0 'hier hängt servo4
On Timer5 Servoirq
Enable Interrupts
Config Pine.5 = Input
USB Alias Pine.5 'Ist 1 wenn USB angeschlossen
Config Com4 = 9600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Open "com4:" For Binary As #4
Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Up , Prescale = 256 ' PWM für Motor 1+2
Enable Timer1
Led = 0
Motor1_reset = 1
Motor2_reset = 1
Dim I As Integer
Dim Alarm As Byte
Dim Test As String * 5
Dim Pos1 As Byte
Dim X As Integer
Dim X_neu As Integer
Dim Y As Integer
Dim Y_neu As Integer
Dim M1 As Integer
Dim M2 As Integer
Dim B1 As Integer
Dim B2 As Integer
Dim R1 As Integer
Dim R2 As Integer
Dim B3 As Integer
Dim B4 As Integer
Dim Notaus As Integer
Dim Akku1 As Integer
Dim Kanal As Byte
Dim Servo(4) As Word
Dim S1 As Word
Dim S2 As Word
Akku1 = 100
Echo Off
I = 0
Servo(1) = 62535
Servo(2) = 62535
Servo(3) = 62535
Servo(4) = 62535
B1 = 0
B2 = 0
R1 = 0
R2 = 0
Do
If Akku1 = 0 Then
Akku1 = 100
Else
Akku1 = Akku1 - 1
End If
Alarm = F1_m1
Alarm = Alarm + F1_m2
Alarm = Alarm + F2_m1
Alarm = Alarm + F2_m2
Alarm = 0 ' Fehlermeldungsproblem muss noch behoben werden!!!!!!
If Alarm > 0 Then
Else
'__________Joystick Koordinaten lesen und verarbeiten_________
Input #4 , Test
Pos1 = Instr(test , "A")
If Pos1 = 1 Then
Input #4 , "" , X
Input #4 , "" , Y
Input #4 , "" , S1
Input #4 , "" , S2
Input #4 , "" , B1
Input #4 , "" , B2
Input #4 , "" , B3
Input #4 , "" , B4
Input #4 , "" , Notaus
If Notaus = 0 Then
Servo(1) = 61535 + S1
Servo(2) = 63535 - S2
If B1 = R1 Then
Elseif B1 = 1 Then
Relais1_r = 1
Waitms 250
Relais1_r = 0
R1 = 1
Elseif B1 = 0 Then
Relais1_l = 1
Waitms 250
Relais1_l = 0
R1 = 0
End If
If B2 = R2 Then
Elseif B2 = 1 Then
Relais2_l = 1
Waitms 250
Relais2_l = 0
R2 = 1
Elseif B2 = 0 Then
Relais2_r = 1
Waitms 250
Relais2_r = 0
R2 = 0
End If
If X < 0 Then
X = 32767 + X
X = X / 128
Else
X = 32767 - X
X = X * -1
X = X / 128
End If
If Y < 0 Then
Y = 32767 + Y
Y = Y * -1
Y = Y / 128
Else
Y = 32767 - Y
Y = Y / 128
End If
If Y > 0 Then
If X < 0 Then
X_neu = X * -1
If Y > X_neu Then
M1 = Y
Else
M1 = X
M1 = M1 * -1
End If
Else
M1 = Y - X
End If
Else
If X > 0 Then
Y_neu = Y * -1
If Y_neu > X Then
M1 = Y
Else
M1 = X
M1 = M1 * -1
End If
Else
M1 = Y - X
End If
End If
If Y > 0 Then
If X > 0 Then
If Y > X Then
M2 = Y
Else
M2 = X
End If
Else
M2 = Y + X
End If
Else
If X < 0 Then
If Y < X Then
M2 = Y
Else
M2 = X
End If
Else
M2 = Y + X
End If
End If
M1 = M1 / 2
M2 = M2 / 2
If M1 > 5 Then
Motor1_richtung = 1
Compare1a = M1
Elseif M1 < -5 Then
Motor1_richtung = 0
Compare1a = -m1
Else
Compare1a = 0
End If
If M2 > 5 Then
Motor2_richtung = 1
Compare1b = M2
Elseif M2 < -5 Then
Motor2_richtung = 0
Compare1b = -m2
Else
Compare1b = 0
End If
Else
Compare1a = 0
Compare1b = 0
End If
End If
End If
'-------------------------------------------
Print #4 , M1
Print #4 , M2
Print #4 , R1
Print #4 , R2
Print #4 , B3
Print #4 , B4
Print #4 , Notaus
Print #4 , Akku1
Loop
Servoirq:
If Kanal = 0 Then
If Portc.0 = 0 Then
Timer5 = Servo(1)
Portc.0 = 1
Else
Portc.0 = 0
Incr Kanal
End If
End If
If Kanal = 1 Then
If Portc.1 = 0 Then
Timer5 = Servo(2)
Portc.1 = 1
Else
Portc.1 = 0
Incr Kanal
End If
End If
If Kanal = 2 Then
If Portc.2 = 0 Then
Timer5 = Servo(3)
Portc.2 = 1
Else
Portc.2 = 0
Incr Kanal
End If
End If
If Kanal = 3 Then
If Portc.3 = 0 Then
Timer5 = Servo(4)
Portc.3 = 1
Else
Portc.3 = 0
Incr Kanal
End If
End If
If Kanal = 4 Then
Timer5 = 40000
Kanal = 0
End If
Return
End
Das Problem ist was vom RN-Mega2560 kommt wird zwar richtig vom Server empfangen und richtig weiter an den Client gesendet, aber was an das RN-Mega2560 vom RPi gesendet wird, empfängt dieser wohl nicht richtig. Empfangen werden soll es "A" das klappt, dann x(0), y(0), s1(1000), s2(1000), b1(0), b2(0), b3(0), b4(0), bnotaus(0). Bei diesen Werten sollte zurückkommen m1(-255), m2(255), r1(0), r2(0), r3(0), r4(0), rnotaus(0), akku1(langsam von 100 bis 0 und dann wieder bei 100 anfangen). Wenn jetzt der Joystick neutral steht(x= 37268, y= 37268 und zurückkommen m1 = 0 m2 = 0) und das selbe bei den Buttons wenn 1 gesendet wird soll 1 zurückkommen.
Mit dem alten vb.net Server hat das prima funktionier, hier mal der Code für den alten vb.net Server:
Code:
Imports System.Text
Imports System.Net.Sockets
Imports System.Threading
Imports System.IO
Public Class Form1
Dim X As String = "32767"
Dim Y As String = "32767"
Dim B1 As String = "0"
Dim B2 As String = "0"
Dim B3 As String = "0"
Dim B4 As String = "0"
Dim notaus As String = "0"
Dim M1 As String = "0"
Dim M2 As String = "0"
Dim S1 As String = "1000"
Dim S2 As String = "1000"
Dim R1 As String = "0"
Dim R2 As String = "0"
Dim R3 As String = "0"
Dim R4 As String = "0"
Dim Rnotaus As String = "0"
Dim Akku1 As String = "78"
Dim port As Integer = 31415
Dim tcpclient As TcpClient
Dim tcplistener As New TcpListener(port)
Dim networkstream As NetworkStream
Dim streamw As StreamWriter
Dim streamr As StreamReader
Dim clientmessage As String
Dim thread_server As System.Threading.Thread
Private timer1 As New System.Timers.Timer
Private timer2 As New System.Timers.Timer
Dim down As Byte
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SerialPort1.PortName = TextBox1.Text
SerialPort1.Open()
Timer3.Start()
Timer4.Start()
TextBox1.BackColor = Color.Lime
multi_server()
End Sub
Private Sub multi_server()
thread_server = New System.Threading.Thread(AddressOf server)
thread_server.Start()
End Sub
Private Sub server()
tcplistener.Start()
ListBox1.Items.Add("[SERVER]: ist ONLINE")
ListBox1.SelectedIndex = ListBox1.Items.Count - 1
Try
tcpclient = tcplistener.AcceptTcpClient
ListBox1.Items.Add("[CLIENT]: Connected")
ListBox1.SelectedIndex = ListBox1.Items.Count - 1
notaus = 0
timer1.Start()
timer2.Start()
Catch ex As Exception
MsgBox("FAIL_____FAIL_____FAIL")
End Try
End Sub
Private Sub Timer_empfangen(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs)
o:
Dim pos5 As Integer
Dim pos1 As Integer
Dim pos2 As Integer
Dim pos3 As Integer
Dim pos4 As Integer
networkstream = tcpclient.GetStream()
Dim streamr = New StreamReader(networkstream)
clientmessage = streamr.ReadLine()
pos1 = InStr(1, clientmessage, ";")
pos2 = InStr(1, clientmessage, ",")
pos3 = InStr(1, clientmessage, "!")
pos4 = InStr(1, clientmessage, "§")
pos5 = InStr(1, clientmessage, "clientdown")
If pos5 > 0 Then
timer1.Stop()
timer2.Stop()
notaus = 1
tcpclient.Close()
ListBox1.Items.Add("[CLIENT]: has Disconnected")
ListBox1.SelectedIndex = ListBox1.Items.Count - 1
server()
GoTo o
Else
X = Mid(clientmessage, 1, pos1 - 1)
Y = Mid(clientmessage, pos1 + 1, pos2 - pos1 - 1)
S1 = Mid(clientmessage, pos2 + 1, pos3 - pos2 - 1)
S2 = Mid(clientmessage, pos3 + 1, pos4 - pos3 - 1)
B1 = Mid(clientmessage, pos4 + 1, 1)
B2 = Mid(clientmessage, pos4 + 2, 1)
B3 = Mid(clientmessage, pos4 + 3, 1)
B4 = Mid(clientmessage, pos4 + 4, 1)
notaus = Mid(clientmessage, pos4 + 5, 1)
TextBox2.Text = X & "/" & Y & "/" & S1 & "/" & S2 & "/" & B1 & B2 & B3 & B4 & notaus
ListBox1.Items.Add("[CLIENT]: " + clientmessage)
ListBox1.SelectedIndex = ListBox1.Items.Count - 1
End If
End Sub
Private Sub Form1_FormClosend(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
tcpclient.Close()
tcplistener.Stop()
End Sub
Private Sub Timer_senden(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs)
networkstream = tcpclient.GetStream()
Dim streamw = New StreamWriter(networkstream)
TextBox5.Text = M1 & ";" & M2 & ";" & R1 & R2 & R3 & R4 & Rnotaus & "/" & Akku1
streamw.WriteLine(M1 & ";" & M2 & ";" & R1 & R2 & R3 & R4 & Rnotaus & "/" & Akku1)
streamw.Flush()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Control.CheckForIllegalCrossThreadCalls = False
AddHandler timer1.Elapsed, New System.Timers.ElapsedEventHandler(AddressOf Me.Timer_empfangen)
timer1.Interval = 1
AddHandler timer2.Elapsed, New System.Timers.ElapsedEventHandler(AddressOf Me.Timer_senden)
timer2.Interval = 4
End Sub
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
If SerialPort1.BytesToRead > 0 Then
Do
M1 = SerialPort1.ReadLine()
M2 = SerialPort1.ReadLine()
R1 = SerialPort1.ReadLine()
R2 = SerialPort1.ReadLine()
R3 = SerialPort1.ReadLine()
R4 = SerialPort1.ReadLine()
Rnotaus = SerialPort1.ReadLine()
Akku1 = SerialPort1.ReadLine()
TextBox4.Text = M1 & ";" & M2 & ":" & R1 & R2 & R3 & R4 & Rnotaus & "/" & Akku1
If SerialPort1.BytesToRead = 0 Then
Exit Do
End If
Loop
End If
End Sub
Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
TextBox3.Text = X & "/" & Y & "/" & S1 & "/" & S2 & "/" & B1 & B2 & B3 & B4 & notaus
SerialPort1.Write("A")
SerialPort1.Write(Chr(13))
SerialPort1.Write(X)
SerialPort1.Write(Chr(13))
SerialPort1.Write(Y)
SerialPort1.Write(Chr(13))
SerialPort1.Write(S1)
SerialPort1.Write(Chr(13))
SerialPort1.Write(S2)
SerialPort1.Write(Chr(13))
SerialPort1.Write(B1)
SerialPort1.Write(Chr(13))
SerialPort1.Write(B2)
SerialPort1.Write(Chr(13))
SerialPort1.Write(B3)
SerialPort1.Write(Chr(13))
SerialPort1.Write(B4)
SerialPort1.Write(Chr(13))
SerialPort1.Write(notaus)
SerialPort1.Write(Chr(13))
End Sub
End Class
Private Sub Timer4_Tick ist der Teil in dem an das RN-Mega2560 gesendet wird und Private Sub Timer3_Tick wo die Daten vom RN-Mega2560 empfangen werden.
Mfg McGrrizzly
Lesezeichen