An optical signal encoding method for visible light communications, comprising the following steps: reading N-ary data bit by bit, and encoding the data into electric signal units: encoding each different digit in the N-ary data into a different electric signal unit, wherein a sequence of high level duration and low level duration in the electric signal unit is Ti1, Ti2,..., and Tij, i, j, and N are natural numbers, and the different electric signal units are separated by delimiters, calculated values between Ti2, Ti3,..., and Tij and Ti1 are predetermined values or in predetermined ranges, where the calculation refers to calculating a ratio, a product, a difference, a sum, a reciprocal, and/or a remainder after division between Ti2, Ti3..., Tij and Ti1; encoding the electrical signal units depending on the calculated values; and converting the encoded electric signal units into optical signals; wherein the delimiters are level transitions; or, high and low levels with different duration are characteristic levels, and the delimiters are reference levels different from the characteristic levels; when Tij/Ti1≈1, binary data represented by the electric signal unit is 0; Tij/Ti1≈ m, and m≠1, binary data represented by the electric signal unit is 1, wherein the predetermined value m is a specified coefficient, or when |Tij -Ti1|≤T0, binary data represented by the electric signal unit is 0; and when |Tij -Ti1|>T0, binary data represented by the electric signal unit is 1, or when (Tij + Ti1)≤T0, binary data represented by the electric signal unit is 0; and when (Tij + Ti1)>T0, binary data represented by the electric signal unit is 1, or when (Tij ∗ Ti1)≤T0, binary data represented by the electric signal unit is 0; and when (Tij ∗ Ti1)>T0, binary data represented by the electric signal unit is 1 wherein the predetermined value T0 is a preset decision duration. reading N-ary data bit by bit, and encoding the data into electric signal units: encoding each different digit in the N-ary data into a different electric signal unit, wherein a sequence of high level duration and low level duration in the electric signal unit is Ti1, Ti2,..., and Tij, i, j, and N are natural numbers, and the different electric signal units are separated by delimiters, calculated values between Ti2, Ti3,..., and Tij and Ti1 are predetermined values or in predetermined ranges, where the calculation refers to calculating a ratio, a product, a difference, a sum, a reciprocal, and/or a remainder after division between Ti2, Ti3..., Tij and Ti1; encoding the electrical signal units depending on the calculated values; and encoding each different digit in the N-ary data into a different electric signal unit, wherein a sequence of high level duration and low level duration in the electric signal unit is Ti1, Ti2,..., and Tij, i, j, and N are natural numbers, and the different electric signal units are separated by delimiters, calculated values between Ti2, Ti3,..., and Tij and Ti1 are predetermined values or in predetermined ranges, where the calculation refers to calculating a ratio, a product, a difference, a sum, a reciprocal, and/or a remainder after division between Ti2, Ti3..., Tij and Ti1; encoding the electrical signal units depending on the calculated values; and converting the encoded electric signal units into optical signals; wherein the delimiters are level transitions; or, high and low levels with different duration are characteristic levels, and the delimiters are reference levels different from the characteristic levels; when Tij/Ti1≈1, binary data represented by the electric signal unit is 0; Tij/Ti1≈ m, and m≠1, binary data represented by the electric signal unit is 1, wherein the predetermined value m is a specified coefficient, or when |Tij -Ti1|≤T0, binary data represented by the electric signal unit is 0; and when |Tij -Ti1|>T0, binary data represented by the electric signal unit is 1, or when (Tij + Ti1)≤T0, binary data represented by the electric signal unit is 0; and when (Tij + Ti1)>T0, binary data represented by the electric signal unit is 1, or when (Tij ∗ Ti1)≤T0, binary data represented by the electric signal unit is 0; and when (Tij ∗ Ti1)>T0, binary data represented by the electric signal unit is 1 wherein the predetermined value T0 is a preset decision duration.