Answer the question
In order to leave comments, you need to log in
Why doesn't python open serial port via browser?
Good afternoon.
Faced such a problem.
I'm trying to give a command to arduin'e through the browser. But the script hangs in the place where there is a connection to the serial port. Everything works in the terminal.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import serial
import cgi, cgitb
import sys
print ("STATUS: 200 OK\n")
req = cgi.FieldStorage();
ser = serial.Serial("/dev/ttyUSB1", 115200, timeout=3, dsrdtr = 1)
ser.isOpen()
ser.write("1")
ser.close()
#print("Content-type:text/html\r\n\r\n")
print ("okey")
$(function() {
$(".ok").click(function() {
$(this).parent();
var PinID = $(this).attr("id");
$.ajax(
{
url: "../cgi/pin.py",
type: "post",
datatype: "html",
data:{
PinID: PinID
},
success: function(html){
$("#pin").html(html);
}
});
});
});
int led=13;
void setup() {
Serial.begin(115200);
pinMode(led, OUTPUT);
}
void loop()
{
int i=0;
char buffer[100];
//если есть данные - читаем
if(Serial.available())
{
delay(100);
//загоняем прочитанное в буфер
while(Serial.available() && i< 99)
{
buffer[i++] = Serial.read();
}
//закрываем массив
buffer[i++]='\0';
}
//если буфер наполнен
Serial.println(buffer);
if ((String)buffer == "1")
{
digitalWrite(led, HIGH);
}
if ((String)buffer == "2")
{
digitalWrite(led, LOW);
}
buffer[0] = '\0';
delay(1000);
}
Answer the question
In order to leave comments, you need to log in
If you are working with a serial port through a device file, it is easiest to open it as a normal file. See examples for the post My first shield and magic screen
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question