The online racing simulator
Strange problem with outgauge, and arduino
Hello, i got strange problem with sending outgaugepack through serial, when ill send car pack, everything is good, and on my LCD i got data, but when i want send rpm pack or other pack, through serial, i cant, cause python give me that error "SerialException: could not open port 'COM3': WindowsError(5, acces is denied".Its very strange, cause when I sending car pack, everything works properly.

CODE

import socket
import struct
import serial
ser = serial.Serial(2, 115200)

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

sock.bind(('127.0.0.1', 3))

while True:

data = sock.recv(256)

if not data:
break


outgauge_pack = struct.unpack('I3sxH2B7f2I3f15sx15sx', data)
time = outgauge_pack[0]
car = outgauge_pack[1]
flags = outgauge_pack[2]
gear = outgauge_pack[3]
speed = outgauge_pack[5]
rpm = outgauge_pack[6]
turbo = outgauge_pack[7]
engtemp = outgauge_pack[8]
fuel = outgauge_pack[9]
oilpressure = outgauge_pack[10]
oiltemp = outgauge_pack[11]
dashlights = outgauge_pack[12]
showlights = outgauge_pack[13]
throttle = outgauge_pack[14]
brake = outgauge_pack[15]
clutch = outgauge_pack[16]
display1 = outgauge_pack[17]
display2 = outgauge_pack[18]
ser.write( rpm )

sock.close()
Why are you requesting Python support on LFS forum?

Anyway, as I can see, you dont open/close your serial connection. Try to add the following:

if not ser.isOpen():
ser.open()
print('COM3 IS NOW OPENED', ser.isOpen())


Then, you should have your serial connection opened. If not, it means something else is using COM3. If my code works for you, dont forget to close the serial after script ending.


EDIT: Could you share a photo of what you have done already? I'm trying to build something similar, just for fun.
Quote from Skinzinho :Why are you requesting Python support on LFS forum?

Anyway, as I can see, you dont open/close your serial connection. Try to add the following:

if not ser.isOpen():
ser.open()
print('COM3 IS NOW OPENED', ser.isOpen())


Then, you should have your serial connection opened. If not, it means something else is using COM3. If my code works for you, dont forget to close the serial after script ending.


EDIT: Could you share a photo of what you have done already? I'm trying to build something similar, just for fun.

Did you excatlly readed my post ? I got opened COM3 (and its use by arduino), cause when i sending car pack ( outgauge_pack[1] ), i got data on my LCD ( name of car, which i got now ), but when i want send RPM pack or other pack besides car pack, i got that "float' object is not iterable".Error of "COM is close, acces is denied" is my bad, just when runing program i got that error, cause i have to wait a while to open port, for next run of program.So, my problem is that error "float' object is not iterable".

And yes, i can send u photos, but its only alfanumeric LCD, pluged to arduino, nothing more.
Yes, I read your first post, but looks like you didn't.

If you want help, the first thing you should learn is about to give details. You say "Did you excatlly readed my post?", what is a grossly way (and I'm trying to be polite, and not to swear at you). But take a look on what you say about the error you get in the first topic: "python give me that error "SerialException: could not open port 'COM3': WindowsError(5, acces is denied", and there's no calling to SER.OPEN in your code. Ok, you can call it from another part of your software, but if you don't give DETAILS, we cannot help much. Mind reading isn't a skill of mine.

What is your Python version? Because if you are a programmer, you know that version 2 is different than 3, including the way you call functions.

Is your code exactly like this: "ser.write( rpm )"? I mean with the spaces between rpm and parenthesis.

Second, did you try to print the rpm value to check if you really get the data there?

Third, are you sure that you can write directly as float and do not need to str the value before sending?
Sorry for my rude behavior.My python ver is 2.7.Spaces, between rpm and parentesis didnt make a diffrence.Yes, i got data from "print" fuction in float.And third, its a problem, when I even convert float, to int or string, i got the all time the same result "float' object is not iterable"
Does this work in Python?


rpmBytes = "{.1f}".format(rpm).encode("latin1")
ser.write(rpmStr)

I assume that you need to send an array of bytes down the socket and float or Python 2 string cannot be treated as such directly.
When i use that piece of code "rpmBytes = "{.1f}".format(rpm).encode("latin1")
ser.write(rpmStr)"

unfortunately, i got that error "AttributeError: 'float' object has no attribute '1f'"

Problem is, in these pack they are in float, i can send car pack, cause ( i think ), its in ASCII.So, my solve is convert floats to ints.
#8 - expr
The way to format numbers in Python (IIRC) is with either of:

"{:.1f}".format(num)
"%.1f" % num

In the shown case the format specifier ".1f" means to format num as a fixed point number with 1 digit after the decimal separator.

(Resulting in a string, which you have to encode to convert it to raw bytes in Python3.)

FGED GREDG RDFGDR GSFDG