serial communication python

Solutions on MaxInterview for serial communication python by the best coders in the world

showing results for - "serial communication python"
Luigi
15 Apr 2018
1//python
2
3import serial
4
5port = serial.Serial('COM5',9600)
6
7while( port.isOpen()):
8    comdata = int(input())
9
10    if(comdata == 1):
11        port.write(str.encode('1'))
12    elif(comdata == 2):
13        port.write(str.encode('2'))
14
15    elif (comdata == 3):
16        port.write(str.encode('3'))
17    elif (comdata == 4):
18        port.write(str.encode('4'))
19
20    elif (comdata == 5):
21        port.write(str.encode('5'))
22    elif (comdata == 6):
23        port.write(str.encode('6'))
24
25    elif (comdata == 7):
26        port.write(str.encode('7'))
27    elif (comdata == 8):
28        port.write(str.encode('8'))
29
30    else:
31        print('Invalid input!!!!')
32        
33// arduino
34
35const int LED1 = 8;
36const int LED2 = 9;
37const int LED3 = 10;
38const int LED4 = 11;
39char comdata;
40
41void setup() {
42
43  pinMode(LED1, OUTPUT);
44  pinMode(LED2, OUTPUT);
45  pinMode(LED3, OUTPUT);
46  pinMode(LED4, OUTPUT);
47  Serial.begin(9600);
48}
49
50void loop() {
51
52  if (Serial.available() > 0) {
53      comdata =(Serial.read());
54      
55    if (comdata == '1') {
56      Serial.println("LED1 is ON");
57      digitalWrite(LED1, HIGH);
58    }  
59    else if (comdata == '2') {
60      Serial.println("LED1 is OFF");
61      digitalWrite(LED1, LOW);
62    }
63    
64    
65    else if (comdata == '3' ) {
66      Serial.println("LED2 is ON");
67      digitalWrite(LED2, HIGH);
68    }
69    else if (comdata == '4' ) {
70      Serial.println("LED2 is OFF");
71      digitalWrite(LED2, LOW);
72    }
73    
74    else if (comdata == '5') {
75      Serial.println("LED3 is ON");
76      digitalWrite(LED3, HIGH);
77    }
78    else if (comdata == '6') {
79      Serial.println("LED3 is OFF");
80      digitalWrite(LED3, LOW);
81    }
82    
83    else if (comdata == '7') {
84      Serial.println("LED4 is ON");
85      digitalWrite(LED4, HIGH);
86    }
87    
88    else if (comdata == '8') {
89      Serial.println("LED4 is OFF");
90      digitalWrite(LED4, LOW);}
91
92
93    
94  }
95}
96
97
queries leading to this page
python read from serial portpython read serial with marjerspython serial write pyserial listenpython3 serial reading examplehow to use python serial modulepython view serial communicationpython serialaserial serail 28 29serial read in pythonread serial port pythonpython serial ir communication examplepython serial serialuart pythouart pythonpyserial uart examplereading serial string pyserialpython uartpython write to serial portserial 28 29 pythonpython serielpyserial read linepython library for serial communicationprint in serial pythonpython serial communication readpython serial read linepython with serial 28 29 as portread serial pythonpython read 2fwrite serial portserialport pythonpython to python serial communication without pyserialserial python usingpython com portpython serial communication webpython serial communication codeserial terminal pythonpython serial port exampleserial serial pythonpython usb serial communicationserial in pythonserial python communicationpython serial communication examplepython serial readline timeoutpython serial communication desktoppython pyserial exampleserial communication with pythonpython serial commandspython serial readlinepyserial readpyserial tutorialserial ports pythonpython serial communicationpython com port stop read at 10047read serial data pythonpyserial code examplecreate a serial terminal on pythonpython serial port communication examplepython serial withpython serial 5cpython com port stop read writepyserial readlinepython serial communication serverpyserial read serial port in linuxseriall on pythonpython open serial line on produced idpython serial examplehow to use serial port in pythonpython install serial communicationserial pythonlisten to a com port with pyserialpython2 serialcom port pythonpython ser readserial python examplepython serial interfaceserial readline pythonhow to get serial in pythonreadline serial pythonpython serial communication linuxserials port pythonpython with serial serial communication python windowspython serial communication pyserialserial communication in pythonpyserial examplepyserial com portpython and serial communicationhow to use com port in pythonpython serial readserial port python examplserial communication in oythonhow to use the serial module in python 3fserial port pythonserial communication pythonpython com port communicationwrite over serial port pythonport com pythonserial communication python